Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, newly i notice that FromBase64String cause error.
what should i do to solve this?

C#
string s = "Hello, World!";
byte[] plainText = Convert.FromBase64String(s);


Quote:
Invalid character in a Base-64 string.


Please use Improve question instead of submit solution -- E.H.
OP wonders:
then what should i use instead of base64 :-/ to convert my string to byte and pass to encryption algorithm?

i see must cases have get byte, but not to string, that's my problem :|
Posted
Updated 1-Jan-12 5:53am
v2
Comments
GuyThiebaut 1-Jan-12 11:23am    
RTM: http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx

Don't put invalid characters in strings that you want to convert from Base64!
Base64 contains upper and lower case A-Z, 0-9, "plus" and "slash" only.
Any other characters will cause an error.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Jan-12 11:22am    
Correct, a 5.
I would say, to convert from base64, one needs to convert some data to base64 first.
--SA
Espen Harlinn 1-Jan-12 11:48am    
5'ed!
I did a google search on "c# string to byte array".
Fifth item on first page - http://www.dotnetperls.com/convert-string-byte-array[^]

I fiddled around with things a bit and got this code to work:

C#
// Input string.
const string input = "Hello, World!";

// Invoke GetBytes method.
// ... You can store this array as a field!
byte[] array = Encoding.ASCII.GetBytes(input);

// Loop through contents of the array.
foreach (byte element in array)
{
  System.Diagnostics.Debug.WriteLine((char)element);
}
 
Share this answer
 
From the manual:

FormatException:

The length of s, ignoring white-space characters, is not zero or a multiple of 4.
-or-
The format of s is invalid. s contains a non-base-64 character, more than two padding characters, or a non-white space-character among the padding characters.


This means your string's length also needs to a multiple of 4 - I have just tested this myself...
 
Share this answer
 
no that wasnt the answer, i found nothing, for convert char to string

XML
public static string GetString(byte[] data)
{
    List<char> lChar=new List<char>();
    foreach (byte b in data)
    {
        lChar.Add((char)b);
    }
    return Convert.ToString(lChar.ToArray());
}


the top code which i though was wrong...

one other issue... :
if i conver my chars to string, maybe i was on encrypt stage, so if i wanna decrypt them again, i may have data loss! isnt it?
 
Share this answer
 
if i make instance of the Encoder class, it also will five me getString...

it mean, for example UTF8Encoding.GetBytes is exist as static member, but GetString doest exist.

so to get string back, we jsut have to make an instance:

C#
UTF8Encoding utf8= new UTF8Encoding();


and then call
C#
Byte[] b;
utf8.GetString(b);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900