getting continuously hexadecimal and converting it to string

outcast1881

Active member
Joined
Aug 25, 2006
Messages
38
Programming Experience
Beginner
Hello eveyone,
I am getting continuously hexadecimal data from an external device to my textbox through tcp/ip connection and I have to convert it to string...I tried but I couldn't,i have to make a loop somehow...MY aim is substirng the data and save the useful data in SQL...so I use substring but it is hex so I can't get the data I want...

I am getting the data in my textbox like:

0x0000000000000000000000123babc0x111111111111222212112222cdab
0x0121313154555555555555555accd

my code:


VB.NET:
If networkStream.CanWrite And networkStream.CanRead Then
Dim sendBytes(TextBox1.Text.Length) AsByte
'Convert the contents of textbox1 to bytes for transmission
sendBytes = Encoding.ASCII.GetBytes(TextBox1.Text)
'send the contents of textbox1 to address specified by networkstream
Dim remainder AsInteger
DoWhile networkStream.CanRead
networkStream.Write(sendBytes, 0, sendBytes.Length)
 
Dim bytes(tcpClient.ReceiveBufferSize) AsByte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Output the data received from the host to the textbox.
Dim returndata AsString = Encoding.ASCII.GetString(bytes)
TextBox2.Text = returndata
 
'I can't get into this condition:confused: 
If TextBox2.Text IsNotNothingThen
remainder = CInt(TextBox2.TextLength) Mod 30
If remainder = 0 Then
For i AsInteger = 0 To TextBox2.Text.Length - 1 Step 30
temp1 = (TextBox2.Text.Substring(i, 30))
EPC = temp1.Substring(2, 18)
MAT_NUM = temp1.Substring(20, 6)
MessageBox.Show(temp1)
MessageBox.Show(EPC)
MessageBox.Show(MAT_NUM)
Next
EndIf
EndIf
 
TextBox2.Refresh()
Application.DoEvents() ' Process outstanding messages before sleeping
System.Threading.Thread.Sleep(1000)
Loop
Else
IfNot networkStream.CanRead Then
Console.WriteLine("cannot not write data to this stream")
tcpClient.Close()
Else
IfNot networkStream.CanWrite Then
Console.WriteLine("cannot read data from this stream")
tcpClient.Close()
EndIf
EndIf
EndIf
I hope someone can give me some ideas to solve it...

thanks&regards,

Can
 
Last edited by a moderator:
THis thread may answer your question:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21061244.html

The byte to char conversion should be handled by the GetString call already, but bear in mind your device might be returning more than just ASCII text (7 bit) - if it is returning 8 bit then you should use UTF8 - a hybrid unicode and asciiesque encoding that uses single byte semantics for 0-255 and multibyte sematics for the upper regions. It should be able to determine the stream content by examining the stream.

If youre still getting spurious results, let us know. It might be helpful for you to reduce the size of your buffer to e.g. 20 bytes, read the bytes in and convert them, then break execution with a breakpoint. type the following into the immediate window:
?bytes
?returndata

and paste us the result. I want to see the byte array content and the resulting string encoding
 
Hi Cjard,
thank you for your reply.The data I am getting is not hexa.Because I saved some some data on some tags and device read them.i could see my data in textbox,the device is adding 0x to the beginning and 4 characters to the end.so my data was as it is between them.like 0x1213245acde '121324 was what I had saved to the tag.

But i stil don't know why the characters I see are 30 but textbox.textlength gives me 32(for one tagdata(one single set of characters the device is sending after reading a tag) that i receive from the device)if the device sends 2 tagdata,then i see 60 characters but textbox.textlength=63,with 3 tagdata I see 90 characters but textbox.textlength=94 so it goes 32+31....+31 like that.

i am using trim function with the string(returndata) and with the textbox...
about testing what you offered I didn't mention in my program anywhere the buffer size,and didn't get how to use ??byte ??returndata commands,sorry I am not a programmer I 've been using vb.net since 70-80 days for my thesis:)


ok here is a screenshot,but not the way you explained sorry I didn't get it... it is the form I receive the data in my textbox2.,

thanks&regards
 

Attachments

  • unknown.JPG
    unknown.JPG
    22.5 KB · Views: 45
OK, so youre happy that the data coming back from the device will be a string like:

0x123456abcd

And youre confused that when you receive 30 characters of info from the device, and you put it into a text box, the length is reported as 32, rising by 31 each time the device sends some more data?

Its hard to tell you how to debug this, but i would still like you to follow the advice I gave. Ignore the part about setting the buffer size.. just put a breakpoint on the line TextBox2.Text = returndata

To add a breakpoint, click the white space to the LEFT of the line number. A red dot appears and the line is highlighted. When the program reaches this point, the code will halt.
At this moment you should ensure the Immediate Window is visible. If you cant see it you can enable it on the Debug menu
In the debug window type this and press return: ?bytes
then type ?returndata and press return. The immediate window will print some info about each variable contents..

Thats the data i need to see
 
Hi Cjard,

Thank you again for your interest on my problem.About character size I see 30 characters in my textbox on screen but textbox.textlength returns=32
(this was for one block of data,if I receive 1 block in string-like 0x223333abdc,when I receive more than one textboxtextlength increases by 31 but I the characters I see in my textbox are 30,60,90....)

ok I did what you had advised me:

?bytes
{Length=8193}
(0): 48
(1): 120
(2): 49
(3): 49
(4): 49
(5): 52
(6): 48
(7): 48
(8): 48
(9): 48
(10): 48
(11): 48
(12): 48
(13): 48
(14): 48
(15): 48
(16): 48
(17): 48
(18): 48
(19): 48
(20): 49
(21): 48
(22): 48
(23): 48
(24): 49
(25): 49
(26): 55
(27): 67
(28): 56
(29): 49
(30): 10
(31): 48
(32): 120
(33): 49
(34): 49
(35): 49
(36): 51
(37): 48
(38): 48
(39): 48
(40): 48
(41): 48
(42): 48
(43): 48
(44): 48
(45): 48
(46): 48
(47): 48
(48): 48
(49): 48
(50): 48
(51): 49
(52): 48
(53): 48
(54): 48
(55): 49
(56): 49
(57): 53
(58): 53
(59): 51
(60): 67
(61): 10
(62): 10
(63): 0
(64): 0
(65): 0
(66): 0
(67): 0
(68): 0
(69): 0
(70): 0
(71): 0
(72): 0
(73): 0
(74): 0
(75): 0
(76): 0
(77): 0
(78): 0
(79): 0
(80): 0
(81): 0
(82): 0
(83): 0
(84): 0
(85): 0
(86): 0
(87): 0
(88): 0
(89): 0
(90): 0
(91): 0
(92): 0
(93): 0
(94): 0
(95): 0
(96): 0
(97): 0
(98): 0
(99): 0
< Erweitert... (Die ersten 100 von 8193 Elementen werden angezeigt.) >

?returndata
"0x1114000000000000001000117C81
0x111300000000000000100011553C


I hope this gives you and idea so you can give me an idea:)

Regards,

CAn
 
Looking at the byte array, it starts 48, 120
that is the 0x

Looking for this, further down, we see it, but before the 48, we see a 10
10 is a line feed, or vbLf in old money

At the end of the message, we see another two 10s, obviously signifying the end of the transmission

These are your 3 missing characters - they may show uyp as boxes in a textbox that is not multiline, or they may force a new line in a multiline text box.

So, now the question remains what do you want with your data?
If you split by the char 10 you can get the individual lines of data as an array. it's down to you what you do with them after that?
 
Thank you for the info...I am trying to substiring the data to save different parts of the tags in SQL.I was using varables as defined like:

VB.NET:
For n AsInteger = 0 To TextBox3.Text.Length - 1 Step 32
 
temp1 = (TextBox3.Text.Substring(n, 32))
Tagid = temp1.Substring(1, 18) 
mat = temp1.Substring(19, 6)

But now I Think,I have read from x (0x121212.....) and then take 18 characters,then 6 characters smthg like that...I am checking in internet about how I can do it....


Regards,

Can
 
Last edited by a moderator:
hi Cjard,
I found how to do it but I couldn't put it in a loop...

VB.NET:
Dim pos1 AsInteger
pos1 = TextBox3.Text.IndexOf("x")
temp1 = (TextBox3.Text.Substring(pos1 + 1, 24))
MessageBox.Show(temp1)
Tagid = temp1.Substring(0, 18)
MessageBox.Show(Tagid)
MAT_NUM = temp1.Substring(18, 6)
MessageBox.Show(MAT_NUM)

do you have an idea about how I can put them in a loop so I can get all split the data in string starting from "x" to the next "x" and to the next "x" etc...I also tried split function but didn't work out...

regards
 
Last edited by a moderator:
I'd do this:

VB.NET:
strFromDevice = WhateverReadFromDevice()
 
'split on the char 10
Dim devThings as String() = strFromDevice.Split( New Char(){ Convert.ToChar(10) } )
For Each bit as String in devThings
  MessageBox.Show(bit)
Next bit

see if that helps you
 
Back
Top