jl66
asked on
How to decompose a decimal number into 4 hex numbers with C/C++
1) Have a decimal number, which is < 4 GB
2) want to decompose it into 4 hex numbers in the way shown in the example: For example,
Decimal number: 987654321
Convert it into a hex number: 3ADE68B1
Want it to get 4 hex numbers as follows: 3A, DE, 68, B1
Given a decimal number, how to get 4 hex numbers with C/C++?
2) want to decompose it into 4 hex numbers in the way shown in the example: For example,
Decimal number: 987654321
Convert it into a hex number: 3ADE68B1
Want it to get 4 hex numbers as follows: 3A, DE, 68, B1
Given a decimal number, how to get 4 hex numbers with C/C++?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> Be careful with the memcpy approach
The same is true for the union approach.
The same is true for the union approach.
memcpy copies bytes. bit-shifting extracts bytes. no difference. q. contains no requirements for order.
>> no difference.
Yes there is : in the order of the bytes and the amount of the bytes.
>> q. contains no requirements for order.
Precisely because no restrictions have been mentioned, you need to be careful with the advice. We don't know how big an unsigned int is on the target platform, nor do we know what the endianness of the platform is, so we cannot assume anything.
Why propose a solution that might work on some platforms, if you can propose one that is equally good (if not better) and that will work on any platform ?
Yes there is : in the order of the bytes and the amount of the bytes.
>> q. contains no requirements for order.
Precisely because no restrictions have been mentioned, you need to be careful with the advice. We don't know how big an unsigned int is on the target platform, nor do we know what the endianness of the platform is, so we cannot assume anything.
Why propose a solution that might work on some platforms, if you can propose one that is equally good (if not better) and that will work on any platform ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> the code output should have been that on a little-endian machine (e.g., Intel CPU) , the output will be:
And if sizeof(long) == 4 on that machine.
And if sizeof(long) == 4 on that machine.
It used to be that a long was required to be "at least" 32 bits, and it could be larger. Now it looks like in "ISO/IEC 9899:TC2", it is tightened up to be 32 bits. But better to be sure of your situation.
"ISO/IEC 9899:TC2" --
minimum value for an object of type long int
LONG_MIN -2147483647 // (2^31 1) <== I think this should be (2^31)
maximum value for an object of type long int
LONG_MAX +2147483647 // 2^31 1
maximum value for an object of type unsigned long int
ULONG_MAX 4294967295 // 2^32 1
"ISO/IEC 9899:TC2" --
minimum value for an object of type long int
LONG_MIN -2147483647 // (2^31 1) <== I think this should be (2^31)
maximum value for an object of type long int
LONG_MAX +2147483647 // 2^31 1
maximum value for an object of type unsigned long int
ULONG_MAX 4294967295 // 2^32 1
don't understand the discussion. doesn't seem to have any importance for q.
I needed to clarify my statement in http:#26197918 about what the code produces since it produces different results on different platforms; and if the platform wer a big-endian having a 64 bit long, then the result would be all 0's.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the requirements were to display 32 bit integer 987654321 as 3ADE68B1 what is little endian and display the bytes from high to low as 3A, DE, 68, B1. big endian integer or 64-bit is not required so most comments are answering questions not asked for.
>> the requirements were to display 32 bit integer 987654321
No, the requirements literally state : "Have a decimal number, which is < 4 GB". They say nothing about the type of the decimal number, nor of the size of the decimal number type (except that it has to be at least 32 bits wide to be able to hold all required values).
>> as 3ADE68B1 what is little endian
No. This is a representation of the value. Endianness has no impact on the representation - it is an implementation detail, and defines how the value is STORED (ie. in which order the bytes of the value are laid out in memory, or on disk, or ...).
No, the requirements literally state : "Have a decimal number, which is < 4 GB". They say nothing about the type of the decimal number, nor of the size of the decimal number type (except that it has to be at least 32 bits wide to be able to hold all required values).
>> as 3ADE68B1 what is little endian
No. This is a representation of the value. Endianness has no impact on the representation - it is an implementation detail, and defines how the value is STORED (ie. in which order the bytes of the value are laid out in memory, or on disk, or ...).
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
a decimal number < 4GB is 32 bit. i stop no as it is no help for jl66 only dogmatism.
ASKER
Thank each of you very much. Science has been advanced via positive arguments. Each should get full 500 points. I am testing the solutions.
ASKER
Thank you all so much.
ASKER