Solved

C# String With Cyrillic Digits To Byte Array

Posted on 2010-08-27
18
3,810 Views
Last Modified: 2013-12-17
OK guys, I have a string and it contains cyrillic  characters and digits and latin characters. And I want to convert this string to a byte array, converting it to unicode following this table.
http://www.ibm.com/developerworks/linux/library/l-u-cyr/table4.jpg
For example if I have a cyrrilic "A" the byte value should be 0xC0.
Don't tell me to use System.Text.UTF8Encoding.UTF8.GetBytes(string str) as it returns ... stupid stings :).
0
Comment
Question by:IncognitoMan
  • 9
  • 8
18 Comments
 
LVL 2

Expert Comment

by:SkydiverFL
ID: 33545208
Won't ToCharArray() return the character array?  If so, can you not just convert the individual characters to the equiv bytes?
0
 

Author Comment

by:IncognitoMan
ID: 33545242
OK Ill try converting it to byte array and then convert it to byte aquvalents. Then I'll tell you the result. :)
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 33545310
Try this
UnicodeEncoding unicode = new UnicodeEncoding();
Byte[] encodedBytes = unicode.GetBytes(unicodeString);
0
 

Author Comment

by:IncognitoMan
ID: 33545316
Nope, the byte array returns stupid things. For example it returns values like 1040 for "¿". Maybe It's UTF-16. But how do I make the string UTF-8?
0
 

Author Comment

by:IncognitoMan
ID: 33545328
OK SriVaddadi I'll try it.
By the way the character in the post above was cyrillic "A".
0
 

Author Comment

by:IncognitoMan
ID: 33545388
This retirns to bytes for an "A" 0x16 and 0x04. Any other ideas :).
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 33545423
How about
ASCIIEncoding ascii = new ASCIIEncoding();
Byte[] encodedBytes = unicode.GetBytes(unicodeString);
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 33545490
This should work

int pageCode = 1251
Encoding encoding = Encoding.GetEncoding(pageCode);
Byte[] encodedBytes = encoding.GetBytes(unicodeString)
0
 

Author Comment

by:IncognitoMan
ID: 33545521
It again returns two bytes 208 and 144. Maybe I'll try with switch case statement :), but thats not a solution.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

by:SriVaddadi
ID: 33545582
int pageCode = 1251
Encoding encoding = Encoding.GetEncoding(pageCode);
Byte[] encodedBytes = encoding.GetBytes(unicodeString)

This should work if it is not working then the page code mentioned at the url you posted is incorrect
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 33545683
Did you try it?
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 33545723
Encoding en = Encoding.GetEncoding(1251);
            MessageBox.Show(en.EncodingName);

This give the Encoding name as cyrillic correctly. If this is not working for you then issue might be something else
0
 

Author Comment

by:IncognitoMan
ID: 33548448
I've searched all over the net and tried all of the above, as it was in other sites. Nothing worked for me. I guess I'll be writing a switch case statement with over a hundred cases. :)
0
 
LVL 16

Accepted Solution

by:
SriVaddadi earned 500 total points
ID: 33548870
You do not need a switch statement. you need the correct windows code page. 1251 is the windows code page for Cyrillic. If tht is not what you are looking for then the language might be different with different page code. once you get the page code the code snippet in my last post should work
0
 

Author Comment

by:IncognitoMan
ID: 33549810
It works as wire and electric current with switch case statement, but it's 350 lines.
0
 

Author Closing Comment

by:IncognitoMan
ID: 33549812
Just because you searched all over the net for me, I will give you point ;).
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 33549814
So you could resolve the issue with switch satement?
0
 

Author Comment

by:IncognitoMan
ID: 33549903
Yes, but its a lot of coding for this little problem and sounds stupid. It's like to go from Russia to Germany and take the shortcut to USA :D.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now