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".
CodePudding user response:
As mentioned in the comments, sscanf(source, "-", &actualChar) and sscanf(source, "=", &actualChar)
is the easiest way to get it, although I had to cast actualChar it to int*
for the sscanf to work:
sscanf(source i, "-", (int*)(&actualChar));