将数据类型'long'转换为字节数组

Ind*_*ent 17 c# arrays serialization byte

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

.//数据类型长4byte -99999999,99到99999999,99
//数据类型长4byte -99999999,9到99999999,9
//数据类型短2byte -999,99到999,99
//数据类型短2byte -999,9到999,9

在我的"家里的世界"中我只是将它和ASCII.GetBytes()串起来.

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

[编辑]
由于一些帮助和答案,我在这里附上一些结果,

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
Run Code Online (Sandbox Code Playgroud)

还有一个细节需要找出来.lng-variabel得到8字节,即使它保持较低的值,即99951(我不会包括ToString()样本).

如果该值甚至是"更短",这意味着-999,99 - 999,99它只需要2个字节的空间.
[结束编辑]

V4V*_*tta 26

你检查过了吗 BitConverter

long lng =-9999999999L;
byte[] mybyt = BitConverter.GetBytes(lng);
Run Code Online (Sandbox Code Playgroud)

希望这就是你所期待的

  • 请注意,结果取决于您机器的字节序。 (2认同)

小智 6

尝试以这种方式做到:

long l = 4554334;

byte[] bA = BitConverter.GetBytes(l);
Run Code Online (Sandbox Code Playgroud)


xan*_*tos 6

请注意,在2个字节中,您只能有4个完整数字+符号,而在4个字节中,您只能有9个数字+符号,因此我必须相应地缩放您的先决条件.

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;
}
Run Code Online (Sandbox Code Playgroud)

所以很明显,(签名)短(16位)的范围是-32,768到32,767,所以很明显你只有4位全数加上一小块(0-3),a的范围(签名) )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位.


Ale*_*lex 5

致所有阅读此问题及其答案的人。请注意:

//convert to bytes
long number = 123;
byte[] bytes = BitConverter.GetBytes(number);

//convert back to int64
long newNumber = BitConverter.ToInt64(bytes);

//numbers are different on x64 systems!!!
number != newNumber;
Run Code Online (Sandbox Code Playgroud)

解决方法是检查您运行的系统:

newNumber = BitConverter.IsLittleEndian
    ? BitConverter.ToInt64(bytes, 0)
    : BitConverter.ToInt64(bytes.Reverse().ToArray(), 0);
Run Code Online (Sandbox Code Playgroud)