Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert char to hex 1

Status
Not open for further replies.

FoxProProgrammer

Programmer
Apr 26, 2002
967
0
0
US
I have an array that contains a char that represents a hex number. The array might look like the following:

stimByte[0][0] = "13"
stimByte[0][1] = "FB"
stimByte[1][0] = "FC"
stimByte[1][1] = "53"

The hex data is:
0x13FB
0xFC53

I need to convert the char array to hex data.

I tried the following:

val[n] = stimByte[n][0] << 8;
val[n] += stimByte[n][1];

Here's what happens when n = 0.

val[0] = stimByte[0][0] << 8 = 1300 (so far so good)
val[0] +- stimByte[0][1] = 12FB

I don't understand why it calculates 12FB. I get 13FB when I add the numbers manually. Any suggestions?

Thanks!


dz
 
I solved my problem but still don't understand it.

stimByte[n][1] has a value of FFFFFFFB instead of 000000FB. When it is added to 00001300, the result is 12FB. I don't understand why the compiler represents FB as FFFFFFFB, but I masked the upper bits with 000000FF. I'll keep researching this. Sorry for the wasted posts.

dz
 
Use unsigned int instead of int. int causes sign extension.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top