Byte Array to String - Encoding question

MJCM

Member
Joined
Jun 17, 2004
Messages
22
Location
SE-Asia
Programming Experience
1-3
I have the following piece of code to convert a Byte Array to a String,
but there is something going wrong with the encoding !!

VB.NET:
Sub Main()
Dim fs As FileStream
' file.bin is a binary file !
fs = New FileStream("d:\file.bin", FileMode.Open, FileAccess.Read)
 
Dim bytes(fs.Length) As Byte
 
Console.WriteLine(ConvertByteArrayToString(bytes))
 
End Sub
 
Private Function ConvertByteArrayToString(ByVal byteArray As Byte()) As String
Dim enc As Encoding = Encoding.ASCII
Dim enc1 As Encoding = Encoding.UTF7
Dim enc2 As Encoding = Encoding.UTF8
Dim enc3 As Encoding = Encoding.UTF32
Dim enc4 As Encoding = Encoding.BigEndianUnicode
Dim text As String = enc.GetString(byteArray)
Dim text1 As String = enc1.GetString(byteArray)
Dim text2 As String = enc2.GetString(byteArray)
Dim text3 As String = enc3.GetString(byteArray)
Dim text4 As String = enc4.GetString(byteArray)
Return text
End Function
After running the text (or text1 or text2 or text3 or even text4 string) is completly empty . What the am i missing here ??? Am i right that it has to do something with the encoding ? If so, what am I doing wrong ?

Thanks
Mjcm
 
Last edited by a moderator:
No, it's not the encoding, you simply haven't read any bytes from the stream.
VB.NET:
Sub Main()
  Dim fs As FileStream
  ' file.bin is a binary file !
  fs = New FileStream("d:\file.bin", FileMode.Open, FileAccess.Read)
  Dim bytes(fs.Length -1) As Byte
  fs.Read(bytes, 0, fs.Length)
  fs.Close()
  Console.WriteLine(ConvertByteArrayToString(bytes)) 
End Sub
 
Hi John,

thanks for the piece of code, but if i go through the debugger, the values of text (till text4) keep coming up empty. It prints the file on screen, but i want to do some search and replace on the file before i print it. And that is why i want the string.

If I add the following code to your example, it still keeps coming up as an empty string.

Code:

VB.NET:
Sub Main()
Dim textoutput as string
Dim fs As FileStream
' file.bin is a binary file !
fs = New FileStream("d:\file.bin", FileMode.Open, FileAccess.Read)
Dim bytes(fs.Length -1) As Byte
fs.Read(bytes, 0, fs.Length)
fs.Close()
output = ConvertByteArrayToString(bytes)
End Sub
Regards
MJCM
 
Last edited by a moderator:
Not sure if I understand you about this, but if you put a break-point on the line that says "Return text" you can read out all the variables text-4 that have been assigned before that line. If you stop on "Dim text As ...." you can't read the variable value because it hasn't been assigned the result of the encoding until debugger reaches next code line.
 
I debug with F8 (Step into).
If I stop at Return Text, text-4 are empty
If i stop at after output = ConvertByteArrayToString(bytes), output is also empty !

I have set text-4 and output in Watch (and they keep coming up empty)
 
JOHNH, i hope you can help me with this one ??

I tried it with another binary file (a normal zip file) and then text-4 comes up as PK(and then (8 bits) 3 4 10) and thats it.

The file i am trying the open starts with (8 Bits) 0 0 0 32 102 etc.etc.etc.
(But on this one, the text-4 keeps coming up empty.)

So what i would like is (maybe there is a better way) to have the whole file in a string.
 
Hi, if I do it this way, the sb.tostring, will fill up ith (0,0,0,32,166 etc etc etc), but i really want the ASCII values.

Dim sb As New System.Text.StringBuilder(bytes.Length * 4)
For Each b As Byte In bytes
sb.Append(b.ToString())
sb.Append(","c)
Console.WriteLine(sb.ToString)
Next
 
If you can't debug using the VS tools, you can always put in a MsgBox after a string variable has/should've been assigned a value.

I don't know what you're on about with this zip file, there is no plausible reason to display the bytes of a zip file as a string.
 
You cant do this. You cannot read a binary file into a string - why would you want to?

As soon as a binary 0 is encountered, In all encodings, it kills the string.

Youre saying your bytes are 0,0,32 ... well the first character is a 0 - a string terminator. any string with a string terminator as the first character is a string of length 0



for example paste this code:

VB.NET:
Imports system.io

Module Module1

    Sub Main()
        Dim fi As New FileInfo("c:\ntldr")

        Dim fs As FileStream = fi.OpenRead()

        Dim ar(1023) As Byte
        fs.Read(ar, 0, ar.Length)

        Dim x As String = New String(New System.Text.ASCIIEncoding().GetChars(ar))
        MsgBox(x)

    End Sub

End Module

(i hate the way this forum removes all the indent from my code, even when i use code tags.. who knows how to disable that? edit: ahhh, switch editor mode)

put a breakpoint on MSGBOX(x)

run the code - on windows xp you definitely have a file ntldr

now look at the variables in the LOCALS window

you see the byte array is full of 1023 bytes of data. the string length is 6.. why? because the 6th byte of ntldr is a 0. a string terminator..


Youre either working with strings, or youre working with bytes.. dont convert the 2! If you app reads bytes intoa byte array, they need to stay there while you work on them and write them some place else.. :)
 
Here is some sample code

Well here is a simple byte array to string snippet created by one of my co workers and I.
Should be pretty easy to understand.

Dim ascii As Encoding = Encoding.UTF8
Dim [unicode] As Encoding = Encoding.Unicode
Dim asciiChars(ascii.GetCharCount(input, 0, input.Length)) As Char
ascii.GetChars(input, 0, input.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)


If you want to change the encoding just pick what you want.

Ill be back to ask some questions have a nice day.
 
here's a version in code tags, not mangled up by the board software :)

VB.NET:
Imports System.Text
...
...
Dim ascii As Encoding = Encoding.ASCII
Dim asciiChars(ascii.GetCharCount(input, 0, input.Length)) As Char
ascii.GetChars(input, 0, input.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)

other points of note.. try and avoid using variable names like "unicode" as they are already identifiers.. it avoids the need to use [ brackets ] around them (which looks a little odd)

Incidentally, I changed your code to ascii = Encoding.Ascii, as utf8 isnt ascii, it's utf8 :)
 
Hi,
I believe what you are looking for is how to convert a string to a byte array and vice versa.

Here is from string to byte array

dim myString as string = "SomeText"
dim myBytes() as byte = Convert.FromBase64String(myString)


and here is from ByteArray to a string

myString = Convert.ToBase64String(myBytes)


I hope this answers your question.

Have fun
G.
 
Plain text is not the same as Base64 encoded string, gmacek! System.Text.Encoding provides methods to convert different types to/from byte array. But it was not the question anyway, it was asked how to view binary data that is not meant to view as plain text. So there needs to be some conversion of non-printable characters.
 
Will someone *please* tell me where the questions about reading MP3 into byte array were split out to?
 
cjard, you can look in your list of posts, click your profile name and 'find all posts by cjard' and they are listed newest posts first. The posts/posters splitted to other thread was notified duely and you should have gotten a mail notification about this somewhere yesterday.
 
Back
Top