Solved

# Hex to Ascii & Viceversa

Posted on 2001-08-27
309 Views
Can any one help for converting Hexa value to ascii and viceversa.

Govar
0
Question by:govar

Expert Comment

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

LVL 4

Expert Comment

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

LVL 4

Expert Comment

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

LVL 4

Expert Comment

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

-=- James.
0

LVL 30

Expert Comment

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 Comment

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

LVL 4

Accepted Solution

jtwine100697 earned 50 total points
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 Comment

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

LVL 4

Expert Comment

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 Comment

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

LVL 4

Expert Comment

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

LVL 11

Expert Comment

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

## Featured Post

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.