News:

Holy shit, it's 2018 2019 2020 2021 2022 2023 2024, and the US isn't a fascist country! What a time to be alive.

Main Menu

[JAVA, W3XP, W3GS] Host Counter Decoding

Started by Lance, December 06, 2009, 01:22:29 AM

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

Lance

Quote from: http://forum.valhallalegends.com/index.php?topic=14994.0
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 :)
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!

Joe

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.
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Sidoh

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?

Lance

#3
Quote from: Sidoh 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?
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 }

Quote from: Joe 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 am working with a W3XP game host counter, and have changed the title to clarify this; sorry!

So, do this?

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

Sidoh

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.

Lance

#5
Quote from: Sidoh 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.

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:

java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)


EDIT:
Got it working!

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

Sidoh

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:


// 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]);
}

Sidoh

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.

Chavo

IIRC this value doesn't even have to be correct..

Lance

Quote from: Chavo on December 07, 2009, 05:26:51 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!

Sidoh

Quote from: SarCaSTiC on December 07, 2009, 06:10:44 PM
Quote from: Chavo on December 07, 2009, 05:26:51 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...

Lance

Quote from: Sidoh 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.

Yeah, I changed it so its a bit like the example you posted:

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!

Sidoh

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?

Lance

Quote from: Sidoh 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?

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!

Sidoh

Quote from: SarCaSTiC on December 08, 2009, 11:18:55 PM
I am trying to get (2, 1) (4, 3) (6, 5) (8, 7) :'(

Quote from: Sidoh on December 08, 2009, 08:57:55 PM(the digits within the pairs being reversed is a trivial variation, of course)

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

Quote from: SarCaSTiC on December 08, 2009, 11:18:55 PM
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...