char to int ASCII hex

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

char to int ASCII hex

跳至解决方案
6,464 次查看
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
标签 (1)
标记 (1)
0 项奖励
回复
1 解答
2,681 次查看
kef
Specialist I

   val.a[] = "AE";

 

 

I think you meant here

 

   strcpy(val.a, "AE");

 

or

 

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

 

在原帖中查看解决方案

0 项奖励
回复
7 回复数
2,681 次查看
roberto_m
Contributor III

Thank you!

It is very easy

0 项奖励
回复
2,681 次查看
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 项奖励
回复
2,681 次查看
J2MEJediMaster
Specialist I

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

 

---Tom

0 项奖励
回复
2,681 次查看
Seitec
Contributor III
Thank you very much.Great thank you :smileyhappy:.
0 项奖励
回复
2,681 次查看
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 项奖励
回复
2,681 次查看
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 项奖励
回复
2,682 次查看
kef
Specialist I

   val.a[] = "AE";

 

 

I think you meant here

 

   strcpy(val.a, "AE");

 

or

 

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

 

0 项奖励
回复