Translator with type casting not working as supposed

Hi there,
I'm trying to program a "translator" to have a number be translated in an other representation.
As the code shows for example I'd like to translate a number (here 21089) in accordance to the character hold in the string "toRep" so if it would work there should be the output cba76.
To make it a little clearer toRep[2], toRep[1], toRep[0], toRep[8], toRep[9] I tried sveral things with String.functions and type casting, but I mostly got chars > ascii 126 or other dumb stuff.
Could someone help me

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


char toRep[]="abcdef9876";

 int num=21089; // code should be cba76

void setup ()
{
  Serial.begin (9600) ;
  Serial.println ("testing mode") ;
  Serial.println(toRep);

}

void loop ()
{

  int i;
  int y=0;
//  char x;
  char buffer[6]=""; // holds the number as ascii
  char code[6]=""; // holds the code

  itoa(num,buffer,10); // change the number to ascii to get a single Digit from num

  Serial.print("Content Buffer: "); Serial.println(buffer); // should be like num
  // start replacing
  for( i=0; i < strlen(buffer); i++){ // number could be from 1 to 5 digits
    //x=buffer[i];
    //Serial.print(x);Serial.print("|  ");
    //y=int(x);
    y=int(buffer[i]); // converting the digit from the string to int
    code[i]=toRep[y]; // copy the y character from toRep to code

    Serial.print(toRep[y]);
    Serial.print(code[i]);
  }

  Serial.println("<-"); // only for debugging EOL
  Serial.println(code);
  num+=1; // only for debugging
  delay(500);
}

buffer contains ascii digits from '0' to '9', so subtracting '0' gives the offset, a number from 1 to 9

    y = buffer[i]-'0'; // converting the digit from the string to int

I tried sveral things with String.functions and type casting

Casting an object of type String to anything else is just plain foolish.

F**king around using Strings when you really want to manipulate a string is foolish, too.
On the other hand, claiming to use Strings, when you really use strings, is even more foolish.

   y=int(buffer[i]); // converting the digit from the string to int
    code[i]=toRep[y]; // copy the y character from toRep to code

Why are you doing this? The toRep array does NOT have enough elements in it.

If you convert 21089 to a char array using itoa(), the values in the array will be 50, 51, 48, 56, and 57. Casting those values to an int does NOT convert 50 to '2'. And, it certainly does not convert 50 to 2.

Whandall:
buffer contains ascii digits from '0' to '9', so subtracting '0' gives the offset, a number from 1 to 9

    y = buffer[i]-'0'; // converting the digit from the string to int

This worked! Thanks for this

PaulS:
Casting an object of type String to anything else is just plain foolish.

Hmm I wonder why all C-compiler support functions to convert strings to whatever

F**king around using Strings when you really want to manipulate a string is foolish, too.
On the other hand, claiming to use Strings, when you really use strings, is even more foolish.

Even that English is not my native language and maybe I don't understand the ironic content, these sentencences lack any meaning.

y=int(buffer[i]); // converting the digit from the string to int

code[i]=toRep[y]; // copy the y character from toRep to code



Why are you doing this? The toRep array does NOT have enough elements in it.

For my opinion the content of toRep has ten chars, so 0 to 9 are ten digits.

If you convert 21089 to a char array using itoa(), the values in the array will be 50, 51, 48, 56, and 57. Casting those values to an int does NOT convert 50 to '2'. And, it certainly does not convert 50 to 2.

I'm actually unable to check whats in the arduinos memory, but Whandalls post made the code work.

BTW from the GNU-C library manual:

Function: int atoi (const char *string)
.....
This function is like atol, except that it returns an int. The atoi function is also considered obsolete; use strtol instead.

I know that fiddling around with strings could be / is /was / will always be a problem in C when you're not an absolute C-specialist. And I appreciate a nice function which will chop a given number, to point to the members of an array or as in my code a member of a string.
Nevertheless, thanks for your comprehensive post.

Hmm I wonder why all C-compiler support functions to convert strings to whatever

Converting a String to whatever is NOT the same as casting a String to whatever.

A cast tells the compiler that the memory location being cast can be treated as storing whatever kind of data the cast is to. The memory location where a String instance starts can NOT be treated as char data.

I know that fiddling around with strings could be / is /was / will always be a problem in C when you're not an absolute C-specialist.

Not at all. There are only a few string functions that are used regularly, and any C book (or even google) can be used to get the correct syntax quickly.

The real issue with string processing that trips most people is not knowing that somehow/somewhere they must allocate space for the string.

or as in my code a member of a string.

A C-style string does not have members, so this statement is NULL.

One way. Why not look at the atoi code? it's open-source.

void printUsingRep(unsigned int n) {
  if(n<10) {
    // special case
    print(toRep[0]);
  }

  unsigned int place = 1;

  while(n/place >= 10) place *= 10;

  while(place > 0) {
    print(toRep[(n/place)%10]);
    place /= 10;
  }
}