简体   繁体   中英

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). 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 .

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).

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.

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

now, tab[0], tab[1], etc. will be your characters.

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 .

Transform your chars/integers in a set of printable bits: 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. 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. (If the bytes aren't 8 bits, then change the shift value and the mask correspondingly.)

And with regards to your second paragraph: a single byte of an int doesn't necessarily correspond to a character, regardless of the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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