zoukankan      html  css  js  c++  java
  • 基础数据类型与字节数组相互转换 BitConvter类

    BitConveter类

    总结:这是非常实用的一个类,当需要对字节数组操作时,它提供了很多有效的方法。

    1、方法

      GetBytes(Int32)//以字节数组的形式返回指定的 32 位有符号整数值。

      ToString(Byte[])//将指定的字节数组的每个元素的数值转换为它的等效十六进制字符串表示形式。

      ToSingle//返回由字节数组中指定位置的四个字节转换来的单精度浮点数

      ToInt32//返回由字节数组中指定位置的四个字节转换来的 32 位有符号整数。 

    2、实例

      代码:

      

    using System;
    
    public class Example
    {
       public static void Main()
       {
          int value = 1234;
          byte[] bytes = BitConverter.GetBytes(value);//将int转换为bytes数组
          Console.WriteLine(BitConverter.ToString(bytes));//将byts数组转换为16进制形式的字符
    
          if (BitConverter.IsLittleEndian)
             Array.Reverse(bytes); 
    
          Console.WriteLine(BitConverter.ToString(bytes));
          // Call method to send byte stream across machine boundaries.
    
          // Receive byte stream from beyond machine boundaries.
          Console.WriteLine(BitConverter.ToString(bytes));
          if (BitConverter.IsLittleEndian)
             Array.Reverse(bytes);
    
          Console.WriteLine(BitConverter.ToString(bytes));
          int result = BitConverter.ToInt32(bytes, 0);将bytes数组转换为int
          Console.WriteLine("Original value: {0}", value);
          Console.WriteLine("Returned value: {0}", result);
       }
    }

    结果显示:

      

     

  • 相关阅读:
    eclipse建立工作集管理项目
    echarts-x
    GeoJSON
    mysql 5.7 root password 过期
    kubernetes centos7
    JestClient
    树莓派镜像制作
    docker run elasticsearch
    vm.max_map_count
    远程访问jupyter notebook
  • 原文地址:https://www.cnblogs.com/void0/p/4265609.html
Copyright © 2011-2022 走看看