2011-08-26 73 views
15

我得值(雙/漂浮在C#)轉換爲字節,需要一些幫助..

//數據類型長4個字節的-99999999,99到99999999,99
//數據類型長4個字節的-99999999,9到99999999,9
//數據類型短2字節-999,99至999,99
//數據類型短2字節-999,9到999,9

在我的「世界在家「我只是將它和ASCII.GetBytes()串起來。

但現在,在這個世界上,我們必須減少可能的空間。
事實上,'-99999999,99'需要12個字節而不是4個!如果它是'長'數據類型。轉換數據類型「長」到字節數組

[編輯]
由於一些幫助和回答我在這裏附上了一些成果,

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保持較低的值,即99951(我將不包括ToString()樣本),它也得到了8個字節。

如果該值甚至「更短」,即-999,99-999,99,則只需要2個字節的空間。
[編輯完]

+0

你知道多空是整數類型,所以沒有小數,對不對?而長的是8字節,而不是4!那是「int」。 – xanatos

+1

當問題甚至沒有意義時,我驚訝地發現有多少個答案。他得到了什麼,他想得到什麼? – 2011-08-26 08:54:20

+0

@xanatos你的觀點值得一提。這些值在開始時是浮動的(但這是另一個問題)。 – Independent

回答

5

請注意,在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; 
} 

所以這很明顯,的範圍(簽字)短(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位。

15

你檢查BitConverter

long lng =-9999999999L; 
byte[] mybyt = BitConverter.GetBytes(lng); 

希望這是你在找什麼

+0

請注意,結果取決於您的機器的字節順序。 – mpartel

5

嘗試做這樣:

long l = 4554334; 

byte[] bA = BitConverter.GetBytes(l); 
1
long longValue = 9999999999L; 

     Console.WriteLine("Long value: " + longValue.ToString()); 

     bytes = BitConverter.GetBytes(longValue); 

     Console.WriteLine("Byte array value:"); 

     Console.WriteLine(BitConverter.ToString(bytes)); 
1

正如其他答案指出的那樣,您可以使用BitConverter來獲取原始類型的字節表示。

你說,你居住在當今世界,沒有對代表這些數值簡潔成爲可能,在這種情況下,你應該考慮variable length encoding(儘管該文件可能是一個有點抽象)的責任。

如果你決定這個方法適用於你的情況我建議看如何Protocol Buffers項目represents scalar types一些這些類型的使用,如果數據集有利於較小的絕對值產生短輸出可變長度編碼編碼。 (該項目開放源代碼爲New BSD license,因此您將能夠學習從source repository採用的技術,甚至可以在您自己的項目中使用該源代碼。)