Byte arrays to string WITHOUT encoding/decoding

J

Jeff Stewart

I wrote the following function to accomodate a legacy driver function
that only accepted VB6-style strings. The driver expects a string
that represents an array of binary data to be sent over USB.

Private Function byteArrayToString(ByVal byteArray() As Byte) As
String
Dim returnString As String = New String("")
Dim i As Integer
For i = 0 To byteArray.Length - 1
returnString += Chr(byteArray(i))
Next
Return returnString
End Function

I wrote this function because all of the functions I found in
System.Text.Encoding insisted on interpreting my byte array of binary
data as UTF8 or ASCII or Unicode or whatever, effectively returning a
string that differed from the byte array on a character-by-character
basis. Most often, bytes like 0xE6 would be converted to 0x06.

Isn't there a .NET function that will do what I did?
 
H

Hayato Iriumi

Hello Jeff,

System.Text.Encoding.ASCII(or Default maybe?).GetString(blah, blah);

Hope this helps.
 
C

Cor Ligthert

Jeff,

I do not know a VB6 style string, maybe you mean a MS-Dos code page format
byte array.

You can than try this one
\\\
Dim Str As New StreamReader(FilePath)
Dim arrInput As Byte() = _
System.Text.Encoding.GetEncoding(437).GetBytes(Str.ReadToEnd)
Str.Close()
///

This assumes that the codepage 437 is active in your country setting.

However a string in dotNet is forever a two byte unicode string.

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top