We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
UTF82ASCII (Read 975 times)
UTF82ASCII
May 13th, 2008, 12:47pm
 
Hello,

i try to get a text message from the web and display it on a LED display. The problem is that the text, witch comes from the web is in an other codec then my LED display. I think the text is in UTF8 but i am not sure. My LED display understand just ASCII. So that s my question:
How can i change the Codec in Processing?

Thanks for all replyes.
Re: UTF82ASCII
Reply #1 - May 13th, 2008, 6:48pm
 
"Codec" might not be the right term, rather "encoding".
UTF-8 for Ascii characters (like a-zA-Z0-9 and simple punctuation) is just these characters. Perhaps you receive them in UTF-16 (two bytes per character), which is the standard for Java.
But without more info (what is the exact input? how to do send the result to your hardware?) I can't help much more.
Re: UTF82ASCII
Reply #2 - May 13th, 2008, 7:08pm
 
I get the Data like this:

XMLElement rss = new XMLElement(this, url);
 XMLElement[] description = rss.getChildren("channel/item/description");

so i get an array with my Datas.
I send this Array with osc to a second programm wich comunicates with the LED displays. The messages are safed in a String called Text.
My Hardware is steered by RS232 as followed:

void send(String Text) {
   Header();
   port.write(Text);
   port.write(trail);
 }

It works properly but my Display understand just ASCII signs. So i dont know how to engode UTF16 to ASCII.
Any idea?

Thanks for reply
Re: UTF82ASCII
Reply #3 - May 13th, 2008, 7:15pm
 
You can get the ASCII version of the string with:

Code:
String Text="wibble";
byte[] data=Text.getBytes("US-ASCII");
port.write(data); //I'm guessing that the write method can take a byte[]
Re: UTF82ASCII
Reply #4 - May 13th, 2008, 7:34pm
 
Thank you for your reply.

I tried to get these code running but the programm get an error message as follows

/tmp/build4708.tmp/Temporary_1999_7000.java:11:13:11:37: Semantic Error: The method "byte[] getBytes(java.lang.String $1) throws java.io.UnsupportedEncodingException;" can throw the checked exception "java.io.UnsupportedEncodingException", so its invocation must be enclosed in a try statement that catches the exception, or else this method must be declared to throw the exception.

Re: UTF82ASCII
Reply #5 - May 13th, 2008, 7:51pm
 
Ah right, try this:

Code:
byte[] data;
try
{
data=Text.getBytes("US-ASCII");
}
catch(Exception e)
{
e.printStackTrace();
}
Re: UTF82ASCII
Reply #6 - May 13th, 2008, 8:12pm
 
I tried this code like this


String Text="wibble";
byte[] data = new byte[Text.length()];

try
{
 data=Text.getBytes("US-ASCII");
}
catch(Exception e)
{
 e.printStackTrace();
}
print(data);

The result was:

[B@50305e

S dont understand what this program does and i cant understand the result. If i dont make a (new byte[]) the programm would not give me any result.

Re: UTF82ASCII
Reply #7 - May 13th, 2008, 8:30pm
 
You don't need to set the data length initially, as it gets wiped and the data put in it.

Use println rather than print, and you'll see the raw data contained in the byte array:
Code:
String Text="wibble";
byte[] data=null;

try
{
data=Text.getBytes("US-ASCII");
}
catch(Exception e)
{
e.printStackTrace();
}
println(data);


gives

Code:
[0] 119
[1] 105
[2] 98
[3] 98
[4] 108
[5] 101
Re: UTF82ASCII
Reply #8 - May 13th, 2008, 9:18pm
 
YES, that s it,
now it works properly.
Thanks a lot for your help.

Best regards.
Page Index Toggle Pages: 1