473,386 Members | 1,766 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,386 software developers and data experts.

FromBase64Transform truncating string

Hi all,

I am simply trying to do a transform to a base 64 string and back, but for
some reason, the FromBase64Transform is return less bytes than it should. I
have verified that the base64 string is the same length as the one that was
output from ToBase64Transform. I also turned on DoNotIgnoreWhitespaces.
Any ideas?? Here is the code:

// ToBase64
ASCIIEncoding enc = new ASCIIEncoding();
byte[] valBytes = enc.GetBytes(val);
MemoryStream mstr = new MemoryStream();

CryptoStream str = new CryptoStream(mstr,new
ToBase64Transform(),CryptoStreamMode.Write);
str.Write(valBytes,0,valBytes.Length);

byte[] buf = mstr.ToArray();
return Convert.ToBase64String(buf);

// From Base64
byte[] valBytes = Convert.FromBase64String(val);
MemoryStream mstr = new MemoryStream();

CryptoStream str = new CryptoStream(mstr,new
FromBase64Transform(FromBase64TransformMode.DoNotI gnoreWhiteSpaces),CryptoSt
reamMode.Write);
str.Write(valBytes,0,valBytes.Length);

byte[] buf = mstr.ToArray();
ASCIIEncoding enc = new ASCIIEncoding();
return enc.GetString(buf);
Nov 15 '05 #1
2 5054
On Thu, 5 Feb 2004 12:25:26 -0600, Cole Shelton wrote:
Hi all, I am simply trying to do a transform to a base 64 string and back, but for
some reason, the FromBase64Transform is return less bytes than it should. I
have verified that the base64 string is the same length as the one that was
output from ToBase64Transform. I also turned on DoNotIgnoreWhitespaces.
Any ideas?? Here is the code: // ToBase64
ASCIIEncoding enc = new ASCIIEncoding();
byte[] valBytes = enc.GetBytes(val);
MemoryStream mstr = new MemoryStream(); CryptoStream str = new CryptoStream(mstr,new
ToBase64Transform(),CryptoStreamMode.Write);
str.Write(valBytes,0,valBytes.Length); byte[] buf = mstr.ToArray();
return Convert.ToBase64String(buf); // From Base64
byte[] valBytes = Convert.FromBase64String(val);
MemoryStream mstr = new MemoryStream(); CryptoStream str = new CryptoStream(mstr,new
FromBase64Transform(FromBase64TransformMode.DoNotI gnoreWhiteSpaces),CryptoSt
reamMode.Write);
str.Write(valBytes,0,valBytes.Length); byte[] buf = mstr.ToArray();
ASCIIEncoding enc = new ASCIIEncoding();
return enc.GetString(buf);


If all you want to do is switch back and forth between strings, might
something like this work for you?

public static string ToBase64(string asciiText) {
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(asciiText);
return Convert.ToBase64String(buffer);
}

public static string FromBase64(string base64Text) {
byte[] buffer = Convert.FromBase64String(base64Text);
return ASCIIEncoding.ASCII.GetString(buffer);
}

HTH,
Tim
--
Tim Smelser - MVP Visual C#
To email me, make the snot hot.
Nov 15 '05 #2
Thanks Tim, that worked. I had tried something similar, but got an error.
Must've been doing something wrong.

Cole

"Tim Smelser" <t_*******@snotmail.com> wrote in message
news:mr****************************@40tude.net...
On Thu, 5 Feb 2004 12:25:26 -0600, Cole Shelton wrote:
Hi all,

I am simply trying to do a transform to a base 64 string and back, but for some reason, the FromBase64Transform is return less bytes than it should. I have verified that the base64 string is the same length as the one that was output from ToBase64Transform. I also turned on DoNotIgnoreWhitespaces.
Any ideas?? Here is the code:

// ToBase64
ASCIIEncoding enc = new ASCIIEncoding();
byte[] valBytes = enc.GetBytes(val);
MemoryStream mstr = new MemoryStream();

CryptoStream str = new CryptoStream(mstr,new
ToBase64Transform(),CryptoStreamMode.Write);
str.Write(valBytes,0,valBytes.Length);

byte[] buf = mstr.ToArray();
return Convert.ToBase64String(buf);

// From Base64
byte[] valBytes = Convert.FromBase64String(val);
MemoryStream mstr = new MemoryStream();

CryptoStream str = new CryptoStream(mstr,new
FromBase64Transform(FromBase64TransformMode.DoNotI gnoreWhiteSpaces),CryptoSt reamMode.Write);
str.Write(valBytes,0,valBytes.Length);

byte[] buf = mstr.ToArray();
ASCIIEncoding enc = new ASCIIEncoding();
return enc.GetString(buf);


If all you want to do is switch back and forth between strings, might
something like this work for you?

public static string ToBase64(string asciiText) {
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(asciiText);
return Convert.ToBase64String(buffer);
}

public static string FromBase64(string base64Text) {
byte[] buffer = Convert.FromBase64String(base64Text);
return ASCIIEncoding.ASCII.GetString(buffer);
}

HTH,
Tim
--
Tim Smelser - MVP Visual C#
To email me, make the snot hot.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: qazmlp | last post by:
Can anybody comment(& suggest improvements) on the following implementation that is for truncating the character buffer contents to the desired length? Truncating char array void...
5
by: VISHNU VARDHAN REDDY UNDYALA | last post by:
Hello, Can someone over here help me in truncating a float variable. I mean if PI=3.14159 ...How can I get to read the first two or first three decimal values with out rounding them. Any...
5
by: VM | last post by:
How can I truncate a string? For example, if I have a string containing "Hello All" but the space reserved for this string is of 4 chars, how can I truncate it to "Hell"? Would I need to create my...
1
by: Jitesh Sinha | last post by:
Hi, I am running Windows 2003/ IIS 6.0. I was stuck with rather a abnormal behaviour of System.Web.mail class. It was truncating the message body after 3,071 character. The code i was testing...
3
by: Ron | last post by:
I *just* know there's a function to do this, but I can't find it: I want to take the following string: c:\winnt\23342432sss\2343243ccc\32432432423eeee\2432424cc\t...
4
by: DotNetJunkies User | last post by:
I am calling a VB6 dll from a vb.net windows application that returns an array of strings. My issue is it seems to truncate after a NULL character. For Example VB 6 is returning a string with the...
3
by: rangermccoy | last post by:
Hello there, What are the best php/c libraries for handling media including images, video, and music? I would like to manipulate media dfiles, including watermarking, thumbnailing,...
1
by: Daniel Wilson | last post by:
We have a stored procedure that is giving us an XML representation (using FOR XML Explicit) of a sales order. Sometimes that evaluats to quite a long string. Unfortunately, this XML string is...
15
geolemon
by: geolemon | last post by:
I'm having a seriously Twilight Zone moment: In the code below, I'm building a SQL command into a string variable, declared at the top of my code. Then, I connect to the database and try to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.