[C#] Convert string to byte[] (string to byte array - binary)
Tag:[C#] [byte] [Conversion]
And communications applications such as writing to a binary file, byte array from a string byte[] You may want to convert. Unicode strings in C # because it is defined as a string, byte[] array is simply copied to the encoding for Unicode, including the need to convert the encoding process. System.Text.Encoding a string in C # class using the class and provides byte[] can be converted to.
The string byte[] If you convert to type GetBytes() method is used.
private void button1_Click(object sender, EventArgs e)
{
  string text = "string data";

  //ASCII Encode
  byte[] data = System.Text.Encoding.ASCII.GetBytes(text);
  
  //For Shift-JIS
  //byte[] data = System.Text.Encoding.GetEncoding("shift_jis").GetBytes(text);

  //For EUC
  //byte[] data = System.Text.Encoding.GetEncoding("euc-jp").GetBytes(text);

  //For unicode
  //byte[] data = System.Text.Encoding.Unicode.GetBytes(text);

  //For utf-8
  //byte[] data = System.Text.Encoding.UTF8.GetBytes(text);

  //Do something
  WirteBinaryData(data);
}
Register date :2011-10-17    Last update :2017-01-17
Tag:[C#] [byte] [Conversion]
JapaneseEnglish
プライバシー    iPentecについて
iPentec all rights reserverd. (ISDC)