2012-07-10 73 views
0

我得到這個字符串 8802000030000000C602000033000000000000800000008000000000000000001800000000000小尾數爲整數

,這就是我期待從字符串轉換,

88020000 long in little endian => 648 
    30000000 long in little endian => 48 
    C6020000 long in little endian => 710 
33000000 long in little endian => 51 

左邊是我從字符串,右所獲得的價值邊是我期待的價值。右側的值可能是錯誤的,但有什麼辦法可以從左側獲得右側值?

我通過幾個線程去喜歡這裏

How to convert an int to a little endian byte array?

C# Big-endian ulong from 4 bytes

我嘗試完全不同的功能,但沒有給我值周圍或附近我所期待的。

更新: 我在閱讀下面的文本文件。大部分數據都是文本格式的當前數據,但突然間我收到了一堆GRAPHICS信息,我不知道如何處理它。

RECORD=28 

cVisible=1 
dwUser=0 
nUID=23 
c_status=1 
c_data_validated=255 
c_harmonic=0 
c_dlg_verified=0 
c_lock_sizing=0 
l_last_dlg_updated=0 
s_comment= 
s_hlinks= 
dwColor=33554432 
memUsr0= 
memUsr1= 
memUsr2= 
memUsr3= 
swg_bUser=0 
swg_dConnKVA=L0 
swg_dDemdKVA=L0 
swg_dCodeKVA=L0 
swg_dDsgnKVA=L0 
swg_dConnFLA=L0 
swg_dDemdFLA=L0 
swg_dCodeFLA=L0 
swg_dDsgnFLA=L0 
swg_dDiversity=L4607182418800017408 
cStandard=0 
guidDB={901CB951-AC37-49AD-8ED6-3753E3B86757} 
l_user_selc_rating=0 
r_user_selc_SCkA= 
a_conn1=21 
a_conn2=11 
a_conn3=7 
l_ct_ratio_1=x44960000 
l_ct_ratio_2=x40a00000 
l_set_ct_ratio_1= 
l_set_ct_ratio_2= 

c_ct_conn=0 

    ENDREC 
GRAPHICS0=8802000030000000C602000033000000000000800000008000000000000000001800000000000 
EOF 
+0

爲什麼在小尾數不等於88020000 648? 88020000是十六進制中的53F1420,如果反轉字節,則變爲20143F05,即十進制爲538197765。 – 2012-07-10 22:22:39

+1

@Robert:88020000已經在十六進制中,第三個數據包含非十進制數字這一事實就證明了這一點。 – 2012-07-10 22:23:01

+0

@RobertHarvey沒有什麼混淆。對於那個很抱歉。 – 2012-07-10 22:53:41

回答

2

根據您想如何解析了輸入字符串,你可以做這樣的事情:

string input = "8802000030000000C6020000330000000000008000000080000000000000000018000000"; 

for (int i = 0; i < input.Length ; i += 8) 
{ 
    string subInput = input.Substring(i, 8); 
    byte[] bytes = new byte[4]; 
    for (int j = 0; j < 4; ++j) 
    { 
     string toParse = subInput.Substring(j * 2, 2); 
     bytes[j] = byte.Parse(toParse, NumberStyles.HexNumber); 
    } 

    uint num = BitConverter.ToUInt32(bytes, 0); 
    Console.WriteLine(subInput + " --> " + num); 
} 

88020000 --> 648 
30000000 --> 48 
C6020000 --> 710 
33000000 --> 51 
00000080 --> 2147483648 
00000080 --> 2147483648 
00000000 --> 0 
00000000 --> 0 
18000000 --> 24 
+0

你的代碼非常好。這真的很快答案。我只是改變了一件事,而不是「uint num = BitConverter.ToUInt32(bytes,0); 」我使用「int num = BitConverter.ToInt32(bytesi,0);」 – 2012-07-10 22:48:44

2

你真的字面意思是說這是一個字符串嗎?它看起來像這樣:你有一堆32位字,每個字由8個十六進制數字表示。每個以小端順序呈現,首先是低字節。你需要將每個解釋爲一個整數。所以,例如,88020000是88 02 00 00,也就是0x00000288。

如果你可以明確地說明它是什麼 - 一個字符串,一種數字類型的數組,或者什麼 - 那麼它會更容易爲你提供進一步的建議。

+0

你說得對; 0x00000288是十進制648。 – 2012-07-10 22:28:21

+0

你說得對。 @ itsme86的代碼正在做我所需要的。 – 2012-07-10 22:52:05