zoukankan      html  css  js  c++  java
  • C#/C++ 中字节数组与int类型转换

    1.C#中int和byte[]转换:

    /// <summary>
    /// 把int32类型的数据转存到4个字节的byte数组中
    /// </summary>
    /// <param name="m">int32类型的数据
    /// <param name="arry">4个字节大小的byte数组
    public static bool ConvertIntToByteArray(Int32 m, ref byte[] arry)
    {
        if (arry == null) return false;
        if (arry.Length < 4) return false;
        arry[0] = (byte)(m & 0xFF);
        arry[1] = (byte)((m & 0xFF00) >> 8);
        arry[2] = (byte)((m & 0xFF0000) >> 16);
        arry[3] = (byte)((m >> 24) & 0xFF);
        return true;
    }
    
    /// <summary>
    /// 把byte数组中的前4个字节转换为int32类型的数据
    /// </summary>
    public static int ConvertByteArrayToInt(byte[] arry)
    {
        return BitConverter.ToInt32(arry, 0);
    }

    2.C++中byte[]与int类型转换

    //int --> BYTE[]:
    
    int data = 0xFFFFFFFF;
    unsigned char buf[4];
     
    memcpy(buf, &data, sizeof(int));
    
    //BYTE[] --> int :
    
    memcpy(&data, buf, 4);

      

  • 相关阅读:
    8.22
    webstrom安装流程
    8.21
    8.20
    8.20学习笔记
    使用WebClient异步获取http资源
    导航栏,可直接使用
    asp.net mvc5实现单点登录
    使用C#调用Word的接口生成doc文件与html文件
    下载网页并保存
  • 原文地址:https://www.cnblogs.com/wj-love/p/3948288.html
Copyright © 2011-2022 走看看