Solved

Hex to Ascii & Viceversa

Posted on 2001-08-27
12
309 Views
Last Modified: 2010-05-18
Can any one help for converting Hexa value to ascii and viceversa.

Thanks in advance
Govar
0
Comment
Question by:govar
12 Comments
 

Expert Comment

by:mkprasad
Comment Utility
    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

by:jtwine100697
Comment Utility
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

by:AssafLavie
Comment Utility
mkprasad, why not simply:
 
    int i = 0x6689;
    cout << hex << i;
??
0
 
LVL 4

Expert Comment

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

-=- James.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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

by:govar
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Accepted Solution

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

by:govar
Comment Utility
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

by:jtwine100697
Comment Utility
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

by:govar
Comment Utility
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:jtwine100697
Comment Utility
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

by:griessh
Comment Utility
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.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now