Solved

C# String With Cyrillic Digits To Byte Array

Posted on 2010-08-27
18
3,859 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

805 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