从二进制数据中读取某些数字

时间:2012-02-05 16:28:20

标签: c# python binary

我有一个python脚本,我试图转换并卡在一个地方,无法继续。请在下面的代码中查看我在哪里提到“Stuck here”。任何帮助将不胜感激

原始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();

2 个答案:

答案 0 :(得分:2)

使用String.Substring

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

Console.WriteLine("80{0}", lastsix);

答案 1 :(得分: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();
}