Help with char to int and vice versa func

so i need a function that gets a char and puts out a number
ex.
a=0
b=1
and so on.
i have looked into ascii codes,but a is 97,and it is necessary for it to be 0.if i just take 97 97 out of the ascii number,it gives errors for spaces and such
the way i have it now is just one big switch statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  int  ToChar(int x){

           switch(x){
           case 0:
           return 'a';
           break;
           case 1:
           return 'b';
           break;
            case 2:
           return 'c';
           break;
            case 3:
               return 'd';
           break;
            case 4:
           return 'e';
           break;
            case 5:
           return 'f';
           break;
            case 6:
           return 'g';
           break;
              case 7:
           return 'h';
           break;
            case 8:
           return 'i';
           break;
            case 9:
           return 'j';
           break;
            case 10:
           return 'k';
           break;
            case 11:
           return 'l';
           break;
            case 12:
               return 'm';
           break;
            case 13:
           return 'n';
           break;
            case 14:
           return 'o';
           break;
            case 15:
           return 'p';
           break;
            case 16:
           return 'q';
           break;
            case 17:
           return 'r';
           break;
            case 18:
           return 's';
           break;
            case 19:
           return 't';
           break;
            case 20:
           return 'u';
           break;
            case 21:
           return 'v';
           break;
            case 22:
           return 'w';
           break;
            case 23:
           return 'x';
           break;
            case 24:
           return 'y';
           break;
            case 25:
           return 'z';
           break;
            default:
               break;

           }
           return 0;
            }

current func
Last edited on
It returns the ASCII because the argument is an integer and int can't hold a letter only the integers corresponding to that letter according to ASCII.
(if that was your question)
Last edited on
no that isn't my problem.
that was just some idea i had,if i get the ascii number it is easy to get the letter.
my question is if there is a better way to code the function you see above,cause it is ugly imo,and i have no ideas left on how to make it better.
in the C family, char is equivalent to int - one displays the character representation of the int, the other the int itself.

SO

see what happens if you do this:

1
2
3
4
5
char character;
int ascii;
character = 'a';
ascii = character;
cout << character << " = "  ascii <<  " in ascii" << endl;


or this:

1
2
3
4
char character;

character = 0 + 97;
cout << "Zero + 97 returned as char =" << character << endl ;


So if you want your function to return the ascii character for an integer:

add 97 to the int, return it as char

i.e.:

1
2
char  ToChar(int x)
{ return x + 97 }


1
2
int ToInt(char x)
{ return x - 97} // assuming always enter only alpha lower case 


Last edited on
The techniques being discussed only work if using a character set based on ASCII. This is admittedly most of them. However, it will not work with EBCDIC. I would put a comment in the function headers which indicates that it is not portable code.

As PCrumley48 mentions, this only works with lowercase. What do you want or need to happen with 'A'?

by not portable,do you mean to other operating systems or what?
would my original function be portable,compared to this one(which i am now using?).
also upper case is converted to lowercase and then passed through the function,numbers and special characters are converted to 'x'.
i know how to do that though so thanks to all of you :)
Last edited on
by not portable,do you mean to other operating systems or what?
Yes, I mean a computer where the operating system is not using an ASCII based character set. That probably means IBM mainframes which use (or did use) EBCDIC characters.

would my original function be portable,compared to this one(which i am now using?).
Yes, rereading your original, it seems to be portable.

But the fundamental question for many projects, is portability a goal of the project?
Topic archived. No new replies allowed.