views:

75

answers:

1

I have a byte array that I'm encoding to a string:

Private Function GetKey() As String
    Dim ba() As Byte = {&H47, &H43, &H44, &H53, &H79, &H73, &H74, &H65, &H6D, _
                        &H73, &H89, &HA, &H1, &H32, &H31, &H36}

    Dim strReturn As String = Encoding.ASCII.GetString(ba)

    Return strReturn
End Function

Then I write that to a file via IO.File.AppendAllText. If I open that file in 010 Editor (to view the binary data) it displays as this:

47 43 44 53 79 73 74 65 6D 73 3F 0A 01 32 31 36

The original byte array contained 89 at position 11, and the encoded string contains 3F. If I change my encoding to Encoding.Default.GetString, it gives me:

47 43 44 53 79 73 74 65 6D 73 E2 80 B0 0A 01 32 31 36

Any help would be much appreciated!

+2  A: 

Encoding.ASCII is limited to 7-bit characters. That is byte values from 0 to 127 (&H00 to &H7F). GetString sets all values outside this range to &H3F which is a questionmark.

Encoding.Default is the current ANSI code page for the operating system which on my computer is CodePage 1252..

The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For the most consistent results, applications should use Unicode, such as UTF-8 (code page 65001) or UTF-16, instead of a specific code page.

Encoding.UTF7 would work for you here:

Dim strReturn As String = Encoding.UTF7.GetString(ba)

Edit:

Instead of using Encoding I'd write the bytes directly using something like this:

Dim key = GetKey()
Dim f = System.IO.File.OpenWrite("output.txt")
f.Seek(0, SeekOrigin.End)
f.Write(key, 0, key.Length)
f.Close()

Private Function GetKey() As String
  Dim ba() As Byte = {&H47, &H43, &H44, &H53, &H79, &H73, &H74, &H65, &H6D, &H73, &H89, &HA, &H1, &H32, &H31, &H36}

  Return ba
End Function
Sani Huttunen
This gives me 47 43 44 53 79 73 74 65 6D 73 C2 89 0A 01 32 31 36Which has the 89, but also inserted a C2.I probably need to educate myself on encoding more.
Nick Spiers
Hmm... When writing it to the file with AppendAllText then it indeed adds C2. But why do you want to first convert the bytes to a string then write the string to a file? Why not write the bytes directly?
Sani Huttunen
I need the option of running the string through an encryption function before actually writing it to the file. Sometimes it will be encrypted, sometimes not. I COULD just remove the inserted C2 character after encoding it...
Nick Spiers
When encrypting you usually encrypt at byte level, not at string level so I'd still write the bytes themselves instead of the string.
Sani Huttunen
Got it all working, thanks Sani!
Nick Spiers