[英]How do I interpret ASCII values of characters of a string as chars in C?
I have a sequence of numbers representing ASCII values of alphabet characters [A,Z], [a,z] and " ".我有一个数字序列,表示字母字符 [A,Z]、[a,z] 和“”的 ASCII 值。 I can get in a char the ascii value of one of the values (ie char = 76), but when I try to make the boundaries checking and the appending to the result I can't seem to get "L" instead of 76 and cant compare the char value to the ASCII values of the boundaries (ie A = 65).我可以在字符中获取其中一个值的 ascii 值(即 char = 76),但是当我尝试进行边界检查并附加到结果时,我似乎无法得到“L”而不是 76 和无法将 char 值与边界的 ASCII 值进行比较(即 A = 65)。 How do I do this in C?我如何在 C 中做到这一点?
//input would be something like char* source = "76111114..."
// result should be "Lor"
for(int i = 0; i < strLen; i+=2)
{
char actualChar = ' ';
//get two numbers as a character
strncpy(&actualChar, source + i, 2);
//this prints out 76
printf("%s\n", &actualChar);
//this prints out 55
printf("%d\n", actualChar);
//check if the character is a space
if(actualChar == 32)
{
//append
strncat(result, &actualChar, 2);
printf("Added space : %s\n", result);
continue;
}
//[A, Z]
else if(actualChar >= 65 && actualChar <= 90)
{
//append
strncat(result, &actualChar, 2);
printf("Added 2 digit char: %s, %s\n", &actualChar ,result);
continue;
}
else
{
//add one number to the char and check again
strncpy(&actualChar, source + i, 3);
//[a, z]
if(actualChar >= 97 && actualChar <= 122)
{
++i;
//append
strncat(result, &actualChar, 3);
printf("Added 3 digit char: %s, %s\n", &actualChar ,result);
continue;
}
//not a valid char
else
{
printf("Not valid char: %s", &actualChar);
return 0;
}
}
}
I would like result to be "Lor" but instead I am getting "76111114".我希望结果是“Lor”,但我得到的是“76111114”。
如评论中所述, sscanf(source, "%2d", &actualChar) and sscanf(source, "%3d", &actualChar)
是获得它的最简单方法,尽管我必须将 actualChar 转换为int*
用于 sscanf去工作:
sscanf(source+ i, "%2d", (int*)(&actualChar));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.