#C - Display Hexidecimal as Little Endian instead of Big Endian

31 messages · Page 1 of 1 (latest)

untold field
#

I for the life of me cannot figure out how to output hexidecimal as little endian, I know there's a way to do it without rewriting the book of life.

I don't really want someone to solve it for me, just help me jog my memory a bit, it's gotta be simple.

quasi nestBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

untold field
#

Please ignore the brute force use of unions, it's just a temporary utility.

#

I have to flip the results manually, don't I? 😐

quasi nestBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

quasi nestBOT
#

This question thread is being automatically marked as solved.

untold field
#

My attempt at rewriting this failed, so re-asking the question so hopefully someone else knows the answer.

jagged flame
#

What is PRIx32?

verbal mural
#

maybe you could convert the number to little endian before printing it?

untold field
#

PRIX32 is a lowercase hexadecimal printf format for uint32_t

#

That's the thing, I'm trying to figure out how, is there a way print it out in little endian

verbal mural
#

I mean just printing out the integer converted to little endian, for example with htole32()

#

just flip the numbers endianess

calm oyster
#

@untold field Could you give a sample input and output?

untold field
#

Input: 30 40 90
Output:
''X Coord : 30.000000 is 0x41f00000
Y Coord : 40.000000 is 0x42200000
Rotation : 90.000000 is 0x42b40000''
Wanted Output:
''X Coord : 30.000000 is 0x0000f041
Y Coord : 40.000000 is 0x00002042
Rotation : 90.000000 is 0x0000b442''

untold field
verbal mural
#

it just needs to be the opposite of the host endian

#

though I just realized that it converts leading zeros to trailing zeroes

calm oyster
#

@untold field Do you know an algorithm for adding up the digits of a number?

untold field
#

Oh my god, digit summing, I was hoping for something akin to htobe32(), but I'm afraid I'm in a Windows DE, which means I can't use endian.h.

calm oyster
#

Will you answer my question?

untold field
#

Afraid I do not.

calm oyster
#
unsigned change_endianness(unsigned x)
{
    unsigned y = 0;
    
    while (x != 0)
    {
        y = (y << 8) + x % 256U;
        x = x / 256U;
    }
    
    return y;
}
#

Here's an idea.

untold field
#

Wow. that's more than an idea.

calm oyster
#
unsigned change_endianness(unsigned x)
{
    unsigned y = 0;
    
    for (int i = 0; i < sizeof x; ++i)
    {
        y = (y << 8) + x % 256;
        x = x / 256;
    }
    
    return y;
}
#

@untold field This implementation is better.

untold field
#

Much appreciated!

jagged flame
#

You could also use smth like

union int_bytes {
  uint32_t val;
  uint8_t bytes[sizeof(uint32_t)];
}

Then construct a union from the passed x and iterate from the last to first array element, which are the individual bytes from least significant to most significant, and print them.

#

Could even do something like:

union int_bytes {
  uint32_t val;
  struct {
    uint8_t most_significant;
    uint8_t second_most_significant;
    uint8_t second_least_significant;
    uint8_t least_significant;
  }
}