Re: String parsing in VB.net




Stargate4004 wrote:
> OK, it looks like I could be missing a trick here. I did this to get my
> buffer into a byte array:
>
> Dim asciiEncoder As New System.Text.ASCIIEncoding
> Dim bDataBuffer As Byte() = asciiEncoder.GetBytes(myDataBuffer)

The root of the problem is that you are treating 'pure binary' data as
characters. If it's not too much work :) I recommend you try and
refactor your code so that myDataBuffer is an array of Byte, rather
than a string. Earlier you said:

>
The application connects to a TCP port on a Linux box and
receives a data buffer.
>

Is this your code? I appreciate it might not be, but if it is you
should try and make it so that you receive a Byte() not a String or a
Char().

If that isn't possible:

> But instead of getting 684229409 (which I know was sent to me), I got
> 675808033. This is because the four characters in myDataBuffer which
> make up the integer field are 33, 131, 200 and 40, but when the string
> is converted to SIGNED byte array they become 33, 3, 72 and 40.

Actually, Byte is unsigned.

>
> Either asciiEncoder.GetBytes() or BitConverter.ToInt32 is treating the
> bytes as signed. How can I stop this?

It's ASCIIEncoding that is doing this. Although we commonly refer to a
familiar mapping between the numbers 0-255 and a certain list of
characters as 'ASCII', the actual fact is that ASCII is a *7* bit
encoding - it is only defined for 0-127. Anything beyond that depends
on a whole host of things. Because this encoding only looks at the
bottom 7 bits, it maps 131 (binary 1000 0011) to 3 (binary 000 0011) -
ie values over 127 get 128 subtracted from them.

The fix?

You might be able to get away with just changing to using
System.Text.Encoding.Default, which uses your system's default *ANSI
code page* encoding (ANSI code pages DO cover the full range 0-255).
But this relies on whoever converts the bytes to a string in the first
place also using that same code page. Which they probably will. But
maybe not. So really this is why you want to get bytes back from the
TCP communication, not a string :)

--
Larry Lard
Replies to group please

.



Relevant Pages

  • Re: Data type byte
    ... attempt to change the input buffer to a string (although not by changing the ... With multi dimensional array that can hold ... Also the receiving ... dimensional Byte Array is just one long string of memory. ...
    (microsoft.public.vb.general.discussion)
  • Re: vb and msxml - encoding conversion is not working
    ... data in utf-8 format if that's your selected encoding ... I did the above some time ago using asp and msxml, ... I loaded the vb string into a vb byte array using chrb on odd ...
    (microsoft.public.vb.general.discussion)
  • Re: from array of ints to string and vice versa
    ... I want to convert this string to an array, ... other to generate it, separating them with ", ". ... takes a buffer and returns a size, which you first call with a NULL pointer ...
    (comp.programming)
  • Re: Question about astring.getBytes() and a byteArray.toString()
    ... > array and be different ... String contains. ... Every transition between byteand String makes use of an encoding, ... The only really certain way is to NOT do a "string conversion" at all. ...
    (comp.lang.java.programmer)
  • Re: pls help: very odd behaviour when concatenating strings
    ... More then likely you are having a problem because your byte array is ... array to a string. ... // Put some stuff in the buffer. ... > concatenate them some values are missing (I canīt see them in the debugger). ...
    (microsoft.public.dotnet.languages.csharp)

Loading