Author Topic: [Java] Unicode strings where they should not be  (Read 2678 times)

0 Members and 1 Guest are viewing this topic.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
[Java] Unicode strings where they should not be
« on: September 24, 2007, 11:34:21 pm »
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!

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Java] Unicode strings where they should not be
« Reply #1 on: September 25, 2007, 10:07:23 am »
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. 

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: [Java] Unicode strings where they should not be
« Reply #2 on: September 25, 2007, 02:47:45 pm »
I was using new String(byte[])

Other methods that did not work:

String += (char)readByte;

CharBuffer
« Last Edit: September 25, 2007, 03:19:31 pm by Camel »

<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!

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Java] Unicode strings where they should not be
« Reply #3 on: September 25, 2007, 02:58:11 pm »
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.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: [Java] Unicode strings where they should not be
« Reply #4 on: September 25, 2007, 03:20:38 pm »
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!