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

FromBase64Transform truncating string

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.