VB6.0 help required...
If I''ve got a string that is four characters long (or an array of four bytes), how would I go about converting that to a long?
for example:
Dim s as String
Dim l as Long
s = Chr(0) & Chr(0) & chr(0) & chr(69)
l = SomeCoolAndHiddenFunction(s) ''or something?
This is beginning to bug me!
I'm not sure I understand you completely, so let me know if this is incorrect. This code converts all four byte characters to their ASCII values and places them alongside one another.
Dim str As String * 4
Dim lng As Long
Dim strConv As String
'This creates the string we want to convert:
str = Chr(a) & Chr(b) & Chr(c) & Chr(d)
strConv = Asc(Mid(str,1,1)) & Asc(Mid(str,2,1)) & Asc(Mid(str,3,1)) & _
Asc(Mid(str,4,1))
lng = Val(strConv)
Edited by - Tom on September 20, 2000 9:55:04 AM
Dim str As String * 4
Dim lng As Long
Dim strConv As String
'This creates the string we want to convert:
str = Chr(a) & Chr(b) & Chr(c) & Chr(d)
strConv = Asc(Mid(str,1,1)) & Asc(Mid(str,2,1)) & Asc(Mid(str,3,1)) & _
Asc(Mid(str,4,1))
lng = Val(strConv)
Edited by - Tom on September 20, 2000 9:55:04 AM
GDNet+. It's only $5 a month. You know you want it.
the val(string) function will convert any string with a numeric character in it to a number, and then put the numbers together in the order in which they were supplied.
so it would be
Dim sString As String
Dim iInt as Integer
sString = Chr(0) & Chr(0) & chr(0) & chr(69)
iInt = Val(sString)
then iInt will hold the integer value of the number.....
hope this helps
to Code, or Not To Code
so it would be
Dim sString As String
Dim iInt as Integer
sString = Chr(0) & Chr(0) & chr(0) & chr(69)
iInt = Val(sString)
then iInt will hold the integer value of the number.....
hope this helps
to Code, or Not To Code
to Code, or Not To Code
Thanks people, but you misunderstand I didn''t explain properly... my string is actually a four byte long in binary - basically I have the Visual Basic equivalent of exactly what the following C would produce:
char buffer[4];
*(long *) buffer = (long) 6969; // for example.
Do you see my frustration?
char buffer[4];
*(long *) buffer = (long) 6969; // for example.
Do you see my frustration?
Hey cool. I just made a function that does this recently for network programming
It uses memcopy. I don''t have it in front of me right now, but I''ll try and give you pseudo style code
I don''t really remember the parameters for MemCopy, but I think it''s MemCopy(Dest as any, Src as Any, Length as long)
Anyhoo,
---------------------------
"Don't die for your country, make some other dumb bastard die for his" -General George S. Patton
It uses memcopy. I don''t have it in front of me right now, but I''ll try and give you pseudo style code
I don''t really remember the parameters for MemCopy, but I think it''s MemCopy(Dest as any, Src as Any, Length as long)
Anyhoo,
Function GetLongString(V as Long) as String Dim ByteArray(3) as Byte Dim Ret as String Dim I as Long ''Copy Memory MemCopy V, ByteArray, 4 ''Now copy bytes to string For I = 0 to 3 ret = ret & chr(ByteArray(3)) Next GetLongString = retEnd Function
---------------------------
"Don't die for your country, make some other dumb bastard die for his" -General George S. Patton
Oh yeah, mail me if you don''t get it because I might not see this post again
---------------------------
"Don't die for your country, make some other dumb bastard die for his" -General George S. Patton
---------------------------
"Don't die for your country, make some other dumb bastard die for his" -General George S. Patton
Okay, I see now. You don''t need the Win32 API to do this.
Dim strBuff As String
Dim lngInput As Long
Dim bytPart(3) As Byte
bytPart(0) = lngInput And 127: lngInput = lngInput \ 128
bytPart(1) = lngInput And 127: lngInput = lngInput \ 128
bytPart(2) = lngInput And 127: lngInput = lngInput \ 128
bytPart(3) = lngInput And 127
strBuff = Chr(bytPart(0)) & Chr(bytPart(1) & Chr(bytPart(2)) & _
Chr(bytPart(3))
Notice those division symbols are backslashes for integer division. This code can be reduced to four lines, but I''ll leave that as an exercise to the reader.
Dim strBuff As String
Dim lngInput As Long
Dim bytPart(3) As Byte
bytPart(0) = lngInput And 127: lngInput = lngInput \ 128
bytPart(1) = lngInput And 127: lngInput = lngInput \ 128
bytPart(2) = lngInput And 127: lngInput = lngInput \ 128
bytPart(3) = lngInput And 127
strBuff = Chr(bytPart(0)) & Chr(bytPart(1) & Chr(bytPart(2)) & _
Chr(bytPart(3))
Notice those division symbols are backslashes for integer division. This code can be reduced to four lines, but I''ll leave that as an exercise to the reader.
GDNet+. It's only $5 a month. You know you want it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement