zoukankan      html  css  js  c++  java
  • 数据类型互转,字符串编码

    1、字符串转byte数组

    string msg="你好你好";

    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(msg);

    2、byte数组转字符串

    byte[] byts = File.ReadAllBytes(@"c:ooo.txt");
    string msg = System.Text.Encoding.UTF8.GetString(byts);

    3、字节数组转16进制文本表示

     string msg3= BitConverter.ToString(bytes);//结果:E5-93-88-E5-96-BD-E6-B2-83

    4、字节数组转任何的基本类型。比如字节数组转int,single,double等

    Single s= BitConverter.ToSingle(bytes, 4);

    5、double类型数字转为计算机二进制存储的字节数组格式  互转

     byte[] bytes = BitConverter.GetBytes(38414.4);

    double d=38414.4;

    计算机二进制存储格式为:10000001110 0010110000011100110011001100110011001100110011001100  (红色符号位,蓝色阶码,黑色尾数)

                                      01000000 11100010 11000001 11001100 11001100 11001100 11001100 11001100  

                          10进制      64  226 193 204 204 204 204 204

                                   

    6、16进制转整型

    int a=  Convert.ToInt32("AE", 16);

    一、

    类型   CTS类型 string int float double bool 转二进制存储    
    整型 sbyte                  
      short Int16                
      int Int32                
      long Int64                
      byte Byte     Single s= BitConverter.ToSingle(bytes, 2);//从bytes数组第2位开始提取4个字节。进行转换          
      ushort UInt16                
      uint UInt32                
      ulong UInt64                
    浮点 float Single           byte[] bytes= BitConverter.GetBytes(2.56);    
      double Double                
    decimal decimal Decimal                
    bool bool Boolean                
    字符 char Char                
    引用类型 string String                
                         
  • 相关阅读:
    新的开始
    0基础学习ios开发笔记第二天
    0基础学习ios开发笔记第一天
    Linux内核中SPI/I2c子系统剖析
    Linux内核之mmc子系统-sdio
    系统调用和中断处理的异同(以Linux MIPS为例)
    开始lisp的旅程
    Linux kernel驱动相关抽象概念及其实现 之“bus,device,driver”
    Linux kernel驱动相关抽象概念及其实现 之“linux设备模型kobject,kset,ktype”
    /etc/udev
  • 原文地址:https://www.cnblogs.com/crhdyl/p/4989142.html
Copyright © 2011-2022 走看看