perlperl
asked on
convert to hexadecimal
I am trying to understand how the runtime converts this to hex
1. Why do we need this line
s << std::hex;
2) Why does it not work without (unsigned char)
std::stringstream s;
s << std::hex;
int i = 0;
s << std::setw(2) << std::setfill('0') << (unsigned int)(unsigned char)a[i]; // WORKS
s << std::setw(2) << std::setfill('0') << (unsigned int)a[i]; // DOES NOT WORK
1. Why do we need this line
s << std::hex;
2) Why does it not work without (unsigned char)
ASKER
The array was void* and passed to this function as const char*
You could use a 'unsigned int*' cast since it is an array, e.g.
s << std::setw(2) << std::setfill('0') << ((unsigned int*)a)[i];
ASKER
Thanks jkr. that works too
but I am trying to understand my solution as why
this works (unsigned int)(unsigned char)
and why this not (unsigned int)a
but I am trying to understand my solution as why
this works (unsigned int)(unsigned char)
and why this not (unsigned int)a
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.
As to why it does not work without (unsigned char), what is the type of your a array?