base64 encoding - characters above 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
 
M

mikeb

Curt said:
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?


Instead of using the ASCII encodeing (which does, in fact, only return
values in the range 0-127), use the encoding returned by the
Encoding.Default() static method (which returns the ANSI encoding for
your machine setup).
 
C

Curt Fluegel

Ok,

Another question, what is the best way to turn a string of hex values like:

foo = "FFEEFF"

into something that can be base encoded?

My current code:
for (int i=0;i<hexString.Length;i=i+2)

{

string tmpstr =
(((char)hexString).ToString()+((char)hexString[i+1]).ToString());
int hex = Convert.ToInt16(tmpstr,16);
binaryString += (char)hex;

}

Essential the output of FFEEFF should be a string equivelent. The perl
encoder comes up with "ÿîÿ" and I am hoping to get the same!

Seems to barf on some characters, anything between 127 and 159 decimal to be
exact. They all are treated as invalid and turned in decimal 63. I swear I
am not doing anything that was supposed to be this hard :)

Thanks,

Curt



mikeb said:
Curt said:
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?


Instead of using the ASCII encodeing (which does, in fact, only return
values in the range 0-127), use the encoding returned by the
Encoding.Default() static method (which returns the ANSI encoding for
your machine setup).
 
J

Jon Skeet [C# MVP]

Essential the output of FFEEFF should be a string equivelent. The perl
encoder comes up with "?î?" and I am hoping to get the same!

Seems to barf on some characters, anything between 127 and 159 decimal tobe
exact. They all are treated as invalid and turned in decimal 63. I swear I
am not doing anything that was supposed to be this hard :)

My guess is that again, you're treating the characters as ASCII when
ASCII only has characters 0-127.
 
M

mikeb

Curt said:
Ok,

Another question, what is the best way to turn a string of hex values like:

foo = "FFEEFF"

into something that can be base encoded?

My current code:
for (int i=0;i<hexString.Length;i=i+2)

{

string tmpstr =
(((char)hexString).ToString()+((char)hexString[i+1]).ToString());
int hex = Convert.ToInt16(tmpstr,16);
binaryString += (char)hex;

}

Essential the output of FFEEFF should be a string equivelent. The perl
encoder comes up with "ÿîÿ" and I am hoping to get the same!

Seems to barf on some characters, anything between 127 and 159 decimal to be
exact. They all are treated as invalid and turned in decimal 63. I swear I
am not doing anything that was supposed to be this hard :)


If I understand what you want to do, you just need to convert the string
of hex characters into a byte array, then base 64 encode that. There's
no reason to convert your hex string into some other type of string first:

string hexstring = "FFEEFF";

System.Collections.ArrayList temp =
new System.Collections.ArrayList();

// note that the conversion being done in this loop
// will throw one of several expressions if hexstring is
// not well formed. I leave it up to you to add
// error handling appropriate to your situation
for (int i = 0; i < hexstring.Length; i += 2) {
string hexbyte = hexstring.Substring( i, 2);
byte b = Convert.ToByte( hexbyte, 16);
temp.Add( b);
}

byte [] convertedhex = new byte[temp.Count];
temp.CopyTo( convertedhex);

string base64encoded = System.Convert.ToBase64String(convertedhex);

Thanks,

Curt



Curt said:
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?


Instead of using the ASCII encodeing (which does, in fact, only return
values in the range 0-127), use the encoding returned by the
Encoding.Default() static method (which returns the ANSI encoding for
your machine setup).

 

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