473,606 Members | 3,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ASC IIEncoding.ASCI I.GetBytes(bina ryString);
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)hexStri ng[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 3331
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.ASC IIEncoding.ASCI I.GetBytes(bina ryString);
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)hexStri ng[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.Defaul t() 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)hexStri ng[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******** ********@TK2MSF TNGP09.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.ASC IIEncoding.ASCI I.GetBytes(bina ryString);
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)hexStri ng[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.Defaul t() static method (which returns the ANSI encoding for
your machine setup).

--
mikeb

Nov 15 '05 #3
Curt Fluegel <cu**@thewebpra ctice.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.co m>
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)hexStri ng[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.Collecti ons.ArrayList temp =
new System.Collecti ons.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.Lengt h; i += 2) {
string hexbyte = hexstring.Subst ring( 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******** ********@TK2MSF TNGP09.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.A SCII.GetBytes(b inaryString);
string pushString = System.Convert. ToBase64String( binaryArray);
MessageBox.S how(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)hexStri ng[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

Essentiall y 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.Defa ult() 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
5390
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 ~30%, is this the drawback for using the encoding scheme? Thanks.
5
7567
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 as I know base64 data is not any known graphic format, from what I understand it's just encoded stream of bytes. Which I need to write to .dat file. Where to start? any links or suggestions? Thank you.
3
2294
by: nly | last post by:
What's the purpose of "Base64 encoding and decoding"? Thanks in advance!
3
4092
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 what I did wrong here? Thanks.
5
4109
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. Cold somebody check this for me to see what I have done wrong: If you run this program and enter a path for a .GIF file into the white box and hit <Enter> it should display the string in the blue box. To test this if you click the menu File...
1
4952
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 using an SHA-256 algorithm. There are 2 examples included with the specs. The label on the 2 examples are: 'HMAC samples'. In both examples, the message on which the digest is to be calculated is (the 33 chars within the quotes): 'This is a...
8
2796
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 the result should be "pass____ what the result is where each underline char is a nothing char. I am about to write a function that will remove the nothing chars butI would like to know what is causing the problem and simply avoid it
13
14751
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 send me the library file which helps that is really grateful. aruna.
10
4036
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 support for base64 encoding and decoding , but i will have to now implement both in C without using the openssl libraries . I was able to download a code w.r.t. base 64 encoding and decoding . I am attaching the code below .
0
7978
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8461
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8448
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8317
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5987
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5470
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2454
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1572
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1313
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.