Encoding problem

B

bmth

Hi all

I am trying to contvert a message from my server to unicode. I
initially got a base64 endoced byte[] message. After some debugging I
saw the base64 string had "\0" in the end wich resulted in an error
when converting from base64. So I cut it away and can now see my
string in byte[]. I tried calling Encoding.Unicode.GetString() and
sent my byte[] as param. The string I get contains only boxes. My
byte array seems ok, so I was wondering if the GetString method
expects some end token ?
Any suggestions ?
bixx
 
N

Nicholas Paldino [.NET/C# MVP]

bixx,

Did you peform the decoding from base64 into a byte array before you ran
it through the unicode encoding?
 
J

Jon Skeet [C# MVP]

bmth said:
I am trying to contvert a message from my server to unicode. I
initially got a base64 endoced byte[] message. After some debugging I
saw the base64 string had "\0" in the end wich resulted in an error
when converting from base64. So I cut it away and can now see my
string in byte[]. I tried calling Encoding.Unicode.GetString() and
sent my byte[] as param. The string I get contains only boxes. My
byte array seems ok, so I was wondering if the GetString method
expects some end token ?

No, chances are you don't actually have Unicode-encoded characters in
your byte array.

Is the original data text data or binary? If it's text, why is it
encoded in Base64 in the first place?
 
B

bmth thorgeirsson

yes I had already decoded the string succesfully. The server sends a
byte[] message representing a base64 encoded string. It is base64
because it is part of basic authentication scheme. The server is sending
username and password. Below is the content of my byte array after
having decoded it from base64, it should represent "birgirr:password".
Here is my scenario:

1. I first get the string from the message using
Encoding.Unicode.GetString(byte[] serverMessage)
2. Then I have a base64 string in my hands. I had to cut the last two
charachters away ("\0") to have it legal base64. I guess the "\0" has
something to do with windows codepage.
3. I tested the base64 string to see if my editing had not currupted
it. All good, I could decode the base64 string successfully using
3rd-party tools. Now I have the byte[] below.
I could work around this if I could manually create a string by entering
the charachter set value.
My array seems to represent my string so I cant really see where I am
going wrong.

[0] 98 byte
[1] 105 byte
[2] 114 byte
[3] 103 byte
[4] 105 byte
[5] 114 byte
[6] 58 byte
[7] 112 byte
[8] 97 byte
[9] 115 byte
[10] 115 byte
[11] 119 byte
[12] 111 byte
[13] 114 byte
[14] 100 byte
 
J

Jon Skeet [C# MVP]

bmth thorgeirsson said:
yes I had already decoded the string succesfully. The server sends a
byte[] message representing a base64 encoded string. It is base64
because it is part of basic authentication scheme. The server is sending
username and password. Below is the content of my byte array after
having decoded it from base64, it should represent "birgirr:password".

In that case it's most definitely not appropriate to use
Encoding.Unicode in your third step.
Here is my scenario:

1. I first get the string from the message using
Encoding.Unicode.GetString(byte[] serverMessage)

Are you *absolutely* sure that the server message is sent in Unicode to
start with? It sounds unusual to me, but
2. Then I have a base64 string in my hands. I had to cut the last two
charachters away ("\0") to have it legal base64. I guess the "\0" has
something to do with windows codepage.

What's creating the base64 string to start with? Chances are that's
doing something it shouldn't.
3. I tested the base64 string to see if my editing had not currupted
it. All good, I could decode the base64 string successfully using
3rd-party tools. Now I have the byte[] below.
I could work around this if I could manually create a string by entering
the charachter set value.
My array seems to represent my string so I cant really see where I am
going wrong.

By using Encoding.Unicode, which is expecting 2 bytes per character.
Chances are you should use Encoding.ASCII or Encoding.Default, or
possibly Encoding.UTF8 - but it really depends on what the program
which created the Base64 string in the first place used.
 
B

bmth thorgeirsson

"By using Encoding.Unicode, which is expecting 2 bytes per character"

Thanks alot, it works fine with ASCII. The examples wich I have been
reading have always used Unicode. You have been a great help. I
appreciate it :)

bixx
 

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