02-21-2011 10:39 AM
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!
Solved! Go to Solution.
02-21-2011 10:50 AM - edited 02-21-2011 10:51 AM
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)
02-21-2011 11:27 AM
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;
}
02-22-2011 05:22 AM
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.
02-25-2011 07:42 AM
Thanks Roberto
02-28-2011 02:40 AM
You can do this, too:
number += 'A';