Question FromBase64string - Invalid length of the string

miss14

New member
Joined
Oct 20, 2018
Messages
3
Programming Experience
Beginner
Hello everybody!
I have a string in base64, that I have to convert in a byte array. To do this, I use Frombase64string. But it give me an error about the wrong lenght of the string, that isn't a multiple of 4.
So I add to the string a simbol ( =) to get the correct lenght. But I have to add 3 character. I see that i can use only 2 = as padding character.
How can i do?
Thanks
 
Last edited:
You should start with the data that you expect to get back and pass that to ToBase64String and see what it produces and compare that to what you have. That will then tell you what's missing. Have you read up about the base-64 format too? Where is this data coming from that it isn't valid?
 
I see I can do
Regex regex = new Regex(@"==="); //simboli
string[] val = regex.Split("Y2F6em9vb29vb29vb29vb29vb28===");
Byte[] b = convert.frombase64string(val);

Is it right?
 
miss14 said:
Y2F6em9vb29vb29vb29vb29vb28===
The last "==" doesn't belong there, before that is a correct Base64 string.
 
miss14 said:
My string is only Y2F6em9vb29vb29vb29vb29vb
That is not the same string as the one in your code in post 3, and it is not a Base64 string. You also can't add padding like that in Base64 encoding, there can only be zero, one or two padding chars.
 
Back
Top Bottom