473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

handling binary data in vb.net

6
Dear all, I'm new to the forum, I'm really sorry if this has appeared elsewhere. I'm not really a programmer and am having major difficulties handling a binary stream which I am trying to read and process in vb.net.

The stream is coming in from an FTDI chip and should contain 64 8-bit values.

An example of the stream is here
ٶ

I need to get this into something that I can read (decimal or hex or 1s and 0s) in vb and I just don't know what to do - I tried using Asc() but it wasn't working and I know now that it can't work with characters higher than 127.

I am running really short of time and am desperate - any help will be massively appreciated!

Thanks very much, Ken
Aug 13 '09 #1
13 8970
tlhintoq
3,525 Expert 2GB
How about hex?
http://msdn.microsoft.com/en-us/library/bb311038.aspx
Aug 13 '09 #2
kboyd
6
Thanks very much but I've managed to get it working converting these characters to 1s and 0s - in principle this is fine for me.

I'm using this function:

Expand|Select|Wrap|Line Numbers
  1. Public Function TextToBinary(ByVal Text As String, Optional ByVal Separator As String = "  ") As String
  2.         MsgBox(Len(Text))
  3.         Dim oReturn As New StringBuilder
  4.         Dim count As Integer
  5.         count = 0
  6.         oReturn.Remove(0, oReturn.Length)
  7.  
  8.         ' Convert all the bytes
  9.         For Each Character As Byte In UTF8Encoding.UTF8.GetBytes(Text)
  10.             count = count + 1
  11.             oReturn.Append(Convert.ToString(Character, 2).PadLeft(8, "0"))
  12.             oReturn.Append(Separator)
  13.         Next
  14.  
  15.         MsgBox(count)
  16.         Return oReturn.ToString
  17.     End Function
The only problem is that I'm getting more bytes once converted than what I shoudl have - the reading coming in has 64 pieces of data and yet I seem to be getting on average about 120 once I've translated them. Even the input string doesn't seem to always be the same length although it always has fewer characters than what I'm outputting.

The input string of characters is:
ִ
~v~ * (which is 63 characters)

The output in binary is:
11000011 10010110 11000010 10110100 11000011 10111111 11000011 10111111 11000011 10111111 11000011 10111111 11000011 10111111 11000011 10111111 11000011 10111111 11000011 10111111 00001101 11000011 10101000 11000011 10110110 11000011 10111111 11000011 10111111 11100010 10000000 10010100 11000010 10111000 11000011 10100000 11000011 10111111 01111110 11100010 10000000 10011001 11000011 10001011 11000011 10111111 11100010 10000000 10011010 11001011 10011100 11000011 10010111 11000011 10111111 11000011 10111111 11000011 10111111 11000010 10101111 11000101 10111110 11000011 10111111 11000011 10111111 11000011 10110111 11000011 10001000 11000011 10111111 11000011 10111111 11000011 10111111 11000011 10111111 11000011 10111111 11000011 10111111 11000011 10111111 11000011 10111111 11000011 10110000 11000011 10111111 11000011 10111111 11000011 10111111 11100010 10000000 10100010 11100010 10000000 10100010 11000011 10100010 11000011 10111111 01110110 11100010 10000000 10011001 11000011 10011110 11000011 10111111 01111110 11100010 10000000 10011100 11000011 10001001 11000011 10111111 11000011 10111111 11000011 10101001 11000010 10100000 00100000 (which is 128 bytes)

Does anyone have any ideas - I'm just really struggling here!

Thanks, Ken
Aug 13 '09 #3
tlhintoq
3,525 Expert 2GB
Does anyone have any ideas - I'm just really struggling here!
The input string of characters is:
You can't work it from a string. All it takes is for some of your "characters" to be the "backspace" or "delete" and your are screwed.
You need to read the chip as a series of bytes (8 bits is a byte) and just work with bytes. I do the same thing with RFID wristbands.
Aug 13 '09 #4
kboyd
6
Thanks very much once again - ok, this sounds good - can you give me a pointer about how to do this - I'm a bit out of my depth here! I really appreciate your help. Thanks, Ken
Aug 13 '09 #5
tlhintoq
3,525 Expert 2GB
I don't do VB. I code in C# so I can't give you much for code.
But let's start with you showing me how you are reading the file. The code snippet above is just about converting text once you have it read the file.
Aug 13 '09 #6
kboyd
6
ok, I just wrote a reply and then it logged me out - so here goes again!!
Thanks so much for your help.

I have realised that the functions I am using to read the string in are clearly not workign although they are FTDI examples that I have adapted. Here they are:

Expand|Select|Wrap|Line Numbers
  1.  '//Write string data to device
  2.         Dim sendd As String
  3.         sendd = TextBox4.Text + vbCr 'must add a line feed at the end of this
  4.         FT_Status = FT_Write_String(FT_Handle, sendd, Len(sendd), BytesWritten)
  5.         chksend.Checked = True
  6.         If FT_Status <> FT_OK Then
  7.             Exit Sub
  8.         End If
  9.  
  10.         '//Wait()
  11.         Sleep(100)
  12.  
  13.         ' //Get number of bytes waiting to be read
  14.         FT_Status = FT_GetQueueStatus(FT_Handle, FT_RxQ_Bytes)
  15.         If FT_Status <> FT_OK Then
  16.             Exit Sub
  17.         End If
  18.  
  19.  
  20.         ' //Read number of bytes waiting
  21.         ' //Allocate string to recieve data
  22.         TempStringData = Space(FT_RxQ_Bytes + 1)
  23.         MsgBox(FT_RxQ_Bytes)
  24.         FT_Status = FT_Read_String(FT_Handle, TempStringData, FT_RxQ_Bytes, BytesRead)
  25.         If FT_Status <> FT_OK Then
  26.             Exit Sub
  27.         End If
That msgbox is showing that the amount of data it reckons is there - is coming in but when I poll this com port in hyperterminal I am getting 64 bytes every time, so there is clearly something wrong there.

Incidentally, I tried encoding in unicode and it seems almost right although it is adding a byte of zeros in between each character - not a big deal though!

What do you reckon?

Thanks for your help again!
Aug 13 '09 #7
tlhintoq
3,525 Expert 2GB
I have realised that the functions I am using to read the string in are clearly not workign
Well... That's seems like a good place to start. Garbage in, garbage out as the old saying goes.
although they are FTDI examples that I have adapted.
Expand|Select|Wrap|Line Numbers
  1.  '//Write string data to device
This looks like a C# comment, that you turned into a VB comment.
Are the FTDI examples in C#?
Aug 13 '09 #8
kboyd
6
These comments and all code came straight from FTDI - I guess they may have just been lazy and copied them over each time!

Ok, I've made some progress - I realised that I wasn't letting it wait long enough between write and read! Now, I have it producing reliably 64 bytes of data and the carriage return.

Using the unicode encoding, I think the conversion is almost right, but it puts a spare byte in between each one - most of the time this is 00000000 but sometimes it is something else.

Here is the code that I am using in its present state:
For Each Character As Byte In UnicodeEncoding.Unicode.GetBytes(Text)
count = count + 1
oReturn.Append(Convert.ToString(Character, 2).PadLeft(8, "0"))
oReturn.Append(Separator)
Next

Here is an example of the output:
11000110 00000010 00011100 00100000 10110110 00000000 11111111 00000000 01011010 00000000 01110000 00000000 01100000 00000001 11100000 00000000 01001110 00000000 01011100 00000000 10000001 00000000 11101000 00000000 01010100 00000000 01100011 00000000 01010010 00000001 11111011 00000000 11111111 00000000 10101100 00000000 01110110 00000000 01101010 00000000 11111111 00000000 11001001 00000000 10101001 00000000 00110000 00100000 11111111 00000000 11111111 00000000 11111111 00000000 11011100 00000000 11111111 00000000 11111111 00000000 11111111 00000000 11111111 00000000 10001101 00000000 11011100 00000010 11001101 00000000 11111111 00000000 01011010 00000000 01011011 00000000 11000110 00000010 11011110 00000000 01001001 00000000 01011010 00000000 00100001 00100000 11011001 00000000 01010010 00000000 01011110 00000000 01111111 00000000 11011010 00000000 11110000 00000000 00010011 00100000 01101001 00000000 01100001 00000000 01100001 00000000 10101110 00000000 11011100 00000010 10000001 00000000 11111111 00000000 11111111 00000000 11011011 00000000 11101100 00000000 11111111 00000000 11111111 00000000 11111111 00000000 11111111 00000000 00001101 00000000 00100000 00000000

Does this make any sense to you?
Thanks again, finally some progress!!! Ken
Aug 13 '09 #9
tlhintoq
3,525 Expert 2GB
Does this make any sense to you?
Doing all of this conversion to text, then to a string representation of binary doesn't make sense to me. I see no reason to introduce all these places where something gets translated and you are reliant on someone else's code.

Using the unicode encoding, I think the conversion is almost right, but it puts a spare byte in between each one
Of course it does. Unicode is a two byte encoding scheme for text. It's how we have these huge alphabets that can read from right to left, or Japanese Kanji. You use two bytes to represent a letter.

You said yourself that you only have 64 bytes of data. Just read in 64 bytes from the reader. In this example when the serial port ("MyPort") raises an event that data has been received we read all the bytes into a byte array ("ReadBytes"). No conversion. Throughout the application I only ever work with the bytes. Each byte has a value 0-255 which can be represented however you like: As a decimal number... has a binary string... has a hex... That's up to you. But I don't tamper with the actual bytes that were read. Make copies in other representations if you like, but don't be destructive with the original data.

Expand|Select|Wrap|Line Numbers
  1.         void MyPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  2.         {
  3.             MyPort.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(MyPort_DataReceived);
  4.             // We don't need to respond while we are still processing
  5.  
  6.             try
  7.             {
  8.                 int ByteCount = MyPort.BytesToRead;
  9.                 byte[] ReadBytes = new byte[ByteCount];
  10.                 for (int Index = 0; Index < ByteCount; Index++)
  11.                 {
  12.                     ReadBytes[Index] = (byte)MyPort.ReadByte();
  13.                 }
  14.                 BytesReadIn = ReadBytes;// BytesReadIn is a global that gets set to the local ReadBytes
  15.             }
  16.             catch (TimeoutException) { }
  17. }
  18.  
Aug 13 '09 #10
tlhintoq
3,525 Expert 2GB
An example of the stream is here
ŽŒ‚ …™ٶ
No. This is not an example of the byte stream. It is an attempt to translate the bytes into a human readable alphabet. The data itself is a bunch of bytes from 0-255
Aug 13 '09 #11
kboyd
6
tlhintoq, thank you so much. As I explained before, I am a bit out of my depth here, but clearly reading it in as a string was the wrong way to go - it is changed to bytes now and as you said, there are no conversion errors - so clearly that was my problem all along! I now have 64 beautiful bits of data coming in reliably and giving the correct readings!

Thank you for all your help yesterday, it is really really appreciated! Now to tidy up and make it all look pretty!!! Cheers, Ken
Aug 14 '09 #12
tlhintoq
3,525 Expert 2GB
Glad it all worked out for you. Don't be a stranger... just be stranger than most
Aug 14 '09 #13
raids51
59
If you're getting it from a stream into a byte array it is fairly easy, just use hex to read it. I'll give you a short little tutorial if you want to read it lol. Basically with binary there's a few things you might want to do with it:

1. Get the value of individual bytes
This is useful if you want the value of for example byte 16. If you would want to check if byte 16 = FF in hex then you could use
Expand|Select|Wrap|Line Numbers
  1. If Bytes(16) = "&HFF" then
  2. Msgbox("Byte 16 = FF")
  3. end if
  4.  
Where bytes() is your byte array from the stream and 16 is the byte

2. Convert a byte array into ASCII text
This is useful if you want to convert the bytes from the stream into a byte array and then a steam.
you could use System.text.encoding.ascii.getstrings(ByteArrray)

3. Get an integer value from a byte array
This is used to read numbers from a stream.
Dim Number as integer
Number = bitconverter.toint32(ByteArray,0)
This code would read the first 4 bytes from the byte array and convert it into an integer for you. You could also do the reverse with bitconverters.

I have quite a bit of experience with binary in vb.net, but i suck at explaining some things lol. So if you need more help or more info about anything binary in vb.net you can message me and ill try my best to help you. Happy coding ;)
Aug 18 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Bill Loren | last post by:
Hello ppl, I'm having difficulties to accomplish some simple chores with binary data. I'm having a string (?) which I received via an HTTP transactions which is a binary file. Problem is the...
3
by: Derfel | last post by:
Hello all, I have a file of binary data. I want to read this file and parse the data in it. The format of the file is: 8 bits number of data pakets 6 bits offset 20 bits time 8 states How...
7
by: Matthias Czapla | last post by:
Hi! Whats the canonical way for handling raw data. I want to read a file without making any assumption about its structure and store portions of it in memory and compare ranges with constant...
0
by: David List | last post by:
I am wondering what I miss to be able to handle binary data from the mysql client. I have ensured that the user has file_priv set to 'Y' and that max_allowed_packet is larger that the binary lumps...
5
by: Chathu | last post by:
Hello everyone........... I have a problem on retriving a content of a binary file I wrote into. My program user structures, dynamic allocation of memory and files. I take the infomation into a...
2
by: Feng | last post by:
Hi, I need a VB.Net function that reads in a stream of binary data coming in from a legacy data source. The data are actually hex numbers in binary format but the problem is that I don't know...
2
by: CharlesL | last post by:
I am trying to handle binary strings in php. I get a binary output initialization vector from mcrypt as such: from mcrypt: $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); This output may have...
0
by: perlprod | last post by:
Hi, I want to generate a data file that is to be read by a 'C' program. But I got in dilemma after checking the file generated by the following program. $buffer = ""; $path = "C:/Documents...
9
by: lokeshrajoria | last post by:
hello everybody, i need some help in binary file handling in perl. can anybody give me some information about binary file. actully i am reading data from some text file and extracting some usefull...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.