News:

Happy New Year! Yes, the current one, not a previous one; this is a new post, we swear!

Main Menu

[Java] Unicode strings where they should not be

Started by Camel, September 24, 2007, 11:34:21 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Camel

Okay, so it turns out the reason my bot couldn't connect with SC/BW/W2 is that when it converted the binary byte[]s to strings in readNTString(), the bytes got all messed up because of some kind of unicode clash.

My solution was to just keep them as byte arrays, never converting to Strings, but just for the sake of curiosity, does anyone know any way to prevent this?

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

iago

I don't think that a String is specifically ascii or unicode, it's the output function that interprets it that way.

This may happen if you're using a "Writer" class, those will attempt to decode the string. Use a "Stream" class instead, like OutputStream. 

Camel

#2
I was using new String(byte[])

Other methods that did not work:

String += (char)readByte;

CharBuffer

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

iago

I think that should work fine. Try this:

String s = new String(...bytes...);
FileOutputStream out = new FileOutputStream(test);
out.write(s.getBytes());
out.close();


See if that writes the proper binary data. System.out is a PrintWriter, so it will attempt to decode the string. That's why you can't check binary data (easily) on stdout.

Camel

I have a HexDump class which I was using to look at the binary data.

It's fucked as soon as it goes in to the String.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!