Reliable conversion from byte() to string WITHOUT use of System.Text.Encoding

  • Thread starter Thread starter I.Charitopoulos
  • Start date Start date
I

I.Charitopoulos

The reason I want to do so, is that I am sending to DOS and I am pretty
certain that it will not work.
Everything I've tried so far hasnt.

In my test environment (Windows to Windows) this works perfectly, but not
when sending to DOS:

Private Function bytearray2string(ByVal input As Byte()) As String

Dim output As String

output = System.Text.Encoding.Default.GetString(input)

Return output

End Function



Private Function string2bytearray(ByVal input As String) As Byte()

Dim output() As Byte

output = System.Text.Encoding.Default.GetBytes(input)

Return output

End Function



I have also tried this, to no avail in Windows:

Private Function bytearray2string2(ByVal input As Byte()) As String

Dim output As String = ""

Dim i As Integer = 0

'Dim vchar As Char

While i < input.Length

output = output & CChar(Chr(input.GetValue(i)))

i = i + 1

End While

Return (output)

End Function



Private Function string2bytearray2(ByVal input As String) As Byte()

Dim output(input.Length - 1) As Byte

Dim i As Integer = 0

For i = 0 To UBound(output)

output(i) = CByte(Asc(input.Chars(i)))

Next

Return (output)

End Function

The desired functions need to use no encoding and be able to work with
simple bytes. The values I'll be receiving wont be higher anyway.

I'd appreciate any feedback whatsoever.

regards,
J
 
Why not use ASCII encoding, instead of 'Default'

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit
 
ASCII Encoding uses 7 bits out of the byte, so I'd be losing 1 bit out of
every byte of data that comes in.

I want to use the extended ASCII that uses all 8 bits, for values of 0 to
255.
 
I.Charitopoulos said:
The reason I want to do so, is that I am sending to DOS and I am
pretty certain that it will not work.
Everything I've tried so far hasnt.

In my test environment (Windows to Windows) this works perfectly, but
not when sending to DOS:
[...]

Maybe codepage 850 is what you're looking for.

System.Text.Encoding.GetEncoding(850)

Use it to convert from/to byte arrays. There are other codepages

See also:
http://www.microsoft.com/globaldev/reference/cphome.mspx

To find the right encoding, open a DOS window and enter
mode con cp<enter>

Should return the encoding you need (but I'm not sure)
 
Thanx a lot for the tip. Will try that.

Seems like a reliable way of getting the encoding.
 
Back
Top