Valhalla Legends Forums Archive | Visual Basic Programming | help strings to hex

AuthorMessageTime
Blade
How do I convert a text string like "BLAH" to "42 4C 41 48" the hex version of "BLAH"?
December 23, 2003, 8:17 PM
Grok
[quote author=Blade link=board=31;threadid=4419;start=0#msg36908 date=1072210663]
How do I convert a text string like "BLAH" to "42 4C 41 48" the hex version of "BLAH"?
[/quote]


[code] 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[/code]

"BLAH"
"42 4C 41 48"
December 23, 2003, 8:25 PM
TheMinistered
Just my version of the above code:
[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
[/code]
December 23, 2003, 9:07 PM
Blade
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
December 23, 2003, 9:54 PM
Spht
[quote author=TheMinistered link=board=31;threadid=4419;start=0#msg36911 date=1072213646]
Just my version of the above code:
[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
[/code]
[/quote]

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.
December 24, 2003, 4:05 AM
Grok
[quote author=Spht link=board=31;threadid=4419;start=0#msg36949 date=1072238713]
[quote author=TheMinistered link=board=31;threadid=4419;start=0#msg36911 date=1072213646]
Just my version of the above code:
[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
[/code]
[/quote]

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.
[/quote]

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
December 24, 2003, 4:32 AM
Grok
[quote author=Blade link=board=31;threadid=4419;start=0#msg36915 date=1072216492]
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
[/quote]

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

[quote author=Blade link=board=31;threadid=4419;start=0#msg36908 date=1072210663]
How do I convert a text string like "BLAH" to "42 4C 41 48" the hex version of "BLAH"?
[/quote]
December 24, 2003, 4:34 AM
Blade
[quote]
"that Grok's code generates"? What the hell? Didn't you ask for spaces?[/quote]
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?
December 24, 2003, 5:03 AM
Grok
[quote author=Blade link=board=31;threadid=4419;start=0#msg36965 date=1072242180]
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?[/quote]

What data type is BNLSCheckSum expecting? Post the function declaration. Post how you're calling it too.
December 24, 2003, 5:35 AM
Blade
Here is the code I am using for the BNLSCheckSum()function.

[code]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
[/code]
and here is how I am callling it.

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

where sOut is the sOut from Grok's code and pktP is defined as a string.
December 24, 2003, 3:45 PM
Grok
[quote author=Blade link=board=31;threadid=4419;start=0#msg36992 date=1072280728]
THIS is the function declaration:

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

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

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

The function takes a Long as its second parameter, yet you asked us to provide you a hex string. There's your type mismatch.
December 24, 2003, 4:22 PM
Blade
ok then how do I get the server code from the BNLS 0x0E packet and use it in the BNLSCheckSum() function im using?
December 24, 2003, 5:19 PM
Kp
[quote author=Blade link=board=31;threadid=4419;start=0#msg37006 date=1072286351]
ok then how do I get the server code from the BNLS 0x0E packet and use it in the BNLSCheckSum() function im using?[/quote]

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. :)
December 26, 2003, 7:34 PM
Spht
[quote author=Blade link=board=31;threadid=4419;start=0#msg37006 date=1072286351]
ok then how do I get the server code from the BNLS 0x0E packet and use it in the BNLSCheckSum() function im using?
[/quote]

Content is the data after the BNLS header.

[code]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[/code]
December 26, 2003, 7:39 PM

Search