473,411 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

base64 encoding - characters above 127

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
4 3305
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: wenmang | last post by:
Hi, I ma thinking whether to use Base64 encoding to encode the binary content in the XML file. I have done some simple calculations, it seems to me that the size for encoded content increases by...
5
by: LP | last post by:
A web service returns base64 encoded data. The goal is to parse it and store it into binary file with .dat extension. This file is then will be used by a custom program to produce diagrams. As far...
3
by: nly | last post by:
What's the purpose of "Base64 encoding and decoding"? Thanks in advance!
3
by: Guoqi Zheng | last post by:
Dear sir, I need to decode base64 encoded email. I used below function but it does not work correctly, especially when I need to decode some Characters like Chinese, Can some one point out...
5
by: Jay | last post by:
I have bean trying to get my head around reading .GIF files from base64 strings, Basically I need to specify a filename and convert it to base64 then I can copy/past the string to wear I want it....
1
by: mirandacascade | last post by:
I am attempting to implement a process, and I'm pretty sure that a major roadblock is that I do not understand the nomenclature. The specs indicate that the goal is to calculate a message digest...
8
by: Jeremy Kitchen | last post by:
I have encoded a string into Base64 for the purpose of encryption. I then later decrypted it and converted it back from Base64 the final string returns with four nothing characters. "pass" what...
13
by: aruna.eies.eng | last post by:
i am currently trying to convert data into binary data.for that i need to know how to achieve it in c language and what are the libraries that we can use. so if any one can send me a sample code or...
10
by: pycraze | last post by:
Hi , I am currently trying to implement base64 encoding and decoding scheme in C . Python has a module , base64 , that will do the encoding and decoding with ease . I am aware of OpenSSL having...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.