Nabmeister
Nabmeister

Reputation: 795

the int is not converting to a char for some reason

int myatoi(const char* string) {
int i;
i = 0;
while(*string) {
    i = (i << 3) + (i<<1) + (*string -'0');
    string++;
}
return i;
}

int main() {
int i = myatoi("10101");
printf("%d\n",i);
char a = (char)(((int)'0')+i);
printf("%c\n",a);


return 0;
}

my output turns out to be 10101

how to fix this

should I parse int by int to convert into a char because we can use any c built in functions that can help us convert

Upvotes: 0

Views: 175

Answers (4)

coproc
coproc

Reputation: 6257

The "trick"

printf("%c", '0'+5);

for getting the output 5 only works for (decimal) digits, i.e. 0-9. printf("%c", '0'+10) gives the output :. Why?

printf("%c",..) is for printing a single character. Each character has a numerical code (see ASCII code). 65 is the ASCII code for A, so printf("%c", 65) gives this letter. printf("%c", 48) gives the character 0. The C language allows to write '0' instead of 48, so you do not have to remember the ASCII code. The C compiler translates any character between '' or "" to its corresponding ASCII code. So the above code line is the same as

printf("%c", 48+5);

For converting an int into its string representation you can do it in C like this:

char repr[12]; // a 32-bit int has at most 10 digits; 
               // +1 for possible sign; +1 for closing 0-character ("null termination")
sprintf(repr, "%d", 10101); // prints the number into a string
itoa(10101, repr, 10); // does the same thing
printf("%s", repr); // prints the string representing the number
printf("%d", 10101); // does the same thing directly

Upvotes: 0

fycth
fycth

Reputation: 3489

A bit reworked and simplified variant:

unsigned int myatoi(const char* string)
{
  unsigned int i = 0;
  while(*string) i = (i << 3) + (i << 1) + *string++ - 0x30;
  return i;
}

int main()
{
  unsigned int i = myatoi("12133");
  printf("%u\n",i);

  return 0;
}

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126526

What do you expect to have happen?

Your code correctly converts the string "10101" into the integer 10101, and prints it. That looks fine. You then compute 10101+(int)'0' which is probably 10149 (in ascii or unicode, (int)'0' is 48). Converting that to a char probably gives you 165 (or perhaps -91) which probably doesn't correspond to a printable character on your system, so you get a blank/garbage/missing character.

All is as expected.

Upvotes: 2

Rohit
Rohit

Reputation: 1918

in case you only need a 5 digit number into string of char you can use this code :

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int myatoi(char* string) {
int i;
i = 0;
while(*string) {
    i = (i << 3) + (i<<1) + (*string -'0');
    string++;
}
return i;
}


char *strrev(char *str) {
    char *p1, *p2;
    if (!str || !*str)
        return str;
    for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
        *p1 ^= *p2;
        *p2 ^= *p1;
        *p1 ^= *p2;
    }
    return str;
}


char *itoa(int n, char *s, int b) {
    static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
    int i=0, sign;

    if ((sign = n) < 0)
        n = -n;
    do {
        s[i++] = digits[n % b];
    } while ((n /= b) > 0);
    if (sign < 0)
        s[i++] = '-';
    s[i] = '\0';
    return strrev(s);
}


int main() {
char buffer[5];
int i = myatoi("10101");

printf("%d\n",i);
itoa(i, buffer, 10);
printf("%s\n",buffer);


return 0;
}

source of the 2 functions I have used : itoa and strrev
http://www.daniweb.com/software-development/c/threads/11049/convert-int-to-string#

Upvotes: 0

Related Questions