Reputation: 69
i'm trying to store bytes of data in to an enum,
enum bufferHeaders
{//output_input
void_void = 0x0003010000,
output_void = 0x00030B8000,
void_input = 0x0006010000,
led_on = 0xff1A01,
led_off = 0x001A01,
};
the leading zero's of my values are being ignored and the compiler stores my values as int.
later I need to be able to find how many bytes each set of data exactly is.
Upvotes: 1
Views: 662
Reputation: 19
Is there a specific need for you to use enums? By default every value will have the same type and you seem to be focused on the numerical value stored. If you are really concerned about the representation it might be a good approach to leave the enum as int and storing the values in a std::map
structure where you can use the enum value as the key itself and the value will be the numerical value whichever way you want to store it.
Upvotes: 1
Reputation: 25526
You are mixing up numerical value and representation!
There are always multiple ways to represent one and the same value: 77, 0x4d, 0x04d (guess, it is my birth year). I won't get older just because you change the representation.
Internally, the CPU has its own representation for a value, which is a specific bit pattern in one if its registers (or somewhere in RAM).
When printing out integer or enum values to console, the internal representation of the CPU is converted to some default representation suitable for most situations. If you want to have another representation, you need to explicitly tell which formatting to apply to retrieve such a representation:
std::cout << std::hex << std::showbase << std::internal << std::setfill('0')
<< std::setw(10) << 77 << std::endl;
Be aware that the stream modifiers above all but std::setw
modify the stream persistently (until next modification), so you need to explicitly revert if you want to have different output format subsequently, whereas std::setw
only applies for next value to be output and has to be re-applied for every subsequent value.
Upvotes: 1
Reputation: 234715
If you write
enum bufferHeaders : int
{
// ToDo - your values here
};
Then it's guaranteed that the backing type is int
, so the number of bytes used to store each value is sizeof(int)
. But then the onus is on you to make sure that the enum
values can fit into an int
: 0xff1A01
for example might not.
Numerically speaking, preserving leading zeros makes no sense at all.
Upvotes: 5