Re: String parsing in VB.net
- From: "Larry Lard" <larrylard@xxxxxxxxxxx>
- Date: 5 Aug 2005 08:56:25 -0700
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
.
- Follow-Ups:
- Re: String parsing in VB.net
- From: Stargate4004
- Re: String parsing in VB.net
- References:
- String parsing in VB.net
- From: Stargate4004
- Re: String parsing in VB.net
- From: Larry Lard
- Re: String parsing in VB.net
- From: Stargate4004
- String parsing in VB.net
- Prev by Date: Re: modifying a textbox bound to datasource - rowstate always says UNCHANGED -full explanation
- Next by Date: Re: Windows Message Values
- Previous by thread: Re: String parsing in VB.net
- Next by thread: Re: String parsing in VB.net
- Index(es):
Relevant Pages
|
Loading