• Status: Solved
• Priority: Medium
• Security: Public
• Views: 332

# Hex to Ascii & Viceversa

Can any one help for converting Hexa value to ascii and viceversa.

Govar
0
govar
1 Solution

Commented:
int i = 0x6689;
char buf[10]="";
sprintf(buf,"%0X",i);//_itoa(i,buf,16);
cout << buf << endl;
cout << atoi(buf) << endl;
0

Commented:
Values of integers do not matter regardless of how they are represented in code.  For example, the following are all identical:

int     iValue1 = 0xFF;
int     iValue2 = 255;
int     iValue3 = 0377;

All of the variables will have a numeric value of 255.  Are you asking how to change from an internal numeric value to text (ASCII), or from one textual representation to another (like changing an input string of "0xFF" to "255" or vice-versa)?

-=- James.
0

Commented:

int i = 0x6689;
cout << hex << i;
??
0

Commented:
Why not know exactly what the poster wants first, before "solutions" are proposed? :)

-=- James.
0

Commented:
The following is example code using STL only.

#include <string>
#include <sstream>
#include <iostream>

int AsciiHexToInt(std::string szData)
{
std::stringstream ss(szData);
int dest = 0;
ss >> std::hex >> dest;
return dest;
}

std::string IntToHexString(int nData)
{
std::stringstream ss;
ss << std::hex << nData;
return ss.str();
}

int main(int argc, char* argv[])
{
int MyNum1 = 0xaf;
int MyNum2 = 123;

std::string num1 = IntToHexString(MyNum1);
std::string num2 = IntToHexString(MyNum2);

int ReturnValueNum1 = AsciiHexToInt(num1);
int ReturnValueNum2 = AsciiHexToInt(num2);

std::cout << std::hex << ReturnValueNum1 << std::endl;
std::cout << std::hex << ReturnValueNum2 << std::endl;

return 0;
}
0

Author Commented:
To be more clear on my question.
I have an array say
unsigned int array1[5]="\xFF\x2F\x05\\x11\x04";
I want to convert this to ascii and then use atol function to ADD or SUBTRACT with another Hex Array, and then convert back to Hex .

0

Commented:
The ::_itoa(...) function will convert those integer values to strings for you.  For example:

::_itoa( array1[ 0 ], caCharBuffer, 10 );  // Will Give You "255" In caCharBuffer
::_itoa( array1[ 1 ], caCharBuffer, 10 );  // Will Give You "47" In caCharBuffer
//
// Etc...
//

Do you NEED to convert the int arrays to text before doing math on them?  (BTW: do not call them "hex arrays", that is only confusing things: no matter how the values for the array are formatted, it is still an array on "int"s, and its internal format is the same, no matter what.)

If you just need to get the value of each element of one array added/substracted from a matching element in another:

int  iValue1 = ( array1[ 0 ] - array2[ 0 ] );
int  iValue2 = ( array1[ 1 ] + array2[ 1 ] );
//
// Etc...
//

> I want to convert this to ascii and then use atol function to ADD or SUBTRACT with another Hex Array,
> and then convert back to Hex .

You need to be a bit clearer, after you clean up your use of "hex".

-=- James.
0

Author Commented:
I have used casting operation on the hex array

unsigned int array1[5]="\xFF\x2F\x05\x11\x04";
char array2[10];

unsigned long newarray1= (unsigned long )* ( long *)array1;

sprintf(array2,"%10ld",newarray1);

it is working fine.

array2 is my ascii value

I do know exactly how it works. But some one told me to do the conversion.

Thanks
Gova
0

Commented:
That is rather strange syntax, IMHO!!!  It looks as if you are treating the first 4 bytes in array1 as a single 32-bit value (assuming a long is 32-bits on your platform).

Actually, now that I think about it, I am surprised that the initialization of array1 compiles at all!  What compiler are you using?

I would be very interested in meeting the person that told you to do the conversion and the initialization in that way!

-=- James.
0

Author Commented:
It was a mistake in declaration and initialisation above

unsigned char array1[5];

array1[0]=0xff;
array1[1]=0x2f;
array1[2]=0x01
array1[3]=0x30;
array1[4]=0x3c;
0

Commented:
By the way it is being used in the sprintf(...) it still seems like you are treating the value as a single 32-bit value.  That looks weird to me.

-=- James.
0

Commented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "jtwine" comment(s) as an answer.

The link to the Community Support area is: http://www.experts-exchange.com/commspt

======
Werner
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.