1. 程式人生 > >Python代碼轉c#部分參考樣例

Python代碼轉c#部分參考樣例

ron adb auto ++ white esp time lis frame

最近在做一部分Pyhton代碼轉c#代碼的工作,以下案例親自都測試過,現整理出來希望對有幫助的同學提供參考:

Python | C#

PythonC#
datetime.datetime.strftime(datetime.datetime.now(), ‘%Y%m%d%H%M%S‘) DateTime.Now.ToString("yyyyMMddHHmmss")
random.choice(‘123456789‘) random.Next(1, 9).ToString()
struct.pack(‘>I‘, int(time.time())) TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
byte[] timeSpanBytes = BitConverter.GetBytes(Convert.ToUInt32(ts));
if (BitConverter.IsLittleEndian)
{
Array.Reverse(timeSpanBytes);
}
binascii.hexlify(ab) BitConverter.ToString(timeSpanBytes)
random.randint(0, 100000000)) Random random = new Random(DateTime.Now.Millisecond);
random.Next(0, 100000000)
myhmac = hmac.new("d6fc3a4a06adbde89223bvefedc24fecde188aaa9161",digestmod=hashlib.sha1)
myhmac.update(binascii.unhexlify(‘57b47f0a1b8a35a00300fbe94bcf‘))
encode=base64.b64encode(myhmac.digest())
string hexData = "57b47f0a1b8a35a00300fbe94bcf";
if(hexvalue.Length % 2 != 0)
{
hexvalue = "0" + hexvalue;
}
int len = hexvalue.Length / 2;
byte[] bytes = new byte[len];
for (int i = 0; i < len; i++)
{
string byteString = hexvalue.Substring(2 * i, 2);
bytes[i] = Convert.ToByte(byteString, 16);
}

string str = "d6fc3a4a06adbde89223bvefedc24fecde188aaa9161";
ASCIIEncoding encoder = new ASCIIEncoding();
Byte[] code = encoder.GetBytes(str);
HMACSHA1 hmSha1 = new HMACSHA1(code);
Byte[] hmBytes = hmSha1.ComputeHash(bytes);
string encode = Convert.ToBase64String(hmBytes);
bytes=binascii.unhexlify(hexvalue) if (hexvalue.Length % 2 != 0)
{
hexvalue = "0" + hexvalue;
}
int len = hexvalue.Length / 2;
byte[] bytes = new byte[len];
for (int i = 0; i < len; i++)
{
string byteString = hexvalue.Substring(2 * i, 2);
bytes[i] = Convert.ToByte(byteString, 16);
}
return bytes;
var hmac=hashlib.md5(‘F%s%s‘ % (time_str, device_no)).hexdigest() var md5 = new MD5CryptoServiceProvider();
byte[] m =md5.ComputeHash(Encoding.UTF8.GetBytes($"F{timeSpan}{deviceNO}"));
var hmac = BitConverter.ToString(m).Replace("-", "").ToLower();
buf_size = 0x1000
raw_memory = bytearray(buf_size)
ctypes_raw_type = (ctypes.c_char * buf_size)
ctypes_raw_memory=ctypes_raw_type.from_buffer(raw_memory)
encLen = Objdll.encode(byref(ctypes_raw_memory),buf_size,inputCode,len(inputCode))#Objdll.encode為c++調用#
return raw_memory[:encLen]
IntPtr data = Marshal.StringToHGlobalAnsi(inputCode);
byte[] aaab = new byte[4096]; int aa = encode(aaab, 4096, data, inputCode.Length);byte[] byteNew = new byte[aa];
for (int i = 0; i < aa; i++)
{
byteNew[i] = aaab[i];
}
return byteNew;
szPara = create_string_buffer(‘/0‘*buf_size)
decLen = Objdll.decode(byref(szPara), buf_size,decodeInput,len(decodeInput))
#Objdll.encode為c++調用#
return szPara.value[:decLen]
byte[] outsting = new byte[0x1000];
int encLen = decode(outsting, outsting.Length, inputCode, inputCode.Length);
String ret = Encoding.UTF8.GetString(outsting, 0, encLen);
return ret;
json.loads(test) JsonConvert.DeserializeObject(test)

Python代碼轉c#部分參考樣例