Reputation: 640
I have a C++ app that has many APIs that get's called by different applications. One of the function in the C++ app is,
long void ConvertHexToDec (char* hex, int size)
{
// hex - Hex value passed in as char pointer
// size - size in bytes
//Now for e.g., if the values are ...
// hex = 567D & size = 2
for (int i = 0; i < size; i++)
{
printf ("hex[i] = %x", i, hex[i]);
}
// the above FOR loop will print
// hex[0] = 56
// hex[1] = 7D
// I was hoping to get each digit in a separate index like, hex[0] = 5, hex[1] = 6, hex[2] = 7, hex[3] = D
//the application that calls this C++ API is reading values from a hardware
//device and get the values in hex, and then call this API to convert it to
//decimal.
//so in above example it reads memory location 0xB10A and get a 2 byte value
//of 567D
//I see many examples of hex to decimal conversion in C++, but all of them
//uses logic to convert by taking one value at a time.
//so from above example, it will start at D and then convert that to decimal
//and then take 7 and convert that and then next and so on......
//Here there's no way i can do that, as every byte has 2 digits in it.
//And this is my challenge and i have no idea...
}
What i tried:
string str;
str = "";
for (int i = 0; i < size; i++)
{
printf ("hex[i] = %x", i, hex[i]);
str += hex[i];
}
//But when i print out string value it again comes out as....
for (int i = 0; i < size; i++)
{
printf ("str[i] = %x", i, str[i]);
}
//str[0] = 56
//str[1] = 7D
Also Tried,
std::hex // this gives a junk "decimal" value and that's no where close to the
//real decimal value.
Again am not getting each digit one by one to make the conversion to decimal.
So what i can do to convert this kind of char pointer containing hex to decimal?
Upvotes: 0
Views: 1190
Reputation: 33931
From the description in the code block there doesn't seem to be any need for strings or complicated conversion. They only seem to want to convert a big-endian byte array into a native-endian number.
Comments embedded in the code where more explanation or a warning seemed necessary.
//long void ConvertHexToDec (char* hex, int size) has been changed to
long ConvertHexToDec (const char* hex, int size)
// const char * much more versatile than char * and since we aren't changing hex
// might as well make it const. And what the heck is a long void? A big nothing?
{
long result = hex[0]; // assuming hex not NULL and size > 0
for (int i = 1; i < size; i++) // loop until out of bytes. Note: long might only
// hold 4 bytes.
{
result <<= 8; // shift current data over one byte
result += (unsigned char)hex[i]; // add in new byte. Cast required to avoid sign
// extension during the math if char happens to
// be signed. Note that overflow of the long
// can bring nasty surprises of its own
}
return result;
}
For stuff like this I generally use the fixed width integers in cstdint
rather than types like long
and char
. It can prevent very nasty surprises. Here I would rewrite
uint32_t ConvertHexToDec (const uint8_t* hex, size_t size)
{
if (size > 0 && size <= sizeof(uint32_t)) // no surprises. Up to 4 bytes regardless
// of target, and no signed overflow.
{
uint32_t result = hex[0];
for (size_t i = 1; i < size; i++)
{
result <<= 8;
result += hex[i];
}
return result;
}
throw std::out_of_range("Invalid size"); // can't convert = no result
}
Note that you may have to convert the returned uint32_t
to a signed type. It's usually better to do this after the call and you've tested and confirmed that what you read is valid and usable.
Upvotes: 2
Reputation: 93
I hypothesize param char* hex contains physical address you can read directly.
Then use
long ConvertHexToDec2Bytes(char* hex)
{
const auto n = *(short*)hex;
#ifdef WANNA_COUT
std::cout << n << std::endl;
#endif
#ifdef WANNA_STRING
const auto str = std::to_string(n);
#endif
return n;
}
May you want convert 4 bytes or 8 bytes, and it's really don't have much differentsh.
Upvotes: 0