Int(str), where str is a single-digit number, gives the utf char code instead of the number as int

edited March 2018 in Questions about Code
String pi = "31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170";

int x;

void draw () {
  for (int i = 0; i < pi.length(); i++) {
    x = int(pi.charAt(i));
    println(x);
  }
}

And this prints numbers around 48-58 as x in the log. I'm dumbfounded as to why it wouldn't give me the right characters from the String pi. Thank you for your help in advance.

Edit: I realized the int(pi.charAt(i)) is not working, as this results in the utf-8 character code of the given number from 0 to 9, which are 48-57. Of course, I can work around this, but would still be best to see how this should be done properly.

Answers

  • Yeah there are various ways to work around this.

        x = int(str(pi.charAt(i)));
        x = int(""+pi.charAt(i));
        x = int(pi.substring(i,i+1)); 
        x = int(pi.charAt(i)-'0');
    

    Now what's the proper way do to it?
    I don't know

  • I would normally use charAt(i) - '0' without the int() - it should already be an int. (Untested)

  • edited March 2018

    Method CharSequence::charAt() already returns the primitive datatype char: ~O)

    1. https://Docs.Oracle.com/javase/9/docs/api/java/lang/CharSequence.html#charAt-int-
    2. https://Processing.org/reference/char.html

    Therefore, it's already "printable" as it is. B-)
    Dunno why you're converting it to some other datatype! :-@

    Anyways, here's my own attempt using String::toCharArray() in place of CharSequence::charAt(): O:-)

    final String PI_STR = "3141592653589793238462643383279502884197169399"
      + "3751058209749445923078164062862089986280348253421170";
    
    for (final char ch : PI_STR.toCharArray())  println(ch);
    
    println(PI_STR);
    
    exit();
    
  • edited March 2018

    Just to make it clear CharSequence::charAt() works as well, here's my attempt using it: :bz

    final String PI_STR = "3141592653589793238462643383279502884197169399"
      + "3751058209749445923078164062862089986280348253421170";
    
    for (int i = 0, len = PI_STR.length(); i < len; println(PI_STR.charAt(i++)));
    
    println(PI_STR);
    
    exit();
    
  • But the point is to get from the '3' to a 3

  • final String PI_STR = "3141592653589793238462643383279502884197169399"
      + "3751058209749445923078164062862089986280348253421170";
    
    final int STR_LEN = PI_STR.length();
    
    final byte[] piDigits = new byte[STR_LEN];
    
    for (int i = 0; i < STR_LEN; piDigits[i] = (byte) (PI_STR.charAt(i++) - '0'));
    
    println(piDigits);
    println(PI_STR);
    
    exit();
    
  • edited March 2018 Answer ✓
    final String PI_STR = "3141592653589793238462643383279502884197169399"
      + "3751058209749445923078164062862089986280348253421170";
    
    final byte[] piDigits = byte(PI_STR.toCharArray());
    
    for (int i = 0, len = piDigits.length; i < len; piDigits[i++] -= '0');
    
    println(piDigits);
    println(PI_STR);
    
    exit();
    
Sign In or Register to comment.