如何将字符串字符的ASCII值解释为C中的字符?
我有一系列代表字母字符[a,z],[a,z]和“” ASCII值的数字序列。我可以在char中获得一个值之一的ASCII值(即char = 76),但是当我尝试进行界限检查并将其附加到结果时,我似乎无法获得“ L”而不是76和76和不能将CHAR值与边界的ASCII值进行比较(即A = 65)。我该如何在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;
}
}
}
我希望结果是“ lor”,但我得到的是“ 7611114”。
I have a sequence of numbers representing ASCII values of alphabet characters [A,Z], [a,z] and " ". I can get in a char the ascii value of one of the values (i.e. 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 (i.e A = 65). How do I do this in 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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如评论中提到的,
sscanf(源,“%2D”,&amp; muthChar)和sscanf(来源,“%3D”,&amp; natualChar)
是获得它的最简单方法,尽管我必须将其施放为int*
才能使SSCANF工作:sscanf(source+ i,“%2D”,(int*),(&amp; amp; muthatch));
>As mentioned in the comments,
sscanf(source, "%2d", &actualChar) and sscanf(source, "%3d", &actualChar)
is the easiest way to get it, although I had to cast actualChar it toint*
for the sscanf to work:sscanf(source+ i, "%2d", (int*)(&actualChar));