Click here to Skip to main content
15,034,454 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
SOLVED!!!! Guys something strange happened and it started to work I don't know why but I'am glad it finally works. Thanks for helping me!

Hi guys. Need your help. How can I encode and decode string with diacritic and spaces? I was looking for hours for solution and all I have now is:
EDIT: Ok, so thanks to solutions I've get this code:

public static string encode(string text)
    byte[] mybyte = System.Text.Encoding.UTF8.GetBytes(text);
    string returntext = System.Convert.ToBase64String(mybyte);
    return returntext;

public static string decode(string text)
    byte[] mybyte = System.Convert.FromBase64String(text);
    string returntext = System.Text.Encoding.UTF8.GetString(mybyte);
    return returntext;

But I'am getting error "Invalid length for a Base-64 char array" in System.Convert.FromBase64String(text);
And I'am also not sure if this will take spaces in strings.

What to do? Thanks for every reply.
Updated 31-May-21 20:14pm
Dr.Walt Fair, PE 13-Feb-12 15:33pm
What are you trying to accomplish? Base64 can only encode 64 unique characters, no matter how many you wish it to encode. If you need more, you need to use something else or just don't encode. So that gets back to the question: What are you trying to accomplish?
LosEagle 13-Feb-12 16:03pm
Just encode text to file so when someone open it, he will see this coded and when I need to use it in my program decode it to variable and show decoded text.
Dr.Walt Fair, PE 13-Feb-12 16:59pm
OK, that would be some sort of encryption. Do you need high security or can some sort of simple bit rotation work?
LosEagle 13-Feb-12 17:07pm
The UTF8 encrypted strings like xaF1bGM= are enough for me at this time :). I don't need hundres of codes just for encryption and decryption.

Your mistake is using ASCII. Use any of Unicode UTFs, preferably UTF-8; and it will work. Of course, if you save text in plain text file, do it in matching encoding. All modern editor can do it, including Visual Studio.

One pretty usual misconception is that Latin-based languages don't use Unicode. This is not true. Even pure American English uses Unicode code point outside the ASCII subset, in typographically correct dash characters, quotation marks, etc.

See: — – … “ ” ' © and a lot more.

You really need some understanding of Unicode.
Please see:[^],[^],[^].

LosEagle 13-Feb-12 15:49pm
ok so I changed the System.Text.ASCIIEncoding.ASCII. for System.Text.Encoding.UTF8. In encoder and decoder
but now it shows error “Invalid length for a Base-64 char array” in converting FromBase64String in decoder when I type something in textbox.
It is because you use ASCII Encoding which is very limited and doesn't support diacritic characters. Try another encoding, like UTF-8. ([^])

Good luck!
LosEagle 13-Feb-12 16:05pm
Thanks. Thats pretty good tutorial really helped me :). But when I do it exactly like that I get an error “Invalid length for a Base-64 char array”.

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