简体   繁体   English

unsigned long long的二进制表示

[英]Binary representation of an unsigned long long

I am trying to get the binary form of an unsigned long long and store each bit of it in an array. 我试图获取无符号long long的二进制形式并将其每一位存储在数组中。

I have an input file like this: 我有一个像这样的输入文件:

0000000000000000    0000000000000000
FFFFFFFFFFFFFFFF    FFFFFFFFFFFFFFFF
3000000000000000    1000000000000001

where each entry is a 64-bit integer represented in hex. 其中每个条目都是一个以十六进制表示的64位整数。 I am using an unsigned long long to hold this value then iterating over the bits and attempting to store them in an array, but some of the arrays have bits in the wrong position. 我正在使用一个无符号的long long来保存此值,然后遍历位并尝试将它们存储在数组中,但是某些数组的位位置错误。

Here is what I have: 这是我所拥有的:

char key_in[17];
char plaintext_in[17];

//64-bit long variables to hold the 64-bit hex values in the input file
unsigned long long key, plaintext;

//I read an entry from the file with fscanf
fscanf(infile,"%s %s",&key_in, &plaintext_in)

//convert the numbers from hex to unsigned long long with strtoull
key = strtoull(key_in, NULL, 16);
plaintext = strtoull(plaintext_in, NULL, 16);

//initialize arrays with 64 positions that will hold the 
//binary representation of the key and plaintext
int key_arr[64];
int pt_arr[64];

//fill the arrays with the binary representations
//of the plaintext and the key
int64_to_bin_array(key, key_arr, 64);
int64_to_bin_array(plaintext, pt_arr, 64);    

//print both arrays
printArray(key_arr, 64);
printArray(pt_arr,  64);

here are the functions I created int64_to_bin_array and printArray : 这里是我创建的功能int64_to_bin_arrayprintArray

/* Converts from an unsigned long long into an array of 
integers that form the binary representation of a */
void int64_to_bin_array(unsigned long long a, int *b, int length)
{
   int i;
   for(i = 0; i < length; i++)
   {
      *(b+i) = (a >> i) & 1; //store the ith bit in b[i]
   }
}

/* prints a one-dimensional array given
   a pointer to it, and its length */
void printArray(int *arr, int length)
{
   int i;
   for(i = 0; i < length; i++)
   {
      printf("%d ", *(arr + i));
   }
   printf("\n\n");
}   

When I print the array for the third input however, I receive an incorrect result: 但是,当我为第三个输入打印数组时,收到错误的结果:

input (in hex): 输入(十六进制):

1.  3000000000000000    2.  1000000000000001   

output (in binary): 输出(二进制):

1    00000000 00000000 00000000 00000000 00000000 00000000 00000000 00001100 

2    10000000 00000000 00000000 00000000 00000000 00000000 00000000 00001000

Can anyone see where I have made a mistake? 谁能看到我在哪里弄错了?

EDIT 编辑

I get the correct output after both reading and printing in reverse, but my problem is I need the array to have its most significant byte first so I can manipulate it. 在反向读取和打印后,我得到正确的输出,但是我的问题是我需要数组首先具有其最高有效字节,以便可以对其进行操作。 Any ideas how that can be done? 有什么想法可以做到吗? Would I have to reassign it to a new array and copy the elements in reverse? 我是否必须将其重新分配给新数组并反向复制元素?

Try reading it the other way around. 尝试以其他方式阅读。 Let's take the last octet: 让我们看最后一个八位位组:

00001100 = 0x0C
00110000 = 0x30 <---

That corresponds yo your first first octet, 0x30 . 这对应于您的第一个第一个八位字节0x30

For the second number: 对于第二个数字:

00001000 = 0x08
00010000 = 0x10 <---

That corresponds to your first first octet, 0x10 . 这对应于您的第一个第一个八位位组0x10

You'll probably get what you expect if you print it like this: 如果这样打印,您可能会得到期望的结果:

for(i = length - 1; i >= 0; i--)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM