Solved

C# String With Cyrillic Digits To Byte Array

Posted on 2010-08-27
18
3,955 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c#, case, if 4 37
Stop Git from being my repository 1 50
c# deserialize JSON from web service using JsonConvert 9 61
Is NLP considered artificial intelligence ? 3 43
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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