Bitwise endian swap for various types

前端 未结 4 1664
感情败类
感情败类 2020-12-03 12:02

With the help of various sources, I have written some SwapBytes methods in my binary reader class that swap endian in ushort, uint, an

4条回答
  •  臣服心动
    2020-12-03 12:49

    You should have a look to following msdn page : http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx

    You may simply use Array.Reverse and bitConverter:

      int value = 12345678;
      byte[] bytes = BitConverter.GetBytes(value);
    
      Array.Reverse(bytes); 
      int result = BitConverter.ToInt32(bytes, 0);
    

提交回复
热议问题