S
Sona
Hi,
I have a char* that holds an ascii character in its first element (at
least I think that's what it holds because when I print it, it prints
weird characters). I need to convert this into an integer and so I
thought the following might work:
char *somevar ... // somevar holds a value in somevar[0] and is null
terminated i.e. somevar[1] = '\0'
int a = atoi(somevar);
However, this is not working. If I try:
int a = (int) somevar[0]; // this works
My second problem is that I now need to convert this "integer value"
that I just got into a char* to be passed onto some funtion for printing
it out as a string. So I do:
char* b = malloc(sizeof(char)*SOME_LENGTH);
b = itoa(a, b, 1);
I then print this out. This seems to work fine, but is there a simpler
way of doing this? Thanks
Sona
I have a char* that holds an ascii character in its first element (at
least I think that's what it holds because when I print it, it prints
weird characters). I need to convert this into an integer and so I
thought the following might work:
char *somevar ... // somevar holds a value in somevar[0] and is null
terminated i.e. somevar[1] = '\0'
int a = atoi(somevar);
However, this is not working. If I try:
int a = (int) somevar[0]; // this works
My second problem is that I now need to convert this "integer value"
that I just got into a char* to be passed onto some funtion for printing
it out as a string. So I do:
char* b = malloc(sizeof(char)*SOME_LENGTH);
b = itoa(a, b, 1);
I then print this out. This seems to work fine, but is there a simpler
way of doing this? Thanks
Sona