3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C#のstringとbyteの相互変換

Posted at

shift-jisは可変長だから、自分で勝手に2バイト区切りにしては駄目

            {
                string sOriginal = "ユニCodeのbyte変換";
                byte[] arrBytes = Encoding.Unicode.GetBytes(sOriginal);
                //foreach (byte b in bytesData) Debug.WriteLine(b);
                Debug.WriteLine(BitConverter.ToString(arrBytes));
                string hexString = BitConverter.ToString(arrBytes).Replace("-", "");
                Debug.WriteLine(hexString);

                byte[] repack = new byte[hexString.Length / 2];
                for (var i = 0; i < hexString.Length / 2; i++)
                {
                    var hexChar = hexString.Substring(2 * i, 2);
                    repack[i] = Convert.ToByte(hexChar, 16);
                }
                Debug.WriteLine(Encoding.Unicode.GetString(repack));

                //2バイトずつもどす
                byte[] tmp = new byte[2];
                for (int i = 0; i < arrBytes.Length / 2; i++)
                {
                    tmp[0] = arrBytes[2 * i + 0];
                    tmp[1] = arrBytes[2 * i + 1];
                    Debug.WriteLine(Encoding.Unicode.GetString(tmp));
                }
            }
            Debug.WriteLine("---");
            {
                string sOriginal = "シフトjisのbyte変換";
                byte[] arrBytes = Encoding.GetEncoding("Shift_JIS").GetBytes(sOriginal);
                //foreach (byte b in bytesData) Debug.WriteLine(b);
                Debug.WriteLine(BitConverter.ToString(arrBytes));
                string hexString = BitConverter.ToString(arrBytes).Replace("-", "");
                Debug.WriteLine(hexString);

                byte[] repack = new byte[hexString.Length / 2];
                for (var i = 0; i < hexString.Length / 2; i++)
                {
                    var hexChar = hexString.Substring(2 * i, 2);
                    repack[i] = Convert.ToByte(hexChar, 16);
                }
                Debug.WriteLine(Encoding.GetEncoding("Shift_JIS").GetString(repack));

                //shiftjisは可変長なので、2バイトで区切るとくしゃくしゃになる
                byte[] tmp = new byte[2];
                for (int i = 0; i < arrBytes.Length / 2; i++)
                {
                    tmp[0] = arrBytes[2 * i + 0];
                    tmp[1] = arrBytes[2 * i + 1];
                    Debug.WriteLine(Encoding.GetEncoding("Shift_JIS").GetString(tmp));
                }
            }

実行結果

E6-30-CB-30-23-FF-4F-FF-44-FF-45-FF-6E-30-62-00-79-00-74-00-65-00-09-59-DB-63
E630CB3023FF4FFF44FF45FF6E3062007900740065000959DB63
ユニCodeのbyte変換
ユ
ニ
C
o
d
e
の
b
y
t
e
変
換
---
83-56-83-74-83-67-6A-69-73-82-CC-62-79-74-65-95-CF-8A-B7
8356837483676A697382CC6279746595CF8AB7
シフトjisのbyte変換
シ
フ
ト
ji
s・
フb
yt
e・
マ・
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?