DWUTKA at marlow.com
DWUTKA at marlow.com
Mon Feb 6 12:51:45 CST 2006
Just something I put together on Friday, for one of my projects. I don't know if anyone on the list will find it useful. In this particular project, I'm sending data through winsock controls, and I need to make sure the data gets put through. There are several levels to it. In Access, text fields are prefaced by a byte that represents the length of the text in the field. So if the text is 'HELLO', that's 5 characters, so the field will have 6 bytes, the first byte being Chr(5) the rest being Chr(72) chr(69) Chr(76) Chr(76) Chr(79). In my 'Data Protocol' class, I link a collection of strings by prefacing the string with it's length. If I wanted to be limited to 255 characters per string, I could just use: Dim i Dim strCombined For i=1 to DataStrings.Count strCombined=strCombined & Chr(len(DataStrings(i))) & DataStrings(i) Next i Now I have a string that I can seperate no matter what characters are used in the strings within the DataStrings collection. However, what if some of those strings are longer then 255 characters? Then a 2 byte preface would work. That gets you to 64k characters. The four functions I am posting at the end of this will convert an Integer or a Long into a string, and back again. So if you are using a Long, and you send it 6, you'll get Chr(6), Chr(0),Chr(0),Chr(0) as the string that is returned. I was using a process that relied on math, to get 2 digits, and wanted something a little faster. These functions scream, because they are simply copying memory back and forth, doing 'forced' conversions. (1234 converted to a string is "1234", which is NOT the binary represenation of the number 1234. Converting 123 to a string would be "123", 3 characters long, therefore not a very good 'length pointer'. Converted to Chr(123) Chr(0) Chr(0) Chr(0), that is four characters, and thus you know exactly how much of your string to 'read' to get the length of the actual string. Anyhow, I hope someone finds a use for this, I thought it was too neat to not pass along: Option Explicit Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Function LongToString(intLong As Long) As String Dim byteArray(0 To 3) As Byte CopyMemory byteArray(0), ByVal VarPtr(intLong), 4 LongToString = StrConv(byteArray, vbUnicode) End Function Function StringToLong(strString As String) As Long Dim byteArray(0 To 3) As Byte Dim intTemp As Long CopyMemory byteArray(0), ByVal strString, 4 CopyMemory ByVal VarPtr(intTemp), byteArray(0), 4 StringToLong = intTemp End Function Function IntegerToString(intInteger As Integer) As String Dim byteArray(0 To 1) As Byte CopyMemory byteArray(0), ByVal VarPtr(intInteger), 2 IntegerToString = StrConv(byteArray, vbUnicode) End Function Function StringToInteger(strString As String) As Integer Dim byteArray(0 To 1) As Byte Dim intTemp As Integer CopyMemory byteArray(0), ByVal strString, 2 CopyMemory ByVal VarPtr(intTemp), byteArray(0), 2 StringToInteger = intTemp End Function