LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

convert number to letter of alphabet?

Solved!
Go to solution

Hi,

 

Is there a LabWindows function for converting a number to an alphabet equivalent?

 

I was trying something like this...

char getLetterOfAlphabetByNum(int number){
    //test if alpha
    if(isalpha (number)==0)
        DebugPrintf ("Error converting number to letter. Non-alphabetic.");
    else{
        //take decimal and convert to ascii
        statusReturned=Fmt (&letter, "%s<%d", number);
        if(statusReturned<0)
            DebugPrintf ("Error converting number to letter!\n");
        DebugPrintf ("tempString = %s\n", letter);
    }
    DebugPrintf ("%d --> %s\n",number,letter);
return letter;
}

 

Thanks!

0 Kudos
Message 1 of 6
(6,746 Views)

Supposing you want to obtain the ASCII character corresponding to a given int number, you could use

 

sprintf (string, "%c", number);

You will need to validate the input number to be <= 255, next to see if the character is printable (which not necessarily means it's alphabetic: you must consider numbers, punctuation and other printable characters like ([{*@# and so on: isprintf and isgraph functions can be of help)



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 6
(6,745 Views)
Solution
Accepted by topic author TurboMetrologist

Thanks, this was my solution. Passing a 4 gives a 'D', for example.


//number of alphabet to letter
char getLetterOfAlphabetByNum(int number){
    number=number+64;
    //test if alpha and printable
    if(isgraph(number)==0){
        DebugPrintf ("Non-printable character.\n");
        return;
    }
    if(isalpha(number)==0){              
        DebugPrintf ("Non-alphabetic.\n");
        return;
    }
    sprintf (&tempString[0], "%c", number);
    letter=tempString[0];
    DebugPrintf ("%d --> %c\n",number,letter);
return letter;
}

0 Kudos
Message 3 of 6
(6,736 Views)

It seems to me that you could simplify your code this way:

 

//number of alphabet to letter
char getLetterOfAlphabetByNum (int number)
{
    number += 64;
    //test if alpha
    if (!isalpha (number)) {              
        DebugPrintf ("Non-alphabetic.\n");
        return 0;
    }
    return (char)number;
}

 

I excluded the test for printable characters as is it overcome by test for alphabetic.

Also, be warned that passing numbers from 33 to 58 returns lowercase letters.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 6
(6,704 Views)

Thanks Roberto

0 Kudos
Message 5 of 6
(6,647 Views)

You can do this, too:

 

number += 'A';

-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 6 of 6
(6,610 Views)