daily pastebin goal
61%
SHARE
TWEET

Broken IntToCharArray()

mvandevander Sep 19th, 2018 2 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top