Solved

Hex to Ascii & Viceversa

Posted on 2001-08-27
12
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 

Expert Comment

by:mkprasad
ID: 6428939
    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
ID: 6429290
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
ID: 6429713
mkprasad, why not simply:
 
    int i = 0x6689;
    cout << hex << i;
??
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

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

-=- James.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6431996
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
ID: 6435132
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

by:
jtwine100697 earned 50 total points
ID: 6436585
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
ID: 6436793
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
ID: 6436920
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
ID: 6439242
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
ID: 6440466
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
ID: 6862357
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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