Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 储存;“二进制”;C程序中的数据类型_C++ - Fatal编程技术网

C++ 储存;“二进制”;C程序中的数据类型

C++ 储存;“二进制”;C程序中的数据类型,c++,C++,我需要创建一个程序,将一个数字系统转换为其他数字系统。我在Windows(Dev C++)中使用了itoa,唯一的问题是我不知道如何将二进制数转换为其他数字系统。所有其他数字系统的转换都相应地工作。这是否涉及使用%存储要转换的输入 以下是我的工作片段: case 2: { printf("\nEnter a binary number: "); scanf("%d", &num); itoa(num,

我需要创建一个程序,将一个数字系统转换为其他数字系统。我在Windows(Dev C++)中使用了itoa,唯一的问题是我不知道如何将二进制数转换为其他数字系统。所有其他数字系统的转换都相应地工作。这是否涉及使用%存储要转换的输入

以下是我的工作片段:

case 2:
        {
            printf("\nEnter a binary number: ");
            scanf("%d", &num);
            itoa(num,buffer,8);
            printf("\nOctal        %s",buffer);
            itoa(num,buffer,10);
            printf("\nDecimal      %s",buffer);
            itoa(num,buffer,16);
            printf("\nHexadecimal  %s \n",buffer);
            break;
        }

对于十进制,我使用%d;对于八进制,我使用%o;对于十六进制,我使用%x。二进制的正确答案是什么?谢谢你以后的回答

内存中已经有了二进制形式的数字。这些%d、%o和%x只是一些提示,让类似printf的函数知道如何解释参数并在十进制/八进制/十六进制表示形式之间进行转换。printf没有二进制格式。相反,您可以迭代所有单个位,如下所示:

for(int i = 0; i < sizeof(num)*8; i++)
{
    printf("%u", !!(byte & (1 << i)));
}
printf("\n");
for(int i=0;iprintf(“%u”),!!(字节和(1基本上是重复的:简短回答:没有。您所能做的就是读取字符串(例如,使用
%s
),然后转换(例如,使用
strtol
)。你知道itoa是非标准的,它可能会在其他系统上损坏?谢谢你的回答!是的,我知道。我们有一个关于数字系统转换的项目,我们的老师会在兼容的系统上检查。