Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data conversion? Or just Simply stupid? HELP!! 1

Status
Not open for further replies.

wakkara

Programmer
Jun 15, 2005
31
0
6
MX
Hi there

I ve being stucked on this part of my program and i can get to work it should.

i have developed a Listenner with Winsock control and i have managed to get the data.

The problem is that i am receiving the data on a wrong type (At least I think).

I am supposed to receive HEX data and i am receving "STRING" data.


I am receiving
V%qˆÿÿÿ™
insted os some HEX numbers like 84 53 57 84.....

Is there a way to avoid VB to convert the data without asking and show it as it should.

SO? Is a data conversion, or is just another stupid programmer, or Both???


Help will make several Stars on your Account, and a extremely non professional programmer less SLOW and better programmer.
 
Let's start by posing a simple question:

How do you think a string is represented in your computer's memory?
 
It is represented as Hex i guess.

So you are saying that the String im reading is the string i am receiving?
 
You're being sent raw bytes. Use a Byte array with GetData.

Whatever information you have that shows hex values is just trying to represent the values in print. They didn't mean you'd get text containing sequences of hex values.
 
Thanks aaaa LOT dilettante you got 3 Stars for the great aproach.

In the end i couldnt understand the concept of raw bytes, as it should,
but it was great to understand that i could receive on the GetData method of the winsock control
a byte array, wich in fact was a really good aproach to make me understand how was the received data structured.

So to get closer to the answer i had to understand how does VB responde to raw bytes,
so i had to try with all the convertions i found on a google search.

And i finally got to this small function (posted in the end) that gave me the answer.

I found out that the raw bytes i was receiving were in diferent types (ASCII, HEX, DEC).
Thats the reason it was so demm difficult to know if the problem was VB convertion,
in fact VB was converting to a DEC and then to String from RAW BYTES without asking, asuming you sent them on a "String",

So those bytes that where sent as ascii were represented as it should(legible string),
and those that where sent as "HEX" where converted to a char that had nothing to do with the information received.

Exapmle: It was sent a 99 55 (That is "HEX" data, but VB asumes that is ascii and convert the data to string) and VB converted it to " ™U " thats because 99 55 where converted to DEC and then to STRING, with some help with a ASCII table.

This is how:


HEX DEC CHAR

H99 153 ™
H55 85 U

And all this trouble you could avoid it by "telling VB on the DataArrival of the Winsock" that you will be receiving on a byte type array.
The great Byte Array .

Then you decide what to do with bytes on the array.

Thats the reason I was reading "wierd data", the problem was .... just the way the data was sent.

Any way...

I had to convert the raw bytes to DEC and CHAR, so i could work with them later.


Public Function ByteArray2String(byteArray() As Byte)
Dim DecimalStr As String
Dim CharStr As String

'MsgBox UBound(byteArray)

For n = 0 To UBound(byteArray)

CharStr = CharStr & Chr(CStr(byteArray(n)))
DecimalStr = DecimalStr & StrConv(bytbyteArrayArray(n), vbUnicode)

Next n

End Function


I hope its usefulll fore someone.

Tnks Guys.

favicon.ico
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top