How do I interpret ASCII values of characters of a string as chars in C?

user18743773

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".

user18743773

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 to int* for the sscanf to work:

sscanf(source+ i, "%2d", (int*)(&actualChar));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I "interpret" escaped characters in a string?

How do I collect chars into a string in C?

How do I replace "fancy" unicode chars with their ascii equivalends?

How do I convert a list of ascii values to a string in python?

How do I convert a string to a list of chars?

How do i remove chars from a string?

How can i turn Turkish chars to ascii?

How do I remove copyright and other non-ASCII characters from my Java string?

How do I generate a string of random ASCII printable characters with a specific size in Rust?

How can I do it faster in java? Copy chars of a long string

How do I convert a list of chars to a string in purescript

MySQL: How do I search and replace chars at the beginning of a string

How do I json_decode string with special chars (" \\ " )

How do I concat a string of chars together from a matrix in python?

How do I remove some chars at the end of a string?

How do I grab the last portion of a log string and interpret it as json?

How do I check if a string is unicode or ascii?

How do I convert from ASCII to String

How can I sanitize a string so that it only contains only printable ASCII chars?

How Do I convert bytes to ASCII chars, using Oracle PL/SQL

How do I remove the first n characters from a string in c?

How do I return a string with variables and characters in c++?

How do I remove multiple characters from a string in C?

How do C++ compiler interpret comparison logic in string/character?

How do I interpret this instruction?

How do i interpret this timestamp

in C# how do I convert what I think is a string of hex ascii to something i can read?

How can I split a string with the following chars \ ? C#

How can I get the ascii characters from a string in Erlang