Author Topic: [JAVA, W3XP, W3GS] Host Counter Decoding  (Read 13287 times)

0 Members and 1 Guest are viewing this topic.

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
[JAVA, W3XP, W3GS] Host Counter Decoding
« on: December 06, 2009, 01:22:29 am »
DotA.For.Rest:
(char[8])3130303030303030 |text formated hexadecimal Hosting Counter (upper case)

After I get those 8 bytes, how would I go about converting them into a host counter/4-length byte array that I can send back to the host (e.g. new byte[] { 1, 0, 0, 0 })?

I have tried converting the byte array to a string, then using String.getBytes with the default, ascii, and utf-8 encodings. None of them work! :(

Thanks :)
« Last Edit: December 07, 2009, 11:41:13 am by SarCaSTiC »
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [JAVA] Host/Join Counter Decoding
« Reply #1 on: December 06, 2009, 07:50:13 pm »
That text, converted to a string is "10000000". I imagine to split it into 4 WORDs, then swap the byte order. That'd end you up with 3031, 3030, 3030, and 3030, which are "01", "00", "00", and "00".

It would help if I had a half a clue what you're talking about, though.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [JAVA] Host/Join Counter Decoding
« Reply #2 on: December 06, 2009, 09:35:56 pm »
I'm not really clear on what you're asking.

Are you supposed to convert the original to an integer, then get the individual bytes within the integer?

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: [JAVA, W3XP] HosT Counter Decoding
« Reply #3 on: December 07, 2009, 11:40:06 am »
I'm not really clear on what you're asking.

Are you supposed to convert the original to an integer, then get the individual bytes within the integer?
I pretty much want to know how to turn 3130303030303030 (which as Joe mentioned, is "10000000" as a string) to a new byte[] { 1, 0, 0, 0 }

That text, converted to a string is "10000000". I imagine to split it into 4 WORDs, then swap the byte order. That'd end you up with 3031, 3030, 3030, and 3030, which are "01", "00", "00", and "00".

It would help if I had a half a clue what you're talking about, though.
I am working with a W3XP game host counter, and have changed the title to clarify this; sorry!

So, do this?
Code: [Select]
byte[] original_segment = new byte[] { /* 3130303030303030 */ };
byte[] result = new byte[4];

for(int i = 0; i < 4; i++) {
result[i] = (byte)Integer.parseInt(new String(new byte[] { ((byte)(original_segment[i + 1] >> 4)), (byte)(original_segment[i] >> 4) }));
}

Going to test it out right now.
« Last Edit: December 07, 2009, 12:20:40 pm by SarCaSTiC »
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [JAVA, W3XP, W3GS] Host Counter Decoding
« Reply #4 on: December 07, 2009, 12:51:50 pm »
Are you supposed to drop half of the digits, or are you concatenating two digits together, and then converting the pairs to 4 bytes?  I know what the character array looks like as a string, but that doesn't help much. :)

Maybe since this is the bot dev thread, there's some context I'm missing out on, but it seems that you left essential details out of the problem specification.  It seems like an easy task whatever it is you're trying to do, but it's hard to answer a question when there's something missing.

Incidentally, your code only indexes 0 - 4 of the original array.  You want to index 2*i and 2*i+1, not i and i+1, unless you're meaning to throw out some of the digits.

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: [JAVA, W3XP, W3GS] Host Counter Decoding
« Reply #5 on: December 07, 2009, 04:03:52 pm »
Are you supposed to drop half of the digits, or are you concatenating two digits together, and then converting the pairs to 4 bytes?  I know what the character array looks like as a string, but that doesn't help much. :)

Maybe since this is the bot dev thread, there's some context I'm missing out on, but it seems that you left essential details out of the problem specification.  It seems like an easy task whatever it is you're trying to do, but it's hard to answer a question when there's something missing.

Incidentally, your code only indexes 0 - 4 of the original array.  You want to index 2*i and 2*i+1, not i and i+1, unless you're meaning to throw out some of the digits.

I am trying concatenate four pairs of two digits into bytes. In the string, two characters are meant represent one byte so I am not losing any of the digits!  :)
Unfortunately, the code above does not work:
Code: [Select]
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

EDIT:
Got it working!
Code: [Select]
byte[] processedHostCounter = new byte[4];
for(int i = 0; i < 4; i++) {
processedHostCounter[i] = (byte)Integer.parseInt(new String(new byte[] { hostCounter[i + 1], hostCounter[i] }));
}

Thanks everyone :)
« Last Edit: December 07, 2009, 04:07:30 pm by SarCaSTiC »
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [JAVA, W3XP, W3GS] Host Counter Decoding
« Reply #6 on: December 07, 2009, 04:09:54 pm »
Then wouldn't you want to end up with:

byte result[] = {10, 0, 0, 0}

not 1,0,0,0?

I'm not sure what you're doing with the byte array turning into a string, but it should be very easy:

Code: [Select]
// char[] orig = {'1', '0', '0', ...};
byte[] result = new byte[4];

for (int i = 0; i < result.length; i++)
{
    result[i] = Byte.parseByte("" + orig[2 * i] + orig[2 * i + 1]);
}

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [JAVA, W3XP, W3GS] Host Counter Decoding
« Reply #7 on: December 07, 2009, 04:11:49 pm »
In the code you gave, you lose 3 of the last digits.  You only index 0 - 4, and you repeat the characters in indexes 1-3.

Offline Chavo

  • x86
  • Hero Member
  • *****
  • Posts: 2219
  • no u
    • View Profile
    • Chavoland
Re: [JAVA, W3XP, W3GS] Host Counter Decoding
« Reply #8 on: December 07, 2009, 05:26:51 pm »
IIRC this value doesn't even have to be correct..

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: [JAVA, W3XP, W3GS] Host Counter Decoding
« Reply #9 on: December 07, 2009, 06:10:44 pm »
IIRC this value doesn't even have to be correct..

I thought the same until I started getting W3GS_REJECTJOIN from some hosts. Not having the problem any more :)
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [JAVA, W3XP, W3GS] Host Counter Decoding
« Reply #10 on: December 07, 2009, 06:52:25 pm »
IIRC this value doesn't even have to be correct..

I thought the same until I started getting W3GS_REJECTJOIN from some hosts. Not having the problem any more :)

Well, your code works in the example you gave, but it won't work in general...

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: [JAVA, W3XP, W3GS] Host Counter Decoding
« Reply #11 on: December 08, 2009, 08:47:58 pm »
In the code you gave, you lose 3 of the last digits.  You only index 0 - 4, and you repeat the characters in indexes 1-3.

Yeah, I changed it so its a bit like the example you posted:
Code: [Select]
byte[] processedHostCounter = new byte[4];
for(int i = 0; i < 4; i++) {
processedHostCounter[i] = (byte)Integer.parseInt(new String(new byte[] { hostCounter[2 * i + (i == 0 ? 1 : 0)], hostCounter[i] }), 16);
}

and the packet logs look good now :)
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [JAVA, W3XP, W3GS] Host Counter Decoding
« Reply #12 on: December 08, 2009, 08:57:55 pm »
That still seems weird... why are you indexing the way you are?  If you want to turn consecutive pairs into bytes, then you should be addressing hostCounter[2*i] and hostCounter[2*i+1].  With your code, if you have digits:

12345678

Your pairs are going to be:

(2,1), (3, 2), (5, 3), (7, 4)

I assume you want:

(1,2), (3,4), (5,6), (7,8)

(the digits within the pairs being reversed is a trivial variation, of course).  You'll get exactly that if you index with 2*i and 2*i+1

It seems that the way you're turning the concatenated digits into a string is a bit off.  The digits aren't going to form something in base-16.  It's going to be in decimal (which is syntactically legal for base-16, but it's semantically decimal).  Also, Byte.parseByte exists.  Why not just use it instead of Integer.parseInt and cast to byte?

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: [JAVA, W3XP, W3GS] Host Counter Decoding
« Reply #13 on: December 08, 2009, 11:18:55 pm »
That still seems weird... why are you indexing the way you are?  If you want to turn consecutive pairs into bytes, then you should be addressing hostCounter[2*i] and hostCounter[2*i+1].  With your code, if you have digits:

12345678

Your pairs are going to be:

(2,1), (3, 2), (5, 3), (7, 4)

I assume you want:

(1,2), (3,4), (5,6), (7,8)

(the digits within the pairs being reversed is a trivial variation, of course).  You'll get exactly that if you index with 2*i and 2*i+1

It seems that the way you're turning the concatenated digits into a string is a bit off.  The digits aren't going to form something in base-16.  It's going to be in decimal (which is syntactically legal for base-16, but it's semantically decimal).  Also, Byte.parseByte exists.  Why not just use it instead of Integer.parseInt and cast to byte?

I am trying to get (2, 1) (4, 3) (6, 5) (8, 7) :'(

I am using Integer.parseInt because I cannot get it to work using Byte.parseByte as previously shown :(
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [JAVA, W3XP, W3GS] Host Counter Decoding
« Reply #14 on: December 08, 2009, 11:22:29 pm »
I am trying to get (2, 1) (4, 3) (6, 5) (8, 7) :'(

(the digits within the pairs being reversed is a trivial variation, of course)

Then you index 2*i+1 and 2*i....

I am using Integer.parseInt because I cannot get it to work using Byte.parseByte as previously shown :(

(byte)Integer.parseInt should be in every way equivalent to Byte.parseByte...