I'm writing a bot for a online game I play that will kick people off if they have been idle for too long. To make the bot go from arena to arena it has to turn the arena address to bytes then send a packet that will make the bot move to that arena. But, people are able to make arenas using a character called a thorn (�) which is made by pressing alt+0254. So I was wondering if anyone here knew any way to turn that into a byte so that it will go to arenas with the thorn in the name. I tried something like...

String thorn = "�"; byte bytes[] = thorn.toBytes();

...but it turned the thorn into a negative 2 and it completely ignores the -2 when it is sending bytes. Anyone know the problem?

Posts:179 Registered: 7/30/04

Re: Thorn to bytes Aug 7, 2004 5:14 PM (reply 1 of 11)

How do you know that it turns into -2?How do you send data?Is it -2 before transmission or after being received?

Posts:16 Registered: 8/7/04

Re: Thorn to bytes Aug 7, 2004 5:23 PM (reply 2 of 11)

I know it turns into a -2 because I printed it out in command prompt with a few other characters that I turned into bytes. I believe data is sent using DatagramSocket. It's a -2 before transmission.

Posts:179 Registered: 7/30/04

Re: Thorn to bytes Aug 7, 2004 5:34 PM (reply 3 of 11)

The reason for "-2" may be sign confusion: 254 = 0xFE, expanded to 0xFFFFFFFE = -2 by some routines.

Try with other high number characters, for example Alt+0128 and Alt+0129.I would start searching for errors around the transmit code.

Posts:16 Registered: 8/7/04

Re: Thorn to bytes Aug 7, 2004 5:42 PM (reply 4 of 11)

the transmitting code has no errors, I've looked all through there, it seems like Java doesn't like the �...if you System.out.println("�"); it will come out with some bar thing which is kind of like alt+254 in notepad

Posts:179 Registered: 7/30/04

Re: Thorn to bytes Aug 7, 2004 5:51 PM (reply 5 of 11)

Quick solution would be to try using the URLEncoder to get rid of all such special characters.

Posts:16 Registered: 8/7/04

Re: Thorn to bytes Aug 7, 2004 5:59 PM (reply 6 of 11)

I've tried using the URLEncoder...but I'm not to good with it...and the thing sends packets in bytes...so it translates anything to bytes first...

Posts:4,130 Registered: 3/31/99

Re: Thorn to bytes Aug 7, 2004 7:34 PM (reply 7 of 11)

You might want to look into the ByteBuffer class. Or use a DataOutputStream and ByteArrayOutputStream. Either way, your chars will get into a byte array without going through any decoding. But you will have to read the bytes at the other end in a corresponding manner.

Posts:16 Registered: 8/7/04

Re: Thorn to bytes Aug 7, 2004 7:47 PM (reply 8 of 11)

that's the problem, the game isn't really changeable...it's written in some language I don't know and I couldn't even begin to change (yet...i'm just going into junior year of highschool now)...but, I was playing around with the 0xFE thing someone said something about before and got it to go to a arena when I manually added the 0xFE byte to the packet...when I tried to get it to detect a � and replace it with the 0xFE byte it didn't seem to work :/

Posts:11,200 Registered: 7/22/99

Re: Thorn to bytes Aug 8, 2004 1:37 AM (reply 9 of 11)

the transmitting code has no errors, I've looked allthrough there, it seems like Java doesn't like the�...if you System.out.println("�"); it will come outwith some bar thing which is kind of like alt+254 innotepad

Something is definitely wrong with the transmitting code or the receiving end.

For the transmitting code, it should not make a difference whether the bytes handed to the transmitting system have negative values or not, it should pass them regardless.

There could be a character encoding issue with the receiving end. When you use the getBytes() method without any arguments it uses the platform's default character encoding, often windows-1252 on Windows systems. The receiving end may use another encoding, like the platform independent UTF-8. Documentation of the communication protocol should tell you which encoding to use.

Also, the DOS prompt uses a different character encoding than the rest of Windows, which causes presentation errors. Try writing the thorn character in a text file with notepad and opening the file in DOS (use TYPE or EDIT command) -- the character you'll see is not thorn.

Posts:262 Registered: 10/8/03

Re: Thorn to bytes Aug 8, 2004 3:27 AM (reply 10 of 11)

Instead of this

String thorn = "þ"; byte bytes[] = thorn.toBytes();

you can also write this

byte bytes[]=new byte[1];bytes[0]=0xFE;

In both cases the array will be absolutely identical in every respect.

So if you say it works with 0xFE but not with "þ" the error has to be elsewhere.But without your source code it's impossible to tell where.

Posts:11,200 Registered: 7/22/99

Re: Thorn to bytes Aug 8, 2004 6:38 AM (reply 11 of 11)

In both cases the array will be absolutely identical in every respect.

Not all the time. The getBytes method uses the platform's default character encoding to convert characters to bytes. If the default encoding maps þ to something else than 0xFE the array won't contain 0xFE. For instance on my computer the default character encoding happens to be UTF-8, so "þ".getBytes() returns the byte array {-61, -98}.

Moral of the story: if you are writing networking code, never use methods that assume the default character encoding. Always use methods that let your program specify the encoding.