char to int ASCII hex

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

char to int ASCII hex

Jump to solution
5,340 Views
Seitec
Contributor III
Hello, I would like ask. I need convert e.g. char "AE" to unsigned short int 0x4145? do any body pleas know how I can convert this? Thank you very much.
Message Edited by Seitec on 2009-03-13 10:53 PM
Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
1,557 Views
kef
Specialist I

   val.a[] = "AE";

 

 

I think you meant here

 

   strcpy(val.a, "AE");

 

or

 

   memcpy(val.a, "AE", 2);

 

View solution in original post

0 Kudos
Reply
7 Replies
1,557 Views
roberto_m
Contributor III

Thank you!

It is very easy

0 Kudos
Reply
1,557 Views
roberto_m
Contributor III

And how can I convert a counter (stored in an unsigned char)  to a string?

I mean:

 

0xFF >> 255 >> 32:35:35 (ASCII values)

0 Kudos
Reply
1,557 Views
J2MEJediMaster
Specialist I

Check out FAQ-27705 for source code that converts a binary value to a text string.

 

---Tom

0 Kudos
Reply
1,557 Views
Seitec
Contributor III
Thank you very much.Great thank you :smileyhappy:.
0 Kudos
Reply
1,557 Views
bigmac
Specialist III

Sorry about that. I was somewhat confused with the initialisation process allowed within the definition of an array variable.

 

Another alternative to the use of the copy functions might be to load each of the two array elements separately -

 

val.a[0] = 'A';

val.a[1] = 'E';

 

or perhaps create a function -

 

unsigned short getval( char *s)
{
ascseq temp;

temp.a[0] = s[0];
temp.a[1] = s[1];
return temp.b;
}


with the following usage -


unsigned short i;

 

i = getval( "AE" );

 

 

Regards,

Mac

Message Edited by bigmac on 2009-03-15 05:04 AM
0 Kudos
Reply
1,557 Views
bigmac
Specialist III

Hello,

 

Assuming big endian data format, simply create a union between a char array and an unsigned short variable.

 

typedef union {

    char a[3];  // Allow for null termination

    unsigned short b;

} ascseq;

 

ascseq val;     // Define variable

 

To load data to the array -

    val.a[] = "AE";

 

To read the equivalent integer value, use val.b

 

Regards,

Mac

0 Kudos
Reply
1,558 Views
kef
Specialist I

   val.a[] = "AE";

 

 

I think you meant here

 

   strcpy(val.a, "AE");

 

or

 

   memcpy(val.a, "AE", 2);

 

0 Kudos
Reply