• Welcome to Valhalla Legends Archive.
 

help strings to hex

Started by Blade, December 23, 2003, 02:17 PM

Previous topic - Next topic

Blade

How do I convert a text string like "BLAH" to "42 4C 41 48" the hex version of "BLAH"?

Grok

Quote from: Blade on December 23, 2003, 02:17 PM
How do I convert a text string like "BLAH" to "42 4C 41 48" the hex version of "BLAH"?


   Dim sIn As String, sOut As String
   Dim lPos As Long
   sIn = Text1.Text
   If Len(sIn) = 0 Then Exit Sub
   For lPos = 1 To Len(sIn)
       If Len(sOut) > 0 Then sOut = sOut & " "
       sOut = sOut & Right("00" & Hex(Asc(Mid(sIn, lPos, 1))), 2)
   Next lPos
   Text2.Text = sOut


"BLAH"
"42 4C 41 48"

TheMinistered

#2
Just my version of the above code:

Dim sIn As String, sOut As String
Dim lPos As Long
   
sIn = Text1.Text
If LenB(sIn) = 0 Then Exit Sub
   
For lPos = 1 To Len(sIn)
   If LenB(sOut) > 0 Then sOut = sOut & " "
   sOut = sOut & Right$("00" & Hex(Asc(Mid$(sIn, lPos, 1))), 2)
Next lPos
Text2.Text = sOut

Blade

#3
ok,  the resone I needed that was to get the servercode from the BNLS_AUTHERIZE(&H0E). I save the resoult as a string and use the function made for encoding you password with the severcode. Anyways to get to the point BNLSChecksum("my_password", THE_STRING_THAT_HAS_THE_SERVERCODE) gives me a type missmatch error. How do I fix this?

<EDIT> Yes I did remove the spaces from the string that Grok's code generates

Spht

Quote from: TheMinistered on December 23, 2003, 03:07 PM
Just my version of the above code:

Dim sIn As String, sOut As String
Dim lPos As Long
   
sIn = Text1.Text
If LenB(sIn) = 0 Then Exit Sub
   
For lPos = 1 To Len(sIn)
   If LenB(sOut) > 0 Then sOut = sOut & " "
   sOut = sOut & Right$("00" & Hex(Asc(Mid$(sIn, lPos, 1))), 2)
Next lPos
Text2.Text = sOut


What did you change, exactly? Removed base tabbing? Someone's being a little picky...

You should do Right$("0" & ...) since Hex can never return a blank string so the extra 0 is redundant.

Grok

Quote from: Spht on December 23, 2003, 10:05 PM
Quote from: TheMinistered on December 23, 2003, 03:07 PM
Just my version of the above code:

Dim sIn As String, sOut As String
Dim lPos As Long
   
sIn = Text1.Text
If LenB(sIn) = 0 Then Exit Sub
   
For lPos = 1 To Len(sIn)
   If LenB(sOut) > 0 Then sOut = sOut & " "
   sOut = sOut & Right$("00" & Hex(Asc(Mid$(sIn, lPos, 1))), 2)
Next lPos
Text2.Text = sOut


What did you change, exactly? Removed base tabbing? Someone's being a little picky...

You should do Right$("0" & ...) since Hex can never return a blank string so the extra 0 is redundant.

Someone's being a little picky?  This is twice now you've focused on "0" vs "00" in my code when doing a Right().  The last time you were completely wrong, and this time it's a non-factor and insignificant.  I think you're being picky too, wouldn't you say?

There's a very good reason for using two characters instead of one, and that is in practice, if you write thousands of lines of code per week, your idioms protect you from oversights, and allow you to focus on bigger issues.  The "00" is correct, even when using Hex(), because we wanted to return 2 character digits.  Had I wanted 9, then 9 zeroes would have been correct.  This leaves you in the desirable position to not have to worry whether the intelligence on the padding has any characters, or none.  A great timesaver and smart thing to do.

So I'd appreciate it if you'd stop optimizing my code in amateur insignificant ways, that have no effect on the algorithm.  Thanks in advance.

Grok

Grok

Quote from: Blade on December 23, 2003, 03:54 PM
ok,  the resone I needed that was to get the servercode from the BNLS_AUTHERIZE(&H0E). I save the resoult as a string and use the function made for encoding you password with the severcode. Anyways to get to the point BNLSChecksum("my_password", THE_STRING_THAT_HAS_THE_SERVERCODE) gives me a type missmatch error. How do I fix this?

<EDIT> Yes I did remove the spaces from the string that Grok's code generates

"that Grok's code generates"?  What the hell?  Didn't you ask for spaces?

Quote from: Blade on December 23, 2003, 02:17 PM
How do I convert a text string like "BLAH" to "42 4C 41 48" the hex version of "BLAH"?

Blade

#7
Quote
"that Grok's code generates"?  What the hell?  Didn't you ask for spaces?
Yes I did becouse I need it for other functions but the BNLSCheckSum() function can not have spaces in it so I removed them from the final resoult but, I did need the spaces for my other functions. I was just saying that I removed the spaces so some one didn't tell me I am getting a type miss mach in the BNLSChecksum() function becouse I had spaces in the servercode.

EDIT:
What im asking now is why am I getting this type miss mach error when I try to use the string for my server code in the BNLSCheckSum() algorithm?

Grok

Quote from: Blade on December 23, 2003, 11:03 PM
What im asking now is why am I getting this type miss mach error when I try to use the string for my server code in the BNLSCheckSum() algorithm?

What data type is BNLSCheckSum expecting?  Post the function declaration.  Post how you're calling it too.

Blade

#9
Here is the code I am using for the BNLSCheckSum()function.

Option Explicit

Private Const CRC32_POLYNOMIAL As Long = &HEDB88320
Private CRC32Table(0 To 255) As Long
Private Sub InitCRC32()
   Dim I As Long, J As Long, K As Long, XorVal As Long
   
   Static CRC32Initialized As Boolean
   If CRC32Initialized Then Exit Sub
   CRC32Initialized = True
   
   For I = 0 To 255
       K = I
       
       For J = 1 To 8
           If K And 1 Then XorVal = CRC32_POLYNOMIAL Else XorVal = 0
           If K < 0 Then K = ((K And &H7FFFFFFF) \ 2) Or &H40000000 Else K = K \ 2
           K = K Xor XorVal
       Next
       
       CRC32Table(I) = K
   Next
End Sub

Private Function CRC32(ByVal Data As String) As Long
   Dim I As Long, J As Long
   
   Call InitCRC32
   
   CRC32 = &HFFFFFFFF
   
   For I = 1 To Len(Data)
       J = CByte(Asc(Mid(Data, I, 1))) Xor (CRC32 And &HFF&)
       If CRC32 < 0 Then CRC32 = ((CRC32 And &H7FFFFFFF) \ &H100&) Or &H800000 Else CRC32 = CRC32 \ &H100&
       CRC32 = CRC32 Xor CRC32Table(J)
   Next
   
   CRC32 = Not CRC32
End Function

Public Function BNLSChecksum(ByVal Password As String, ByVal ServerCode As Long) As Long
   BNLSChecksum = CRC32(Password & Right("0000000" & Hex(ServerCode), 8))
End Function

and here is how I am callling it.

   pktP = sOut
   pktP = Replace(pktP, " ", "")
   pbuff.InsertString Hex(BNLSChecksum("MY_BNLS_PASSWORD", pktP))
   pbuff.SendPacket wsBNLS, &HF


where sOut is the sOut from Grok's code and pktP is defined as a string.

Grok

#10
Quote from: Blade on December 24, 2003, 09:45 AM
THIS is the function declaration:

Public Function BNLSChecksum(ByVal Password As String, ByVal ServerCode As Long) As Long
and here is how I am callling it.

   pktP = sOut
   pktP = Replace(pktP, " ", "")
   pbuff.InsertString Hex(BNLSChecksum("MY_BNLS_PASSWORD", pktP))
   pbuff.SendPacket wsBNLS, &HF


where sOut is the sOut from Grok's code and pktP is defined as a string.

The function takes a Long as its second parameter, yet you asked us to provide you a hex string.  There's your type mismatch.

Blade

ok then how do I get the server code from the BNLS 0x0E packet and use it in the BNLSCheckSum() function im using?

Kp

Quote from: Blade on December 24, 2003, 11:19 AM
ok then how do I get the server code from the BNLS 0x0E packet and use it in the BNLSCheckSum() function im using?

Just from looking at the function prototype, I'd say you're supposed to pass the 32bit value from BNLS 0xe directly into BNLSChecksum.  Consider using CopyMemory to move it (afaik, there's no easier way in VB) into a long of your own declaration, then use that long as the second parameter to BNLSChecksum.  Your original question asking for conversion to hex seems to be entirely inappropriate to this problem. :)
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Spht

Quote from: Blade on December 24, 2003, 11:19 AM
ok then how do I get the server code from the BNLS 0x0E packet and use it in the BNLSCheckSum() function im using?

Content is the data after the BNLS header.

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, source As Any, ByVal length As Long)

Dim ServerCode&: CopyMemory ServerCode, ByVal Left(Content, 4), 4