いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

数値とバイト列の変換、文字列とバイト列の変換 (BitConverter, Convert, X2, IsLittleEndian)

業務でよく利用するのに、しょっちゅう忘れるのでメモメモ。

  1. バイト列を16進数ダンプ
    1. BitConverter.ToString(byte[]);
  2. 数値を16進数表示
    1. string.Format("{0:X2}", b);
  3. 数値からバイト列
    1. BitConverter.GetBytes(int);
  4. バイト列から数値
    1. BitConverter.ToInt32(byte[], 0);
      1. ToXXXでいろいろなオーバーロードが定義されている。
  5. 数値をいろいろな基数に変換
    1. Convert.ToString(xxx, 2)
      1. 第2引数が基数
  6. 文字列からバイト列
    1. Encoding.GetBytes("xxx");
      1. 実際は、Encoding.GetEncoding("sjis").GetBytesとなる
  7. バイト列から文字列
    1. Encoding.GetString(byte[])
      1. 実際は、Encoding.GetEncoding("sjis").GetStringとなる
  8. 利用しているアーキテクチャエンディアン判定
    1. BitConverter.IsLittleEndian
      1. Falseの場合、ビッグエンディアン


以下、サンプルです。

    #region ByteArraySamples-01
    public class ByteArraySamples01 : IExecutable
    {
        public void Execute()
        {
            //
            // バイト配列を2進数表示.
            //
            byte[] buf = new byte[4];
            buf[0] = 0;
            buf[1] = 0;
            buf[2] = 0;
            buf[3] = 98;
            
            Console.WriteLine(
                        string.Join(
                            "", 
                            buf.Take(4).Select(b => Convert.ToString(b, 2).PadLeft(8, '0'))
                        ));
        }
    }
    #endregion
    
    #region ByteArraySamples-02
    public class ByteArraySamples02 : IExecutable
    {
        public void Execute()
        {
            //
            // バイト列を16進数文字列へ
            //
            byte[] buf = new byte[5];
            new Random().NextBytes(buf);
            
            Console.WriteLine(BitConverter.ToString(buf));
        }
    }
    #endregion
    
    #region ByteArraySamples-03
    public class ByteArraySamples03 : IExecutable
    {
        public void Execute()
        {
            //
            // 数値を16進数で表示.
            //
        Console.WriteLine("0x{0:X2}", 12345678);
        }
    }
    #endregion
    
    #region ByteArraySamples-04
    public class ByteArraySamples04 : IExecutable
    {
        public void Execute()
        {
            //
            // 数値からバイト列へ変換
            //
            int i = 123456;
            byte[] buf = BitConverter.GetBytes(i);
            
            Console.WriteLine(BitConverter.ToString(buf));
        }
    }
    #endregion
    
    #region ByteArraySamples-05
    public class ByteArraySamples05 : IExecutable
    {
        public void Execute()
        {
            //
            // バイト列を数値に
            //
            byte[] buf = new byte[4];
            new Random().NextBytes(buf);
            
            int i = BitConverter.ToInt32(buf, 0);
            
            Console.WriteLine(i);
        }
    }
    #endregion
    
    #region ByteArraySamples-06
    public class ByteArraySamples06 : IExecutable
    {
        public void Execute()
        {
            //
            // 文字列をバイト列へ
            //
            string s = "gsf_zero1";
            byte[] buf = Encoding.ASCII.GetBytes(s);
            
            Console.WriteLine(BitConverter.ToString(buf));
        }
    }
    #endregion
    
    #region ByteArraySamples-07
    public class ByteArraySamples07 : IExecutable
    {
        public void Execute()
        {
            //
            // バイト列を文字列へ.
            //
            string s = "gsf_zero1";
            byte[] buf = Encoding.ASCII.GetBytes(s);
            
            Console.WriteLine(Encoding.ASCII.GetString(buf));
        }
    }
    #endregion
    
    #region ByteArraySamples-08
    public class ByteArraySamples08 : IExecutable
    {
        public void Execute()
        {
            //
            // 数値をいろいろな基数に変換.
            //
            int i = 123;
            
            Console.WriteLine(Convert.ToString(i, 16));
            Console.WriteLine(Convert.ToString(i, 8));
            Console.WriteLine(Convert.ToString(i, 2));
        }
    }
    #endregion
    
    #region ByteArraySamples-09
    public class ByteArraySamples09 : IExecutable
    {
        public void Execute()
        {
            //
            // 利用しているアーキテクチャのエンディアンを判定.
            //
            Console.WriteLine(BitConverter.IsLittleEndian);
        }
    }
    #endregion

================================
過去の記事については、以下のページからご参照下さい。