简体   繁体   English

将整数打印为4个字节的集合(以Little Endian排列)?

[英]Printing integers as a set of 4 bytes arranged in little endian?

I have an array of 256 unsigned integers called frequencies[256] (one integer for each ascii value). 我有一个256个无符号整数的数组,这些整数称为frequency [256](每个ascii值一个整数)。 My goal is to read through an input and for each character i increment the integer in the array that corresponds to it (for example the character 'A' will cause the frequencies[65] integer to increase by one) and when the input is over I must output each integer as 4 characters in little endian form . 我的目标是读取输入,对于每个字符,我将增加与之对应的数组中的整数(例如,字符“ A”将使frequencys [65]整数增加1),并且当输入结束时我必须以小尾数形式将每个整数输出为4个字符。

So far I have made a loop that goes through the input and increases each corresponding integer in the array. 到目前为止,我已经做了一个遍历输入的循环,并增加了数组中每个对应的整数。 But i am very confused on how to output each integer in little endian form. 但是我对如何以小端序形式输出每个整数感到非常困惑。 I understand that each byte of the four bytes of each integer should be output as a character (for instance the unsigned integer 1 in little endian is "00000001 00000000 00000000 00000000" which i would want to output as the 4 ascii characters that correspond to those bytes). 我知道每个整数的四个字节中的每个字节都应作为字符输出(例如,little endian中的无符号整数1是“ 00000001 00000000 00000000 00000000”,我希望将其输出为与那些字符对应的4个ascii字符)个字节)。

But how do i get at the binary representation of an unsigned integer in my code and how would i go about chopping it up and rearranging it? 但是,如何获取代码中无符号整数的二进制表示形式,以及如何将其切碎并重新排列呢?

Thanks for the help. 谢谢您的帮助。

For hardware portability, please use the following solution: 为了实现硬件可移植性,请使用以下解决方案:

int freqs[256];
for (int i = 0; i < 256; ++i)
    printf("%02x %02x %02x %02x\n", (freqs[i] >> 0 ) & 0xFF
                                  , (freqs[i] >> 8 ) & 0xFF
                                  , (freqs[i] >> 16) & 0xFF
                                  , (freqs[i] >> 24) & 0xFF);

You can use memcpy which copies a block of memory. 您可以使用memcpy复制一块内存。

char tab[4] ; 
memcpy(tab, frequencies+i, sizeof(int));

now, tab[0], tab[1], etc. will be your characters. 现在,tab [0],tab [1]等将成为您的角色。

A program to swap from big to little endian: Little Endian - Big Endian Problem . 从大端到小端的交换程序: -大端问题

To understand if your system is little or big endian: https://stackoverflow.com/a/1024954/2436175 . 要了解您的系统是小端字节序还是大端字节序: https : //stackoverflow.com/a/1024954/2436175

Transform your chars/integers in a set of printable bits: https://stackoverflow.com/a/7349767/2436175 以一组可打印的位来转换您的字符/整数: https : //stackoverflow.com/a/7349767/2436175

It's not really clear what you mean by "little endian" here. 目前还不清楚您所说的“小端”。 Integers don't have endianness per se; 整数本身没有字节序; endianness only comes into play when you cut them up into smaller pieces. 仅当将它们切成小块时,endianness才起作用。 So which smaller pieces to you mean: bytes or characters. 因此,您要指出的是较小的部分:字节或字符。 If characters, just convert in the normal way, and reverse the generated string. 如果是字符,则以常规方式转换,然后反转生成的字符串。 If bytes (or any other smaller piece), each individual byte can be represented as a function of the int : i & 0xFF calculates the low order byte, (i >> 8) & 0xFF the next lowest, and so forth. 如果是字节(或其他较小的字节),则每个字节都可以表示为int的函数: i & 0xFF计算低位字节, (i >> 8) & 0xFF第二个字节,依此类推。 (If the bytes aren't 8 bits, then change the shift value and the mask correspondingly.) (如果字节不是8位,则相应地更改移位值和掩码。)

And with regards to your second paragraph: a single byte of an int doesn't necessarily correspond to a character, regardless of the encodig. 关于第二段: int的单个字节不一定与字符相对应,而与encodig无关。 For the four bytes you show, for example, none of them corresponds to a character in any of the usual encodings. 例如,对于您显示的四个字节,它们中的任何一个都不对应于任何常用编码中的字符。

With regards to the last paragraph: to get the binary representation of an unsigned integer, use the same algorithm that you would use for any representation: 关于最后一段:要获取无符号整数的二进制表示形式,请使用与任何表示形式相同的算法:

std::string
asText( unsigned int value, int base, int minDigits = 1 )
{
    static std::string digits( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
    assert( base >= 2 && base <= digits.size() );
    std::string results;
    while ( value != 0 || minDigits > 0 ) {
        results += digits[ value % base ];
        value /= base;
        -- minDigits;
    }
    //  results is now little endian.  For the normal big-endian
    std::reverse( results.begin(), results.end() );
    return results;
}

Called with base equal to 2, this will give you your binary representation. 以等于2的底数进行调用,这将为您提供二进制表示形式。

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

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