2012-02-05 86 views
3

我有一个python脚本,我试图转换并卡在一个地方,无法继续。请在下面的代码中查看我曾经提到的“卡在这里”。任何帮助,将不胜感激从二进制数据读取某些数字

原始Python脚本:

import hashlib 
meid = raw_input("Enter an MEID: ").upper() 
s = hashlib.sha1(meid.decode('hex')) 
#decode the hex MEID (convert it to binary!) 
pesn = "80" + s.hexdigest()[-6:].upper() 
#put the last 6 digits of the hash after 80 
print "pESN: " + pesn 

我的C#转换:

UInt64 EsnDec = 2161133276; 
string EsnHex=string.Format("{0:x}", EsnDec); 
string m = Convert.ToString(Convert.ToUInt32(EsnHex, 16), 2); 
/*--------------------------------------------- 
Stuck here. Now m got complete binary data 
and i need to take last 6 digits as per python 
script and prefix "80". 
---------------------------------------------*/ 
Console.WriteLine(m); 
Console.Read(); 
+1

[m.Substring似乎是正确的选择。](http://msdn.microsoft.com/en-us/library/system.string.substring.aspx) – user7116 2012-02-05 16:32:05

+0

同意。要获取字符串's'的最后'n'数字,可以使用:'s.Substring(s.Length - n)'。 – Douglas 2012-02-05 16:33:39

回答

1

怎么是这样的:

static void Main(string[] args) 
{ 
    UInt64 EsnDec = 2161133276; 
    Console.WriteLine(EsnDec); 
    //Convert to String 
    string Esn = EsnDec.ToString(); 
    Esn = "80" + Esn.Substring(Esn.Length - 6); 
    //Convert back to UInt64 
    EsnDec = Convert.ToUInt64(Esn); 
    Console.WriteLine(EsnDec); 
    Console.ReadKey(); 
} 
+0

最后6个十进制字符将是最后20位。我认为OP需要最后6位;这是〜1.5个十进制字符。 – user7116 2012-02-05 16:41:32

2

使用String.Substring

// last 6 characters 
string lastsix = m.Substring(m.Length - 6); 

Console.WriteLine("80{0}", lastsix); 
相关问题