# Convert Hex to ASCII

• (2 Pages)
• 1
• 2

## 20 Replies - 57959 Views - Last Post: 28 February 2013 - 06:10 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=193194&amp;s=aca55f03c2301dc1887494a2a7348b21&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 woppix

Reputation: 2
• Posts: 127
• Joined: 13-September 09

# Convert Hex to ASCII

Posted 02 October 2010 - 05:24 PM

well.. like the title says i'm trying to convert a hex string into ASCII. I'm having trouble writing the code to do so. I tried to add in a few comments to describe some of whats going on.. hope its enough. The code is actually part of a much bigger project, i just made it into a smaller one for purposes of fixing it. As of right now the output is completely off. Somewhere along the line of working on this i just became confused, so if someone could clear some stuff up for me please do.

```#include <iostream>

using namespace std;

char buffer[100];
char RecvData[64] = "474F4420426C65737320416D657269636121";

int main()
{

char *ptrBuffer = &buffer[0];

for (int i = 0; i < sizeof(RecvData); i+=2)
{
//store the values into variables (
int val1 = RecvData[i];
int val2 = RecvData[i+1];

if val2 is a letter convert it to integer
switch(val2)
{
case 'A':
{
val2 = 10;

}break;
case 'B':
{
val2 = 11;

}break;
case 'C':
{
val2 = 12;

}break;
case 'D':
{
val2 = 13;

}break;
case 'E':
{
val2 = 14;

}break;
case 'F':
{
val2 = 15;

}break;
default: break;
}

int newval;

//convert the two values into decimal form
newval = 16 * val1 + val2;

//change newval into a character
*ptrBuffer = char(newval);

//increment ptrBuffer
cout << ptrBuffer;

ptrBuffer++;
}

system("PAUSE");
return 0;
}

```

This post has been edited by woppix: 02 October 2010 - 06:16 PM

Is This A Good Question/Topic? 0

## Replies To: Convert Hex to ASCII

### #2 Alex6788

Reputation: 144
• Posts: 1,667
• Joined: 15-July 10

## Re: Convert Hex to ASCII

Posted 02 October 2010 - 05:31 PM

This isn't regarding your question but, instead of using system("pause"); use cin.get(); and sometimes that doesn't work if there is already input in the buffer then use

cin.ignore();
cin.get();

Hope that helps.

### #3 woppix

Reputation: 2
• Posts: 127
• Joined: 13-September 09

## Re: Convert Hex to ASCII

Posted 02 October 2010 - 05:51 PM

yes i'm aware, i just kind of threw this into another project quickly and added system("PAUSE") since this project will never be used as a console application anyway. It's actually part of a Asynchronus WinSock application, as you might could tell by some of my variable/array names.

This post has been edited by woppix: 02 October 2010 - 05:54 PM

### #4 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

## Re: Convert Hex to ASCII

Posted 02 October 2010 - 05:52 PM

well what do you mean by Hex to ASCII, that could mean a lot of things. if you mean you want to convert a string representing a hexadecimal value and convert it to an ASCII string representing a Decimal value and you want to use a huge number like that then your going to need to use a bignum library becuase "474F4420426C65737320416D657269636121" wont fit in any stranded c/c++ data type.

### #5 woppix

Reputation: 2
• Posts: 127
• Joined: 13-September 09

## Re: Convert Hex to ASCII

Posted 02 October 2010 - 06:08 PM

i want to split up the hex string into groups of two characters, convert those two into decimal form.. and then i can change it into a char. My full theory is below.

First i stored the hexidecimal string into a character array. Then i created the ptrBuffer pointer to point to the buffer array. The for loop then cycles through the array holding the hex string and stores the first char int val1, second into val2... and just repeats since the for loop will be looping in intervals of 2. then I can easily set val1 to equal the first element of the char array, and val2 to equal the second (and so on as the loop continues.) Then i just wanted to take those two numbers that have been stored and convert it to a decimal, which i can then store the decimal number into newval, and change it into a char to give me a actual letter/character. Then i just set *ptrBuffer equal to the character, and increment it. The case statement was my potential way to handle the letters A-F in the hex form. For example on the second cycle of the for loop val1 and val2 is suppose to be set so that...

val1 = 4
val2 = F

after the math is done 4F will be in decimal form, then that will be changed to a char, eventually creating a array of chars.. and then outputting the message.

my case statement is suppose to change the letter F so it equals the appropriate value.

This post has been edited by woppix: 02 October 2010 - 06:24 PM

### #6 CTphpnwb

• D.I.C Lover

Reputation: 3782
• Posts: 13,709
• Joined: 08-August 08

## Re: Convert Hex to ASCII

Posted 02 October 2010 - 06:21 PM

GOD Bless America!
```#include <iostream>

using namespace std;

char buffer[100];
char RecvData[64] = "474F4420426C65737320416D657269636121";

int main()
{

char *ptrBuffer = &buffer[0];

for (int i = 0; i < sizeof(RecvData); i+=2)
{
int	firstvalue = RecvData[i] - '0';
int	secondvalue;
//if RecvData[i+1] is a letter convert it to integer, otherwise use it.
switch(RecvData[i+1])
{
case 'A':
{
secondvalue = 10;

}break;
case 'B':
{
secondvalue = 11;

}break;
case 'C':
{
secondvalue = 12;

}break;
case 'D':
{
secondvalue = 13;

}break;
case 'E':
{
secondvalue = 14;

}break;
case 'F':
{
secondvalue = 15;

}break;
default:
secondvalue = RecvData[i+1] - '0';
break;
}

int newval;

//convert the two values into decimal form
newval =  16 * firstvalue + secondvalue;

//change newval into a character
*ptrBuffer = char(newval);

//increment ptrBuffer
cout << ptrBuffer;

ptrBuffer++;
}
cout << endl;
return 0;
}

```

This post has been edited by CTphpnwb: 02 October 2010 - 06:21 PM

### #7 woppix

Reputation: 2
• Posts: 127
• Joined: 13-September 09

## Re: Convert Hex to ASCII

Posted 02 October 2010 - 07:09 PM

thank you! I just have one more question that has to do with this general thing. Like i said above this program was part of a bigger one, when i added it back into that code the end result of the conversion needs to be sent to a listbox and no longer to a console. I have been using the below function to do this, but i'm having issues making a array of characters print to the listbox. I used the same function to display the char array of the original hex string to the listbox.... but it doesn't seem to work in this case.

i used this to display the original hex string that was sent from my server to the client. The char array is szIncoming.
```vShowText(hlistbox, szIncoming);

```

the function to print to the listbox
```void vShowText(HWND	hChildHandle, char *szText)
{
int Line;

// add string to the listbox

// determine number of items in listbox
Line = SendMessage(hChildHandle,LB_GETCOUNT,0,0);

// flag last item as the selected item, to scroll listbox down
SendMessage(hChildHandle,LB_SETCURSEL,Line-1,0);

// unflag all items to eliminate negative highlite
SendMessage(hChildHandle,LB_SETCURSEL,-1,0);
}

```

and here is the actual translate code with the few changes that was needed in order to work with the other program.

```case MENU_FILE_TRANS:
{
char *ptrBuffer = &buffer[0];

for (int i = 0; i < RecvBytes; i+=2)
{
int firstvalue = RecvData[i] - '0';
int secondvalue;
//if RecvData[i+1] is a letter convert it to integer, otherwise use it.
switch(RecvData[i+1])
{
case 'A':
{
secondvalue = 10;

}break;
case 'B':
{
secondvalue = 11;

}break;
case 'C':
{
secondvalue = 12;

}break;
case 'D':
{
secondvalue = 13;

}break;
case 'E':
{
secondvalue = 14;

}break;
case 'F':
{
secondvalue = 15;

}break;
default:
secondvalue = RecvData[i+1] - '0';
break;
}

int newval;

//convert the two values into decimal form
newval =  16 * firstvalue + secondvalue;

//change newval into a character
*ptrBuffer = char(newval);

//this isn't displaying the translated message
ptrBuffer++;
}

vShowText(hlistbox, ptrBuffer);

}break;

```

This post has been edited by woppix: 03 October 2010 - 05:59 AM

### #8 CTphpnwb

• D.I.C Lover

Reputation: 3782
• Posts: 13,709
• Joined: 08-August 08

## Re: Convert Hex to ASCII

Posted 02 October 2010 - 08:03 PM

It doesn't make sense to me that you're passing an array to vShowText but doing it one character at a time. I'd do that after the loop. I'd also declare newval at the top of the function, so it only gets declared once.

### #9 woppix

Reputation: 2
• Posts: 127
• Joined: 13-September 09

## Re: Convert Hex to ASCII

Posted 02 October 2010 - 09:49 PM

yeah i really don't get this right now... i can make a char array and fill it with something then use vShowText(hlistbox, myArray), and it will display the contents of the array without any problems. When i try to do the same thing but with ptrBuffer, it doesn't actually show the contents of the array, it just adds a line to the listbox. If i use the buffer array i get strange D'ish looking characters.

This post has been edited by woppix: 02 October 2010 - 09:56 PM

### #10 baavgai

• Dreaming Coder

Reputation: 7160
• Posts: 14,924
• Joined: 16-October 07

## Re: Convert Hex to ASCII

Posted 03 October 2010 - 01:39 AM

Functions are your friends. Use them!

e.g.
```#include <iostream>
#include <cstring>

using namespace std;

int getDigitValue(char digit) {
int n = digit-'0';
if (n>=0 && n<10) { return n; }
if (digit=='A') { return 10; }
if (digit=='B') { return 11; }
if (digit=='C') { return 12; }
if (digit=='D') { return 13; }
if (digit=='E') { return 14; }
if (digit=='F') { return 15; }
return -1;
}

int getPairValue(char digit1, char digit2) {
int v1 = getDigitValue(digit1);
int v2 = getDigitValue(digit2);
return (v1==-1 || v2==-1) ? -1 : v1 * 16 + v2;
}

bool hexStrToStr(char *dst, const char *src, int size) {
bool isGood = false;
if (size % 2 == 0) {
isGood = true;
for (int i=0; i<size; i+=2) {
int pv = getPairValue(src[i], src[i+1]);
if (pv==-1) { isGood = false; break; }
*dst = pv;
dst++;
}
}
*dst = 0;
return isGood;
}

bool hexStrToStr(char *dst, const char *src) { return hexStrToStr(dst, src, strlen(src)); }

int main() {
char buffer[100];
char RecvData[64] = "474F4420426C65737320416D657269636121";

cout << RecvData << endl;
cout << "isGood = " << hexStrToStr(buffer, RecvData) << endl;
cout << buffer << endl;

cout << "isGood = " << hexStrToStr(buffer, RecvData, 18) << endl;
cout << buffer << endl;

return 0;
}

```

```case MENU_FILE_TRANS: {
if (!hexStrToStr(buffer, RecvData, RecvBytes)) {
strcpy(buffer, "Message Data invalid");
}
vShowText(hlistbox, buffer);
}

```

### #11 woppix

Reputation: 2
• Posts: 127
• Joined: 13-September 09

## Re: Convert Hex to ASCII

Posted 03 October 2010 - 05:33 AM

thank you, and i may end up using that or something similar. However, i still would like to see what to do to make the original code work.. since it was the one i did mostly myself. Just don't understand why the vShowText function isn't working in this case, but works to show contents of other arrays. The code to do the translating works, just haven't figured out why it will not output to the listbox.

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Convert Hex to ASCII

Posted 03 October 2010 - 05:39 AM

```but it doesn't seem to work in this case.
```

What kind of help do you expect with such a poor "bug report"??? How does it "not work"???

### #13 woppix

Reputation: 2
• Posts: 127
• Joined: 13-September 09

## Re: Convert Hex to ASCII

Posted 03 October 2010 - 05:51 AM

sorry, i explained it more in some of the above posts. Pretty much when the vShowText function is called like so.. vShowText(hlistbox, ptrBuffer), it tried to add to the listbox.. but it only puts a new item or blank line into the listbox and not actually the characters that are stored in the array. When i put the code into a console application i just used cout << ptrBuffer to display the contents, and everything worked fine so i know the actual translation code is working. I can't think of anything else to tell as far as bug reporting goes, if you need to know something else please let me know. As for the actual code, its in one of my long posts above.

This post has been edited by woppix: 03 October 2010 - 05:58 AM

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Convert Hex to ASCII

Posted 03 October 2010 - 06:01 AM

You are calling vShowText with every iteration of the loop with the address of the character to which ptrBuffer is pointing. When you reach the end of the string, what is the last character? That's what's likely displaying.

### #15 woppix

Reputation: 2
• Posts: 127
• Joined: 13-September 09

## Re: Convert Hex to ASCII

Posted 03 October 2010 - 07:35 AM

the hex string says "GOD Bless America!" so the last character in it is "!". Nothing at all from the array is being shown in the listbox. I also moved vShowText out of the loop, i don't want to display individual characters.. but the whole char array at a time. Also like i said in one of the posts before... i can do something like the below code and it works fine.

//create a char array that holds hello world
char myArray[100] = "Hello World!";

//later in the program
vShowText(hlistbox, myArray);

this will print out Hello World! into the listbox with no problems.

This post has been edited by woppix: 03 October 2010 - 07:41 AM