|
-
Apr 18th, 2008, 10:34 PM
#1
Thread Starter
Junior Member
string to byte help needed...
I am having trouble converting a string to byte(in hex)...below is what I'm trying to acheive...
Dim myByteArr(10) As Byte
Text file is of the form:
1011 21FF FF00 00FF
So I'd like to achieve:
myByteArr(0) = 10
myByteArr(1) = 11
myByteArr(2) = 21
myByteArr(3) = FF
myByteArr(4) = FF
myByteArr(5) = 00
so on...
I need to read each line in the text file which looks like shown above and
split the bytes and assign it like shown above, but I'm getting a type conversion error, How is it possible to achieve what I am desiring...pls help...thnks...in advance................
-
Apr 18th, 2008, 10:59 PM
#2
Re: string to byte help needed...
Are you converting the hex values to decimal first? Ex: a byte array can't store "FF" it would need to be converted to 255 first. You can try this:
Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Function HexToBytes(ByRef FilePath As String) As Byte()
Dim bytRet() As Byte, strLine As String
Dim intFF As Integer, strValues() As String
Dim l As Long, lonCount As Long
intFF = FreeFile
ReDim bytRet(499) As Byte
Open FilePath For Input As #intFF
Do While Not EOF(intFF)
Line Input #intFF, strLine
If Len(strLine) > 0 Then
strValues = Split(strLine, " ")
For l = 0 To UBound(strValues)
bytRet(lonCount) = CByte(Val("&H" & Left$(strValues(l), 2)))
lonCount = lonCount + 1
bytRet(lonCount) = CByte(Val("&H" & Mid$(strValues(l), 3)))
lonCount = lonCount + 1
If UBound(bytRet) < lonCount Then
ReDim Preserve bytRet(0 To lonCount + 500) As Byte
End If
Next l
End If
Loop
Close #intFF
If lonCount > 2 Then
If UBound(bytRet) > lonCount Then
ReDim Preserve bytRet(0 To lonCount - 1) As Byte
End If
End If
HexToBytes = bytRet
Erase bytRet
End Function
Private Sub Form_Load()
Dim bytBytes() As Byte
'Get the hex values to a byte array.
bytBytes = HexToBytes("C:\test.txt")
'Show the results.
Dim i As Integer
For i = 0 To UBound(bytBytes)
Debug.Print i & ": " & bytBytes(i)
Next i
Erase bytBytes
End Sub
-
Apr 18th, 2008, 11:09 PM
#3
Re: string to byte help needed...
You can use the &H operator to convert a string representation to a byte value
Code:
Dim bytVal As Byte
Dim strHex As String
strHex = "FF"
bytVal = CByte("&H" & strHex)
Debug.Print bytVal
will display 255 in the immediate window
EDIT: My offering for your situation: (but DigiRev's is nicer)
Code:
Option Explicit
Dim bytArray(7) As Byte
Private Sub ReadHex(strFile As String)
Dim intFile As Integer
Dim strLine As String
Dim strHex As String
Dim intI As Integer
Dim intJ As Integer
intFile = FreeFile
Open strFile For Input As intFile
Do
intI = 1
intJ = 0
Line Input #intFile, strLine
Do
If Mid(strLine, intI, 1) <> " " Then
strHex = Mid(strLine, intI, 2)
bytArray(intJ) = CByte("&H" & strHex)
intJ = intJ + 1
intI = intI + 2
Else
intI = intI + 1
End If
Loop Until intI >= Len(strLine)
Loop Until EOF(intFile)
Close intFile
End Sub
Private Sub Command1_Click()
Dim intI As Integer
Call ReadHex("C:\Hextest.txt")
Debug.Print "Element", "Deciimal", "Hex"
For intI = 0 To UBound(bytArray)
Debug.Print intI, bytArray(intI), "("; Hex(bytArray(intI)); ")"
Next intI
End Sub
Last edited by Doogle; Apr 18th, 2008 at 11:15 PM.
-
Apr 18th, 2008, 11:13 PM
#4
Re: string to byte help needed...
Oh, so the Val() function is unnecessary...oops.
-
Apr 18th, 2008, 11:47 PM
#5
Re: string to byte help needed...
 Originally Posted by DigiRev
Oh, so the Val() function is unnecessary...oops. 
However it will be good with Val() if you are not sure strValues(l) is a valid Hex or not.
An alternative way to split one single line:
Code:
Dim sLine As String
Dim arTemp() As String
Dim myByteArr() As Byte
Dim i As Long, n As Long, x As Long
sLine = "1011 21FF FF00 00FF"
arTemp = Split(Trim(sLine), " ")
n = UBound(arTemp)
ReDim myByteArr(2 * n + 1)
For i = 0 To n
x = CLng("&H" & arTemp(i))
myByteArr(2 * i) = x \ 256
'-- or just in case x \ 256 >= 256, the extra bits will be cut off:
' myByteArr(2 * i) = (x \ 256) Mod 256
myByteArr(2 * i + 1) = x Mod 256
Next
'-- testing
For i = 0 To 2 * n + 1
Debug.Print myByteArr(i), Hex$(myByteArr(i))
Next
-
Apr 19th, 2008, 12:05 AM
#6
Thread Starter
Junior Member
-
Apr 19th, 2008, 12:48 AM
#7
Re: string to byte help needed...
Forgot to say you can remove the CopyMemory declaration as it's not needed.
If I didn't suck so bad at math I might be able to understand ahn's method.
-
Apr 19th, 2008, 01:18 AM
#8
Re: string to byte help needed...
 Originally Posted by DigiRev
Forgot to say you can remove the CopyMemory declaration as it's not needed.
Yeah! It took me 20 seconds to go through your code trying to find where you use CopyMemory.
At first I thought, geez, this guy love to use CopyMemory so much.
 Originally Posted by DigiRev
If I didn't suck so bad at math I might be able to understand ahn's method. 
With x in form of 4 Hex digits (16 bits):
x \ 256 : right-shift 8 bits (2^8 = 256), other word that is 8 left-most bits
x Mod 256 : remove 8 left-most bits, other word that is 8 right-most bits
eg.:
&H81A5 = 33189
&H81 = 129
&HA5 = 165
33189 = 129*256 + 165
Reverse calculation:
33189 \ 256 = 129 = &H81
33189 Mod 256 = 165 = &HA5
-
Apr 19th, 2008, 05:30 AM
#9
Re: string to byte help needed...
Here's my version different again
Code:
Public Function Hex2Bytes(ByRef HexString As String) As Byte()
Dim Buf() As Byte, i As Long, j As Long, LoBits As Boolean, Offset As Long
Buf = StrConv(UCase$(HexString), vbFromUnicode)
For i = 0 To UBound(Buf)
Select Case Buf(i)
Case 48 To 57: Offset = 48 '0-9
Case 65 To 70: Offset = 55 'A-F
Case 10, 13, 32: Offset = 0 'allow (but ignore) Carriage return, line feed, space (add more if required)
Case Else: j=0: Exit for 'bad string so exit
End Select
If Offset Then
If LoBits Then
Buf(j) = Buf(j) + (Buf(i) - Offset)
j = j + 1
Else
Buf(j) = (Buf(i) - Offset) * 16
End If
LoBits = Not LoBits
End If
Next i
if j then ReDim Preserve Buf(j - 1) else redim buf(-1 to -1)
Hex2Bytes = Buf
End Function
Last edited by Milk; Apr 19th, 2008 at 07:37 AM.
Reason: minor tweak for elegence
-
Apr 19th, 2008, 06:57 AM
#10
Re: string to byte help needed...
The OP will supprise. We are over killing this thread.
This is my other completed version that is a bit long but do all checking to make sure the file is in correct format and can be easy to understand.
I haven't done any test run. Comments are wellcome.
Code:
Function HexFileToBytes(sFilePath As String) As Byte()
'== Validation check of return array:
' If UBound(...) = 0 then there is error in text file
Dim sText As String
Dim arTemp() As String
Dim myByteArr() As Byte
Dim i As Long, n As Long, x As Long
Open sFilePath For Input As #1
sText = Input(LOF(1), #1)
Close #1
'-- replace all type of line breaks with spaces
sText = Replace(sText, vbCrLf, " ") '-- Text file may come from different
sText = Replace(sText, vbCr, " ") '-- platform and may use different
sText = Replace(sText, vbLf, " ") '-- linebreak characters
'-- trim and replace all double spaces with single space
sText = Trim(sText)
While InStr(sText, " ")
sText = Replace(sText, " ", " ")
Wend
'-- test to see any invalid character
If sText Like "*[! 0-9A-F]*" Then
ReDim myByteArr(0)
HexFileToBytes = myByteArr
Exit Function
End If
'-- split by spaces into array
arTemp = Split(sText, " ")
n = UBound(arTemp)
ReDim myByteArr(2 * n + 1)
For i = 0 To n
'-- an array item must have exactly 4 characters
If Len(arTemp(i)) <> 4 Then
ReDim myByteArr(0)
HexFileToBytes = myByteArr
Exit Function
End If
myByteArr(2 * i) = CByte("&H" & Left$(arTemp(i), 2))
myByteArr(2 * i + 1) = CByte("&H" & Right$(arTemp(i), 2))
Next
HexFileToBytes = myByteArr
'-- testing ------------------------------------
'For i = 0 To 2 * n + 1
' Debug.Print myByteArr(i), Hex$(myByteArr(i))
'Next
'-----------------------------------------------
End Function
-
Apr 19th, 2008, 02:23 PM
#11
Re: string to byte help needed...
See my post at the below thread. It does what you want but with a different format but you can work that part out.
http://www.vbforums.com/showthread.p...55#post3205655
-
Apr 19th, 2008, 08:46 PM
#12
Thread Starter
Junior Member
-
Apr 20th, 2008, 04:16 AM
#13
Re: string to byte help needed...
If you have a plain old ANSI text file, you won't find any 'funny characters' at the start. They indicate the format of the text file.
Code:
CHR HEX
ÿþ - &HFFFE - Unicode little endian
þÿ - &HFEFF - Unicode big endian
ï» - &HEFBB - UTF-8 (? not sure, but I think it's mixed Unicode/Ansi)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|