将值转换为二进制值并将其存储在数组中 [英] Converting values to binary values and storing them in arrays

查看:103
本文介绍了将值转换为二进制值并将其存储在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些帮助.到目前为止,我已经弄清楚了如何显示输入值的二进制转换,但是我需要一些技巧,以了解如何将这些二进制值添加到数组中,以便实际上能够使用二进制值.是否可以通过开关盒填充阵列?就像我在"0"的情况下那样.由于某些原因,当我打印binary [i]的值时,它将返回0,而不是0000.

Need some help. So far i have figured out how to display the binary conversions of the inputted values, but i need some tips on how to add these binary values into an array in order to actually be able to work with the binary values. Is it possible to populate an array through a switch case? Like i did so in case '0'. For some reason when i print the value of binary[i] it returns 0, instead of 0000.

int main()
{

int i = 0;
char hexstring[] = "24020004";
int binary[8];


while(hexstring[i])
{
     switch(hexstring[i])
     {
         case '0': binary[i] = 0000; printf("%i", binary[i]); printf("0000"); break;
         case '1': printf("0001"); break;
         case '2': printf("0010"); break;
         case '3': printf("0011"); break;
         case '4': printf("0100"); break;
         .
         .
         .
         case 'f': printf("1111"); break;
         default:  printf("\nInvalid hexadecimal digit %c ", hexstring[i]);
    }
    i++;
}
return 0;

}

推荐答案

编写binary[i] = 0000时,是说您正在将 octal 常量分配给binary[i],即0.省略所有前导零.但是第一个零表示该常数应解释为八进制一. 另一个示例:int x = 000011只是意味着为x分配了八进制值11或十进制值9.这就是编译器理解常量的方式.

When you write binary[i] = 0000 you are saying that you are assigning octal constant to binary[i] which is 0. All leading zeros are omitted. But the first zero indicates that the constant should be interpretend as octal one. Another example: int x = 000011 just means that x is assigned an octal value of 11 or decimal value of 9. That is how compiler understands constants.

当该值存储在内存中时,它不知道该值是从哪个基数写入的.如您所知,所有数字都以二进制表示形式存储.但是数字在不同的基础上可能有许多视图.我们很方便地使用十进制 视图中的数字.

When the value is stored in the memory it knows nothing about what base it was written from. All numbers are stored in binary representation as you know. However numbers may have many views in different bases. We are convenient to work with numbers in decimal view.

printf 有很多说明符,用于输出整数表示,它们是%u-十进制,%x-十六进制,%o-八进制.不幸的是,没有输出二进制数字表示形式的选项.为了在base2中输出数字,应该手动计算每个二进制数字(0或1)并按相应的顺序打印它们.

There are many specifiers for printf for integer to output some its representations, they are %u - decimal, %x - hex, %o - for octal. Unfortunately no option to output binary number representation. In order to output number in base2 one should manually calculate each binary digit (0 or 1) and print them in corresponding order.

尝试使用以下代码输出二进制数字表示形式.

Try using the following code to output binary number representation.

void printInBase2(int n) {
    if(n == 0) { printf("0"); return; }
    int b = 0;
    for(b = 0; n >> b + 1; b++);
    for(; b >= 0; b--)
        printf("%c", (n >> b) % 2 + '0');
}

要从存储在字符串中的十六进制表示中读取数字,可以执行以下操作:

To read a number from a hexadecimal representation which is stored in string you can do this:

unsigned int number;
char hexstring[] = "24020004";
sscanf(hexstring, "%x", &number);

如果要将二进制数字存储在int binary[]数组中,则可以执行与字符串相似的操作

In case you want to store binary digits in int binary[] array you can do similar thing you did with string

case '0': binary[4 * i + 3] = 0, binary[4 * i + 2] = 0, binary[4 * i + 1] = 0, binary[4 * i] = 0; break;
case '1': binary[4 * i + 3] = 0, binary[4 * i + 2] = 0, binary[4 * i + 1] = 0, binary[4 * i] = 1; break;
case '2': binary[4 * i + 3] = 0, binary[4 * i + 2] = 0, binary[4 * i + 1] = 1, binary[4 * i] = 0; break;
...
case 'f': binary[4 * i + 3] = 1, binary[4 * i + 2] = 1, binary[4 * i + 1] = 1, binary[4 * i] = 1; break;

这样,您将分别存储每个二进制数字.请注意,您的binary数组应该是hexstring的4倍.

This way you will store each binary digit separately. Note that you binary array should be 4 times longer than hexstring.

这篇关于将值转换为二进制值并将其存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆