简体   繁体   English

将整数转换为字符,反之亦然

[英]Convert integer to character and vice versa

How to convert integer to char and vice versa in "Dynamic C". 如何在“Dynamic C”中将整数转换为char,反之亦然。

Use VB.NET as bellow: 使用VB.NET如下:

Dim i As Integer
Dim c As Char

' Integer to Character
i = 302
c = ChrW(302)
Debug.Print(c)  'Result: Į

' Character to Integer
Dim j As Integer
j = AscW(c)
Debug.Print(CStr(j))  ' Result: 302

Thanks 谢谢

Since both int and char are integer types, you can simply assign an appropriately-valued integer to a char and vice versa: 由于int和char都是整数类型,因此您只需将适当值的整数分配给char,反之亦然:

int i = 65; // 'A'
char c = 'B'; // 66;
int cAsInt = (int)c; // 66 = 'B'
char iAsChar = (char)i; // 'A' = "65"

If you want to parse a character such that '1' becomes the integer 1, you can use itoa and atoi . 如果要解析一个字符,使'1'成为整数1,则可以使用itoaatoi

If you want to convert between the the ascii values and their characters, that's even easier. 如果你想在ascii值和它们的字符之间进行转换,那就更容易了。 Simply cast the int to a char or the char to an int. 只需将int转换为char或将char转换为int。

Why dont you use an other type like uint16_t that can be used for UCS2 ? 为什么不使用可用于UCS2的其他类型的uint16_t? I mean char is used for ascii and extended 0-255 ~ uint8_t, if you need more dont use char. 我的意思是char用于ascii并扩展0-255~uint8_t,如果你需要更多不使用char。

uint16_t c=302;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM