Loading...
Logo
Processing Forum
Hi,

I can't figure this one out:

String drivingme = "nuts";
println(byte(drivingme.charAt(1))); // 0

println(byte("u")); // 0
println(byte('u')); // 117


I need the ascii codes of each letter in a string.
It works fine on P5, not in P5JS.

Doing a char("u") crashes the program.

Any tips?

a

Replies(9)



Copy code
  1. println(int(drivingme.charAt(1)));



Thanks but unfortunately that doesn't work for me. It prints 0.

drivingme.charAt(1) is equal to "u", and int("u") is 0,
if I'm not mistaken.

it works in processing, I don't know about pjs


drivingme.charAt(1) is equal to "u"


no, it's 'u', a char, not a string ("u")

Sorry, can't help you...................


 no, it's 'u', a char, not a string ("u")
Maybe that's the problem, in JS it's not a 'u', but a "u", and I don't know how to cast it.

I know it works in P5. I'm trying to port it to pjs and I get all kinds of troubles :(

UPDATE: it seems to be a bug.
https://processing-js.lighthouseapp.com/projects/41284/tickets/1394-stringcharat-returns-invalid-type

Cheers!

a
Take a look at this recent post for further explanations:

Here's some medicine to your drivingMe = "Nuts":  
Copy code
    final String drivingMe = "Nuts";
    final int[] medicine = int( drivingMe.toCharArray() );
    
    println(medicine);
    
    exit();
    
As you may notice, 1st convert String into an Array of char. Then, make them an Array of int.  

In case you're using 8-bit ASCII only, and not the full 16-bit UNICODE, 
you may also try method getBytes() rather than toCharArray() + int() combo.

[EDIT] : PJS doesn't recognize method getBytes() from class String!  

Although I'm not versed in JS yet; I believe in JS, a value is either a float or a String!  
Maybe it can be a boolean too.
Yes!! That's the undocumented function I was looking for :) Thank you!

You can see the program here http://www.openprocessing.org/sketch/102142 :)
I know, but I did not expect that to work in Javascript.
Actually, it all depends how far Processing.JS's developers decide to convert Java to JS!
String's method getBytes() wasn't so lucky!  

They should strive to get as much as possible!  
As long as it's not that hard or unfeasible, of course.