#C - Display Hexidecimal as Little Endian instead of Big Endian
31 messages · Page 1 of 1 (latest)
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.
Please ignore the brute force use of unions, it's just a temporary utility.
I have to flip the results manually, don't I? 😐
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.
This question thread is being automatically marked as solved.
My attempt at rewriting this failed, so re-asking the question so hopefully someone else knows the answer.
What is PRIx32?
maybe you could convert the number to little endian before printing it?
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
I mean just printing out the integer converted to little endian, for example with htole32()
just flip the numbers endianess
@untold field Could you give a sample input and output?
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''
That's what I was looking for IIRC
oh yeah, that should be htobe32
it just needs to be the opposite of the host endian
though I just realized that it converts leading zeros to trailing zeroes
@untold field Do you know an algorithm for adding up the digits of a number?
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.
Will you answer my question?
Afraid I do not.
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.
Wow. that's more than an idea.
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.
Much appreciated!
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;
}
}