By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,669 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,669 IT Pros & Developers. It's quick & easy.

base64 encoding - characters above 127

P: n/a
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(binaryStr ing);
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[i]).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
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Curt Fluegel wrote:
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(binaryStr ing);
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[i]).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).

--
mikeb
Nov 15 '05 #2

P: n/a
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[i]).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" <ma************@mailnull.com> wrote in message
news:eF****************@TK2MSFTNGP09.phx.gbl...
Curt Fluegel wrote:
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(binaryStr ing);
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[i]).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).

--
mikeb

Nov 15 '05 #3

P: n/a
Curt Fluegel <cu**@thewebpractice.com> wrote:

<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

P: n/a
Curt Fluegel wrote:
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[i]).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

"mikeb" <ma************@mailnull.com> wrote in message
news:eF****************@TK2MSFTNGP09.phx.gbl...
Curt Fluegel wrote:
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(binary String);
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[i]).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).

--
mikeb



--
mikeb

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.