# Extracting hex values from character arrays

This is a discussion on Extracting hex values from character arrays within the Windows Programming forums, part of the Platform Specific Boards category; Hi using a Dialog based application under MFC I am trying to parse a character string array and total the ...

1. ## Extracting hex values from character arrays

Hi using a Dialog based application under MFC I am trying to parse a character string array and total the hex value of each character into a final total.

I have checked the 18 pages of char to hex previous questions but can't seem to get the answer I want and most relate to C++ as well.

Every time I get the character I seem to get the decimal value not the Hex value?

I know the answer is trivial but well I am just going around in circles here trying whatever and hoping for no error on compiling but even then I can't seem to get the Hex value of the character.

Any assistance most appreciated and thanks for your time.

2. You're not going to get the answer you want because the conceptual basis of your question is fundamentally flawed.

There is no difference between a hex value and a decimal value. They are the same value.

Most of us have a total of twenty fingers, thumbs, and toes. The number of fingers, thumbs, and toes doesn't change whether we report that number as decimal (20), as hexadecimal (14), as octal (24), or as binary (10100). The value is the same regardless of the format in which it is reported.

What is changing is the way that number is being printed out. In C++
Code:
```#include <iostream>

int main()
{
char i = 20;
std::cout << std::dec << (int)i << " " << std::hex << (int)i << '\n';
}```

3. You use a combo of MFC and C IIRC.

Have you tried....

Code:
`_snprintf(output, len-1,"DEC:%d, HEX:%X",charTotal,charTotal);`

printf Type Field Characters (CRT)

4. [QUOTE=novacain;1155877]You use a combo of MFC and C IIRC.

Have you tried....

Code:
`_snprintf(output, len-1,"DEC:%d, HEX:%X",charTotal,charTotal);`
IIRC sorry I don't understand what that means?

Well I do this for enjoyment not employment and everything is self taught out of books etc. so please excuse the roughness.

Anyway many thanks you put me onto the path I actually got it working for what I was trying to do please see below code and well it works any suggestions on improving it are most welcome.

Code:
```GetDlgItemText(IDC_EDIT1, name, 80);

len = strlen(name);

for(i=0; i<len;i++)
{
_snprintf_s(char_entered, 2, "%X", name[i]);

charin = atoi (char_entered);

Running_Total += charin;
}```

5. Scratch that it doesn't work sorry.

The Running_Total += charin; line must be causing the problem.

On entry to the loop for the name "Ice" first rotation puts 49 Capital "I" into Running_Total excellent.
Second rotation charin contain "63" lower case "c" excellent.

Added together should equal "AC" but is in fact 112.

Gotta be something to do with signed or unsigned integers I'm thinking back to the drawing board.

Code:
```GetDlgItemText(IDC_EDIT1, name, 80);

len = strlen(name);

for(i=0; i<len;i++)
{
_snprintf_s(char_entered, 2, "%X", name[i]);
//convert HEX to DECIMAL
charin = strtol(char_entered,NULL,16);
Running_Total += charin;
}```

7. Originally Posted by novacain
Works perfectly thank you so very much

Now another small problem I have come across I wish to add FF hex to a value so for example adding to 189 I can use "value -= 1" which works perfectly and will negate the value by one.

If I wish to add FF to a much larger value eg. 8117D in assembler the result would be 8135 however I am unable to replicate this in C ?

Again I am thinking the problem has to do with signed and unsigned numbers but hmm anyone have an idea on how to do this ?