how can i convert a hex value of a byte (unsigned char) to a short value?

unsigned char hexByte = 0x02;
short value= ???

thank you for your help!

Recommended Answers

All 5 Replies

I don't know if there's a pre defined function but it shouldn't be too hard to code yourself.
Ignore the first two charactes and then convert from base sixteen to base 10.
You could use a for loop starting at the end of the string and multiply the number by 16 to the power of where it position is for example it would be 2 * 16 to the power of 0
0 * 16 to the power of one etc..
Obviously you will have to convert the character to an integer before doing the calculations

I don't know if there's a pre defined function but it shouldn't be too hard to code yourself.
Ignore the first two charactes and then convert from base sixteen to base 10.
You could use a for loop starting at the end of the string and multiply the number by 16 to the power of where it position is for example it would be 2 * 16 to the power of 0
0 * 16 to the power of one etc..
Obviously you will have to convert the character to an integer before doing the calculations

i think you got me wrong. the hex value is not a c-string, its a "byte"-value. so there are not 4 characters but only one.

how can i convert a hex value of a byte (unsigned char) to a short value?

unsigned char hexByte = 0x02;
short value= ???

thank you for your help!

As usually:

short value = hexByte;

What's a problem?

>how can i convert a hex value of a byte (unsigned char) to a short value?
By assigning it, perhaps? :icon_rolleyes:

unsigned char hexByte = 0x02;
short value = hexByte;

Your question betrays a misunderstanding of how numbers are represented in memory. There's no conversion going on[1]. The internal representation of hexByte is assigned to value, so it doesn't matter if you initialized it with a hexadecimal literal, an octal literal, or built it manually using bitwise operators in base 64. The result will remain the same.

[1] Unless you count the extension of the value from a smaller unsigned integer type to a larger signed integer type.

oh f***..i may have drunk too much yesterday...I thought it could be a problem because short has 2 Bytes and Char only 1...

thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.