Advertisement
mvandevander

Broken IntToCharArray()

Sep 19th, 2018
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.71 KB | None | 0 0
  1. private static char[] IntToCharArray(int input)
  2. {
  3.      short len = 0; //Number of places in the integer
  4.      if(input < 10) len = 1;
  5.      else if(input < 100) len = 2;
  6.      else if (input < 1000) len = 3;
  7.      else if (input < 10000) len = 4;
  8.      else return null; // We just don't support those bigger numbers
  9.  
  10.      char[] output = new char[len];
  11.      int input_copy = input;
  12.      int i = 0;
  13.  
  14.      while (len > 0)
  15.      {
  16.          // 48 is just the offset in the ASCII table of the character
  17.          // '0', so if we add 0-9 to that index, we can get the
  18.          //characters for 0-9. 
  19.          output[i++] = (char)(48+(input%10));
  20.          input-=(10^len);
  21.          len--;
  22.      }
  23.      return output;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement