Question Encoding / Decoding string from cyrilic to UTF-8

daniel.dfg

Member
Joined
Sep 10, 2012
Messages
5
Programming Experience
3-5
Hi all,

Please help me with this issue I face with.
From a third-party software, I receive a string in an ASCII Textfile, which is originally cyrilic.
I have been given this kind of string: 7dkg0NLFxMzBx8HFzSDawSDSwdrVzc7ZxSDDxc7ZOg
I receive the information from the software that this is koi8-r encoding.

When I use Universal online Cyrillic decoder - recover your texts and I select Source Encoding koi8-r, displayed as UTF-8 and Postfilter "Base64", I get this correct string:
Мы предлагаем за разумные цены:

But sorry, I haven't succeeded getting this kind of decoding in vb.net.
I really hope one of you experts finds a way how I can solve this issue.

Thank you very much in advance,
Daniel
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
Мы предлагаем за разумные цены
The correct Base64 for that string is "7dkg0NLFxMzBx8HFzSDawSDSwdrVzc7ZxSDDxc7Z".
You first have to convert from Base64 to the byte content of the plain string using Convert.FromBase64 method, then get the Encoding object using Encoding.GetEncoding("koi8-r"), and use the Encoding.GetString method to convert the bytes to string, for example:
        Dim en = System.Text.Encoding.GetEncoding("koi8-r")
        Dim b64 = "7dkg0NLFxMzBx8HFzSDawSDSwdrVzc7ZxSDDxc7Z"
        Dim text = en.GetString(Convert.FromBase64String(b64))
 

daniel.dfg

Member
Joined
Sep 10, 2012
Messages
5
Programming Experience
3-5
Thank you John, this works perfectly.
The two characters in the end of the string: "Og" that you've removed - these are the bytes for the ":", right? How can I handle this and get the correct string for the base64?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
In that case, if the colon is to be included, the correct Base64 is "7dkg0NLFxMzBx8HFzSDawSDSwdrVzc7ZxSDDxc7ZOg==". The last = chars is padding to multiple of 4 and is required.
So if you have a non-padded string (b64) you can append padding yourself using modulo calculation:
b64 &= New String("="c, b64.Length Mod 4)
 
Top Bottom