C/C++/java/C#中double/float 怎么获得unsigned char*数组【硬核】

PART1: C/C++部分:

//输入输出流
#include <iostream>
//命名空间
using namespace std;
//对标java的byte类型,8bits,range[0,255]
#define byte unsigned char
//c/c++中,long 占用内存空间32bits,long long 占用64bits
//对标java的long类型 占用64bits,double 占用64bits
//故用一个 LONG的宏定义来对标java的long类型
#define LONG long long
//此处使用指针将LONG类型转为double类型
//因为空间一样,因此,只需要对LONG类型取地址后给其转成double类型的指针
//然后取内容便可以得到double了
//余下方法同理
#define long2double(value) *((double*) &value)
#define double2long(value) *((LONG*) &value)
#define float2int(value) *((int*) &value)
#define int2float(value) *((float*) &value)


/*
* 将LONG类型的值获得其 byte数组
*/
byte* long2byte(LONG value) {
    byte* bytes = new byte[8];
    for (int i = 0; i < 8; i++) {
        bytes[i] = (value >> (i * 8)) & 0xFF;
    }
    return bytes;
}

/*
* 将byte数组转为long类型
*/
LONG byte2long(byte* bytes) {
    LONG value = 0;
    for (int i = 0; i < 8; i++) {
        value |= (LONG) bytes[i] << (i * 8);
    }
    return value;
}

/*
* 将double转为byte数组
*/
byte* double2byte(double value) {
    return long2byte(double2long(value));
}

/*
* 将byte数组转为double
*/
double byte2double(byte* bytes) {
    LONG value = byte2long(bytes);
    return long2double(value);
}

/*
* 将int转为byte数组
*/
byte* int2byte(int value) {
    byte* bytes = new byte[4];
    for (int i = 0; i < 4; i++) {
        bytes[i] = (value >> (i * 8)) & 0xFF;
    }
    return bytes;
}

/*
* 将byte数组转为int
*/
int byte2int(byte* bytes) {
    int value = 0;
    for (int i = 0; i < 4; i++) {
        value |= (int) bytes[i] << (i * 8);
    }
    return value;
}

/*
* 将float 转为byte数组
*/
byte* float2byte(float value) {
    return int2byte(float2int(value));
}

/*
* 将byte数组转为float 
*/
float byte2float(byte* bytes) {
    int value = byte2int(bytes);
    return int2float(value);
}

//测试部分
int main() {
    double value = 123.456;
    byte* bytes = double2byte(value);
    double result = byte2double(bytes);
    cout << result << endl;

    //float test
    float value2 = 3.14159f;
    byte* bytes2 = float2byte(value2);
    float result2 = byte2float(bytes2);
    cout << result2 << endl;
    return 0;
}

[c/cpp]测试部分

测试结果

引申部分:

Notes: 用java/c#同学可以继续看一下

java:

[java]代码部分

public byte[] long2Bytes(long l){
        byte[] bytes = new byte[8];
        for (int i = 0; i < 8; i++) {
            bytes[i] = (byte) ((l >> 8 * i) & 0xff);
        }
        return bytes;
    }
    
    public long bytes2Long(byte[] bytes){
        long result = 0;
        for (int i = 0; i < 8; i++) {
            result |= ((long) (bytes[i] & 0xff)) << (8 * i);
        }
        return result;
    }

    public byte[] double2Bytes(Double d) {
        return long2Bytes(Double.doubleToLongBits(d));
    }

    public double bytes2Double(byte[] bytes) {
        return Double.doubleToLongBits(bytes2Long(bytes));
    }

    public int bytes2Int(byte[] bytes) {
        int value = 0;
        for (int i = 0; i < 4; i++) {
            value |= ((int) (bytes[i] & 0xff)) << (8 * i);
        }
        return value;
    }

    public byte[] int2Bytes(int value) {
        byte[] bytes = new byte[4];
        for (int i = 0; i < 4; i++) {
            bytes[i] = (byte) ((value >> 8 * i) & 0xff);
        }
        return bytes;
    }

    public float bytes2Float(byte[] bytes) {
        return Float.intBitsToFloat(bytes2Int(bytes));
    }

    public byte[] float2Bytes(float f) {
        return int2Bytes( Float.floatToIntBits(f));
    }

当然java也是有工具类的:

public static int bytes2int(byte[] bytes){
    int result = 0;
    ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
    return result;
  }
  
  public static byte[] int2bytes(int value){
    byte[] bytes = new byte[4];
    ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).putInt(value);
    return bytes;
  }
  
  public static byte[] float2bytes(float value){
    byte[] bytes = new byte[4];
    ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).putFloat(value);
    return bytes;
  }
  
  public static float bytes2float(byte[] bytes){
    float result = 0;
    ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat();
    return result;
  }
  
  public static byte[] double2bytes(double value){
    byte[] bytes = new byte[8];
    ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).putDouble(value);
    return bytes;
  }
  
  public static double bytes2double(byte[] bytes){
    double result = 0;
    ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getDouble();
    return result;
  }
  
  public static byte[] long2bytes(long value){
    byte[] bytes = new byte[8];
    ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).putLong(value);
    return bytes;
  }
  
  public static long bytes2long(byte[] bytes){
    long result = 0;
    ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getLong();
    return result;
  }

C#是如何做的呢?

C#就工具类BitConverter我们看看其中几个源码:

public unsafe static byte[] GetBytes(int value)
{
    byte[] array = new byte[4];
    fixed (byte* ptr = array)
    {
        *(int*)ptr = value;
    }

    return array;
}

public unsafe static byte[] GetBytes(long value)
{
    byte[] array = new byte[8];
    fixed (byte* ptr = array)
    {
        *(long*)ptr = value;
    }

    return array;
}

public unsafe static byte[] GetBytes(float value)
{
    return GetBytes(*(int*)(&value));
}

public unsafe static byte[] GetBytes(double value)
{
    return GetBytes(*(long*)(&value));
}

public unsafe static int ToInt32(byte[] value, int startIndex)
{
    if (value == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
    }

    if ((uint)startIndex >= value.Length)
    {
        ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
    }

    if (startIndex > value.Length - 4)
    {
        ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
    }

    fixed (byte* ptr = &value[startIndex])
    {
        if (startIndex % 4 == 0)
        {
            return *(int*)ptr;
        }

        if (IsLittleEndian)
        {
            return *ptr | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
        }

        return (*ptr << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
    }
}

所以说语言大同小异。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值