473,230 Members | 1,811 Online

# How to convert alphabet to numbers?

im newbie here.. n need some help..

alphabets got 26 character n each character got secret number..

example: a = 01
Feb 10 '08 #1
12 86642
Laharl
849 Expert 512MB
Characters are already directly convertible to integers via the ASCII standard, http://asciitable.com/ has the conversions. To quickly get a->1 and such, subtract 'a' from a given character and add 1.

You can also use an array of all the characters, which would make a->00 and z->25, since arrays are zero-indexed. To get a->01, just pad the array with some control character that won't be used, maybe a number.

If you're after some sort of substitution encryption, either of these solutions will be fairly easy to work with, as the latter can easily be declared in global scope and the former allows direct manipulation.

Expand|Select|Wrap|Line Numbers
1. const char* alpha = "abcdefghilmnopqrstuvwxyz"; //Zero-indexed array of letters
2.
3. char b = 'b'-'a'+1; //b=2, an unprintable character
4. int c = b; //b=2
5.
6.
Feb 10 '08 #2
i know about ASCII standard but can u teach me how to apply ASCII code in turbo c.

if a=01,b=02,c=03

when i type "abc" the answer should be 010203

i almost giv up doing this.. huhuh...
Feb 10 '08 #3
Laharl
849 Expert 512MB
If you want a direct way to link a->01, use a std::map<char, int>. That might fit your needs a little better, as it allows you to easily change the number a given character is mapped to.
Feb 10 '08 #4
If you know the ASCII standard then you know that 'a' has value 65. Now if you want to print out your code for 'a' as 01 then you have to subtract your character first by 65 and then add 1. effectively code of 'b' is ASCII(b) - ASCII(a) +1,
ASCII(b) = 66
ASCII(a) = 65

Therefore ASCII(b) - ASCII(a) +1 = 2.

Next you get to coding. The following snippet considers that the value for the character is only between 'a' and 'z' and this can be easily extended

Expand|Select|Wrap|Line Numbers
1.     char arr[255];
2.     int valArr[255],index;
3.     printf("Enter string: ");
4.     scanf("%s",arr);
5.     for(index=0;index<strlen(arr);++index){
6.         if(arr[index]>='a' && arr[index]<='z'){
7.             valArr[index] = arr[index]-'a'+1;
8.         }
9.     }
10.     for(index=0;index<strlen(arr);++index){
11.         printf("%02d",valArr[index]);
12.     }
13.
Feb 11 '08 #5
when i did space.. the answer was wrong.. but thanks bro... its help me soo much..

example: when i enter the string a b c it should show me 01 02 03
Feb 26 '08 #6
a is 97 not 65, A is 65.

And if you just want the ascii value then just cast it to an int.
for example;

char test='a';
cout<<test<<endl;
cout<<(int)test;

Has as output
a
97

Here's an ascii table: http://www.asciitable.com/

getting the values as you want them is easy if it's all lower letters or upper cases.

If it's all lower cases deduct 96, upper case -64
Feb 26 '08 #7
Banfa
9,065 Expert Mod 8TB
Characters are already directly convertible to integers via the ASCII standard, http://asciitable.com/ has the conversions. To quickly get a->1 and such, subtract 'a' from a given character and add 1.
Note this is only true if the execution character set happens to be ASCII (or another character set where the letters are contiguous). The C/C++ standards do not specify either the compilation or execution character sets which can be anything and there are some character sets where the letters are not contiguous.

The C/C++ standards do specify that whatever character set is in use the character '0' - '9' must be contiguous, e.g. '5' - '0' == 5 is guaranteed by the standard for a conforming implementation of the compiler.

Having said all that many (most?) compilers today do use the ASCII character set and many people do use this sort of character mathematics.

However in doing so you should be aware that you have introduced a possible portability issue into your code.

Here is an example of a character set that used to be used in compilers/computers a far bit but does not have contiguous letters.
Feb 26 '08 #8
Expand|Select|Wrap|Line Numbers
1. #include <stdio.h>
2. #include <ctype.h>
3.
4. main() {
5.
6.           char arr[255];
7.
8.           int valArr[255],index;
9.
10.       clrscr();
11.
12.           printf("Enter string: ");
13.
14.           gets(arr);
15.
16.           for(index=0;index<strlen(arr);++index){
17.
18.             if(arr[index]>='a' && arr[index]<='z'){
19.
20.                   valArr[index] = arr[index]-'a'+1;
21.
22.               }
23.         else
24.         {
25.             if (isspace(arr[index]))
26.             {
27.                 valArr[index] = '\0';
28.             }
29.         }
30.           }
31.
32.           for(index=0;index<strlen(arr);++index){
33.
34.               printf("%02d",valArr[index]);
35.
36.           }
37. getch();
38. }
39.
when i change "scanf("%s",arr);" to "gets(arr)" and
i get the result:

Enter String: a bc
Output: 01000203

can i get the result as 01 0203?
Mar 11 '08 #9
Expand|Select|Wrap|Line Numbers
1. int a[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
2. #include<stdio.h>
3. main()
4. {
5. int i,j,size;
6. char srt[100];
7. clrscr();
8. printf("enter your string size");
9. scanf("%d",&size);
10. printf("enter your name or a string");
11. for(i=0;i<=size;i++)
12. {
13. scanf("%c",&str[i]);
14. }
15.             for(i=0;i<=size;i++)
16.              {
17.               for(j=0;j<26;j++)
18.                 {
19.                  if(str[i]==a[j])
20.                  {
21.                   printf("\t%d",j+1);
22.                   }
23.                  }
24.               }
25. }
26.
27. \* This program defnetly works.
28.     o/p: enter your string size : 50
29.          enter your name or a string : john abraham
30.          10 15 8 14 1 2 18 1 8 1 13
Aug 4 '14 #10
did my programs works..?
does that gives u exact result what u want..?
Sep 23 '14 #11
zmbd
5,501 Expert Mod 4TB
pridephani:
You posted to a long dead thread... it's over 6 years old. Either the poster has already solved the problem or isn't around any more to care.
Oct 6 '14 #12
It is helpful but there is some problem in your explanation . The ascii code of 'a' is 97
Nov 7 '17 #13