简体   繁体   English

从字节转换为字

[英]convert from byte to word

after spending quite some time trying to understand the function of this method, I still couldn't figure what does it do. 在花了很多时间试图理解这种方法的功能之后,我仍然不知道它是做什么的。 As I understand, stateAsBytes should contain hex strings like "\\xA1\\X32\\X89\\XB2", what does stateAsWords[i%5][i/5] |= (unsigned long )(stateAsBytes[i*(64/8)+j]) << (8*j) do? 据我了解,stateAsBytes应该包含十六进制字符串,例如“ \\ xA1 \\ X32 \\ X89 \\ stateAsWords[i%5][i/5] |= (unsigned long )(stateAsBytes[i*(64/8)+j]) << (8*j) ”, stateAsWords[i%5][i/5] |= (unsigned long )(stateAsBytes[i*(64/8)+j]) << (8*j)吗? Why it uses bitwise assignment ? 为什么使用按位分配?

void fromBytesToWords(unsigned long  **stateAsWords, unsigned char *stateAsBytes)
{
  for(int i=0; i<(1600/64); i++) {
    stateAsWords[i%5][i/5] = 0;
    for(int j=0; j<(64/8); j++)
      stateAsWords[i%5][i/5] |= (unsigned long )(stateAsBytes[i*(64/8)+j]) << (8*j);
  }
} 

What it's doing is treating an array of 1600 bytes as an array of little endian 64-bit values and reorganizing them into the five stateAsWords arrays as native 64-bit words: 它的工作是将1600个字节的数组视为小端64位值的数组,并将它们重组为五个stateAsWords数组,作为本机64位字:

                 "index" of the 64 bit value 
                    from the stateAsBytes 
                         buffer

    stateAsWord[0]: 0, 5, 10, 15, 20
    stateAsWord[1]: 1, 6, 11, 16, 21
    stateAsWord[2]: 2, 7, 12, 17, 22
    stateAsWord[3]: 3, 8, 13, 18, 23
    stateAsWord[4]: 4, 9, 14, 19, 24

The bitwise assignment is just building up a native 64-bit word byte-by-byte from the little-endian buffer. 按位分配只是从little-endian缓冲区逐字节构建一个本机64位字。

Note that the code you posted assumes that unsigned long is a 64-bit type so it has some portability issues - in particular it won't work on most 32-bit targets and it won't work on Windows (even 64-bit windows). 请注意,您发布的代码假定unsigned long是64位类型,因此存在一些可移植性问题-特别是它不适用于大多数32位目标,并且不适用于Windows(甚至是64位Windows) )。

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

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