base64 encode characters > 127

C

Curt Fluegel

I seem to be having a problem base64 encoding characters above 127. I
can encode a sentence like "The big bad dog" without problems, but if
I try to encode something like 0xFF I get different results than in
Perl.

For example I am using:

byte[] binaryArray =
System.Text.ASCIIEncoding.ASCII.GetBytes(binaryString);
string pushString = System.Convert.ToBase64String(binaryArray);
MessageBox.Show(pushString);

For 0xFF I get the result: Pw== which doesn't match my results in
Perl.

If it helps, I am converting a HEX input from a string, like FFE0 etc
with the following:

for (int i=0;i<hexString.Length;i=i+2)
{
string tmpstr = (((char)hexString).ToString()+((char)hexString
[i+1]).ToString());
int hex = Convert.ToInt32(tmpstr,16);
binaryString += (char)hex;
}

I run into problems with ASCII.GetBytes()

It seems it takes my 255 for FF and turns it into 127

If I use Unicode instead I get 255, but it uses two spots in the byte
array, like

255 0

Essentially I need FFFFFFFF as an input to turn into a byte array of:

255 255 255 255 255 255 255 255

Any ideas?

Thanks,

Curt
 
J

Jon Skeet [C# MVP]

Curt Fluegel said:
Essentially I need FFFFFFFF as an input to turn into a byte array of:

255 255 255 255 255 255 255 255

Any ideas?

Yes - you're talking about *binary* data here, so don't try to treat it
as *character* data.

Create a byte array which is of an appropriate size, then populate it
by taking each pair of characters in turn and using Byte.Parse with the
number style of HexNumber.
 

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