C ++基本转换 [英] C++ base conversion
问题描述
您好,我正在尝试将一些代码从Windows移植到Linux.我有这个:
Hello I'm trying to port some code from Windows to Linux. I have this:
itoa(word,aux,2);
但是GCC无法识别itoa.我该如何以C ++方式转换为以2为基础?
But GCC doesn't recognize itoa. How can I do this conversion to base 2 on a C++ way?
谢谢;)
推荐答案
此处一些帮助
/* itoa: convert n to characters in s */
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
您应根据需要对其进行调整(请注意,此参数有2个参数,而不是3个) 还要注意,反向功能也位于Wikipedia链接中.
You should adapt it to your needs (notice this one has 2 arguments, not 3) Also notice the reverse function is also in the wikipedia link.
此外,此处还有其他一些情况(但不适用于基本情况) 2)
Additionally, here are some other cases (but not for base 2)
此功能未在ANSI-C中定义 并且不是C ++的一部分,但是 一些编译器支持.
This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.
符合标准的替代方案 某些情况下可能是sprintf:
A standard-compliant alternative for some cases may be sprintf:
sprintf(str,"%d",value)
转换为十进制基数.
sprintf(str,"%d",value)
converts to decimal base.
sprintf(str,"%x",value)
转换为十六进制基数.
sprintf(str,"%x",value)
converts to hexadecimal base.
sprintf(str,"%o",value)
转换为八进制.
这篇关于C ++基本转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!