954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Convert and saving a Byte array HEX values to binary file

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

waleed.makarem
Light Poster
27 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 


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.

Unhnd_Exception
Posting Pro
570 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

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

waleed.makarem
Light Poster
27 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 


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))
Next
Unhnd_Exception
Posting Pro
570 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

Unfortunately , I got the following error on line 8
"Could not find any recognizable digits."


Kindly advice.

waleed.makarem
Light Poster
27 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 


Post the first 8 bytes read from the data array.

Unhnd_Exception
Posting Pro
570 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

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,

waleed.makarem
Light Poster
27 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 


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

Unhnd_Exception
Posting Pro
570 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: