Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have written the following code which is converting string to byte array so in that code when i gave input string in English i get correct output but when i gave string in Japanese i'm getting error. Can anyone please solve this issue.


<pre> public class name
{
     public static void Main()
    {
    
        string str = "出衣紅詩弥";
        byte[] byt = new byte[str.Length];
        for (int i = 0; i < str.Length; i++)
        { 
            byt[i] = Convert.ToByte(str[i]);
        }
         for(int i =0; i<byt.Length; i++)
        {
            Console.WriteLine("Byte of char "+ str[i] + "\' :" +byt[i]);
        }
  
    }
}



Getting error like this:
Unhandled Exception:
System.OverflowException: Value was either too large or too small for an unsigned byte.
at System.Convert.ThrowByteOverflowException () [0x00000] in <533173d24dae460899d2b10975534bb0>:0 
  at System.Convert.ToByte (System.Char value) [0x00008] in <533173d24dae460899d2b10975534bb0>:0 
  at name.Main () [0x00021] in <eba12a16001248a19f09d95e0b7b46aa>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.OverflowException: Value was either too large or too small for an unsigned byte.
  at System.Convert.ThrowByteOverflowException () [0x00000] in <533173d24dae460899d2b10975534bb0>:0 
  at System.Convert.ToByte (System.Char value) [0x00008] in <533173d24dae460899d2b10975534bb0>:0 
  at name.Main () [0x00021] in <eba12a16001248a19f09d95e0b7b46aa>:0 


What I have tried:

I tried but i didn't get it. Can someone please review the code and try to solve this issue.
Posted
Updated 21-Jun-22 23:28pm

As Japanese is Unicode and uses several bytes per character, you can not simply convert that to bytes.
You might use the Encoding.Convert Method (System.Text) | Microsoft Docs[^]

If you want to get a byte array you could use:
var byt = Encoding.Unicode.GetBytes(str);
Where one Japanese character occupies several bytes in the array.

Also see: C# : Japanese characters with unicode encoding - Stack Overflow[^]
 
Share this answer
 
v3
Comments
Member 15682479 22-Jun-22 7:17am    
Yeah i got the answer, thanks for taking time to helping me out in this. Yeah definitely i will go through that documents what you mentioned. Thank you.
Try this:

string str = "出衣紅詩弥";
var byt = System.Text.Encoding.Unicode.GetBytes(str);
                
for (int i = 0; i < str.Length; i++)
{
     Console.WriteLine("Byte of char " + str[i] + "\' :" + byt[2 * i]  + "  " + byt[2 * i + 1]);
}


You get two bytes for every Japanese character.
 
Share this answer
 
Comments
Member 15682479 22-Jun-22 7:23am    
Yeah its working, Thank you for the help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900