char to int ASCII hex

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

char to int ASCII hex

ソリューションへジャンプ
6,487件の閲覧回数
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,704件の閲覧回数
kef
Specialist I

   val.a[] = "AE";

 

 

I think you meant here

 

   strcpy(val.a, "AE");

 

or

 

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

 

元の投稿で解決策を見る

0 件の賞賛
返信
7 返答(返信)
2,704件の閲覧回数
roberto_m
Contributor III

Thank you!

It is very easy

0 件の賞賛
返信
2,704件の閲覧回数
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,704件の閲覧回数
J2MEJediMaster
Specialist I

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

 

---Tom

0 件の賞賛
返信
2,704件の閲覧回数
Seitec
Contributor III
Thank you very much.Great thank you :smileyhappy:.
0 件の賞賛
返信
2,704件の閲覧回数
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,704件の閲覧回数
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,705件の閲覧回数
kef
Specialist I

   val.a[] = "AE";

 

 

I think you meant here

 

   strcpy(val.a, "AE");

 

or

 

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

 

0 件の賞賛
返信