将数据类型'long'转换为字节数组 [英] Convert datatype 'long' to byte array

查看:112
本文介绍了将数据类型'long'转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将值(C#中的double/float)转换为字节,并且需要一些帮助..

//数据类型long 4byte -99999999,99至99999999,99
//数据类型long 4byte -99999999,9至99999999,9
//数据类型短2个字节-999,99到999,99
//数据类型短2个字节-999,9到999,9

在我的家里的世界"中,我只需要将它和ASCII.GetBytes()字符串化即可.

但是现在,在这个世界上,我们必须减少可能的空间.
实际上,'-99999999,99'需要12个字节而不是4个字节!如果它是长"数据类型.

I have to convert values (double/float in C#) to bytes and need some help..

// Datatype long 4byte -99999999,99 to 99999999,99
// Datatype long 4byte -99999999,9 to 99999999,9
// Datatype short 2byte -999,99 to 999,99
// Datatype short 2byte -999,9 to 999,9

In my "world at home" i would just string it and ASCII.GetBytes().

But now, in this world, we have to make less possible space.
And indeed that '-99999999,99' takes 12 bytes instead of 4! if it's a 'long' datatype.


由于得到了一些帮助和回答,我在此处附加了一些结果,


Due to some help and answer I attach some results here,

long lng = -9999999999L;
byte[] test = Encoding.ASCII.GetBytes(lng.ToString());  // 11 byte
byte[] test2 = BitConverter.GetBytes(lng);              // 8 byte
byte[] mybyt = BitConverter.GetBytes(lng);              // 8 byte
byte[] bA = BitConverter.GetBytes(lng);                 // 8 byte

仍然需要找出一个细节.即使Lng-variabel的值较低,也就是8字节,即99951(我不会包含ToString()示例).

There still have to be one detail left to find out. The lng-variabel got 8 byte even if it helds a lower values, i.e. 99951 (I won't include the ToString() sample).

如果该值甚至更短,则意味着-999,99-999,99,它将仅占用2个字节的空间.
[END EDIT]

If the value are even "shorter", which means -999,99 -- 999,99 it will only take 2 byte space.
[END EDIT]

推荐答案

请注意,在2个字节中您只能有4个全数字+符号,在4个字节中您只能有9个数字+符号,因此我必须相应地调整您的前提条件.

Be aware that in 2 bytes you can only have 4 full digits + sign, and in 4 bytes you can only have 9 digits + sign, so I had to scale your prereqs accordingly.

public static byte[] SerializeLong2Dec(double value)
{
    value *= 100;
    value = Math.Round(value, MidpointRounding.AwayFromZero);

    if (value < -999999999.0 || value > 999999999.0)
    {
        throw new ArgumentOutOfRangeException();
    }

    int value2 = (int)value;

    return BitConverter.GetBytes(value2);
}

public static double DeserializeLong2Dec(byte[] value)
{
    int value2 = BitConverter.ToInt32(value, 0);
    return (double)value2 / 100.0;
}

public static byte[] SerializeLong1Dec(double value) {
    value *= 10;
    value = Math.Round(value, MidpointRounding.AwayFromZero);

    if (value < -999999999.0 || value > 999999999.0) {
        throw new ArgumentOutOfRangeException();
    }

    int value2 = (int)value;

    return BitConverter.GetBytes(value2);
}

public static double DeserializeLong1Dec(byte[] value) {
    int value2 = BitConverter.ToInt32(value, 0);
    return (double)value2 / 10.0;
}

public static byte[] SerializeShort2Dec(double value) {
    value *= 100;
    value = Math.Round(value, MidpointRounding.AwayFromZero);

    if (value < -9999.0 || value > 9999.0) {
        throw new ArgumentOutOfRangeException();
    }

    short value2 = (short)value;

    return BitConverter.GetBytes(value2);
}

public static double DeserializeShort2Dec(byte[] value) {
    short value2 = BitConverter.ToInt16(value, 0);
    return (double)value2 / 100.0;
}

public static byte[] SerializeShort1Dec(double value) {
    value *= 10;
    value = Math.Round(value, MidpointRounding.AwayFromZero);

    if (value < -9999.0 || value > 9999.0) {
        throw new ArgumentOutOfRangeException();
    }

    short value2 = (short)value;

    return BitConverter.GetBytes(value2);
}

public static double DeserializeShort1Dec(byte[] value) {
    short value2 = BitConverter.ToInt16(value, 0);
    return (double)value2 / 10.0;
}

很明显,(带符号的)短整数(16位)的范围是-32,768至32,767,因此很清楚,您只有4个全数字和一个小数(0-3),即一个(有符号的)int(32位)是−2,147,483,648到2,147,483,647,因此,很明显,您只有9位全数字和一小部分(0-2).转到(带符号的)长(64位),您有-9,223,372,036,854,775,808到9,223,372,036,854,775,807,因此18位数字加上(大)个数.使用浮点数会降低精度.浮点数(32位)的精度约为7位,而双精度点(64位)的精度约为15-16位.

So that it's clear, the range of a (signed) short (16 bits) is -32,768 to 32,767 so it's quite clear that you only have 4 full digits plus a little piece (the 0-3), the range of a (signed) int (32 bits) is −2,147,483,648 to 2,147,483,647 so it's quite clear that you only have 9 full digits plus a little piece (the 0-2). Going to a (signed) long (64 bits) you have -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 so 18 digits plus a (big) piece. Using floating points you lose in accuracy. A float (32 bits) has an accuracy of around 7 digits, while a double (64 bits) has an accuracy of around 15-16 digits.

这篇关于将数据类型'long'转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆