Dear All ,
I have a device with upon serial communication , it send the data as HEX values , (eg, C020042ABD0F91A103E400F929EBC) . I use the following code to get data from the serial port.
Dim fStream As New FileStream(sFileName, FileMode.CreateNew) ' creates new file
Dim bw As New BinaryWriter(fStream)
System.Threading.Thread.Sleep(1500)
ComTd.Read(data, 1, bCount)
Dim bCount As Integer = 4119 ' it is the size of the chunk not block size
Dim data(bCount) As Byte
bw.Write(data) ' This saves the byte array to a binary file . The problem is that the stored data are in hex not decimal . I want to covert this HEX data in the byte array to a decimal to save into binray file .
Thanks,
WSN
now
You can use the Convert.ToInt32 function to get the numeric
dim int as integer = Convert.ToInt32("AF3",16)
If I knew more about the format of the serial data I could give you a better example.
Dear Unhnd ,
Thanks for your fast response , but the problem is that I had the bytes in an array. I need to convert each 2 bytes into one HEX . This is because I receive from the device through serial communcation data that are in HEX mode (eg C020042ABD0F91A103E400F929EBC) . Upon receive data from serial port into Byte , the data are transformed into ASCII code for each letter , eg , "C0" is a HEX number , however , the byte read it as C [and converts it to 67 which is the ASCII code for C] and the 0 [and converts it to 48] . I need to first read each 2 bytes [ie C0] , and convert them to the decimal part [which is 192] , thus when it is saved to binary file , it is read correctly .
Thanks,
WSN
waleed.makarem @ gmail.com
See if this helps you
Dim fStream As New System.IO.FileStream("c:\asfas", IO.FileMode.Create)
Dim bWriter As New System.IO.BinaryWriter(fStream)
Dim data(4119) As Byte
For i = 0 To UBound(data) Step 2
bWriter.Write(Convert.ToInt32(CStr(Convert.ToChar(data(i))) & CStr(Convert.ToChar(data(i + 1))), 16))
NextUnfortunately , I got the following error on line 8
"Could not find any recognizable digits."
Kindly advice.
I have a screen shot with everything , kindly find in the attached 4shared link .
data are 0 0 0 0 0 0 0 0 0 0 36 80
link is :
http://www.4shared.com/photo/H-X3xxrV/error_byte.html
Thanks,
I can't help you any more.
Don't know if the packet has a header on it or not but cant get 36 or 80 into any recogizable hex value.
All I know is that
Dim dec As Integer = Convert.ToInt32(CStr(Convert.ToChar(67)) & CStr(Convert.ToChar(48)), 16)
Will give you your 192