最近在做一部分Pyhton代码转c#代码的工作,以下案例亲自都测试过,现整理出来希望对有帮助的同学提供参考:
Python | C#
*:first-child{margin-top:0 !important}body>*:last-child{margin-bottom:0 !important}p,blockquote,ul,ol,dl,table,pre{margin:15px 0}h1,h2,h3,h4,h5,h6{margin:20px 0 10px;padding:0;font-weight:bold;-webkit-font-smoothing:antialiased}h1 tt,h1 code,h2 tt,h2 code,h3 tt,h3 code,h4 tt,h4 code,h5 tt,h5 code,h6 tt,h6 code{font-size:inherit}h1{font-size:28px;color:#000}h2{font-size:24px;border-bottom:1px solid #ccc;color:#000}h3{font-size:18px}h4{font-size:16px}h5{font-size:14px}h6{color:#777;font-size:14px}body>h2:first-child,body>h1:first-child,body>h1:first-child+h2,body>h3:first-child,body>h4:first-child,body>h5:first-child,body>h6:first-child{margin-top:0;padding-top:0}a:first-child h1,a:first-child h2,a:first-child h3,a:first-child h4,a:first-child h5,a:first-child h6{margin-top:0;padding-top:0}h1+p,h2+p,h3+p,h4+p,h5+p,h6+p{margin-top:10px}a{color:#4183c4;text-decoration:none}a:hover{text-decoration:underline}ul,ol{padding-left:30px}ul li>:first-child,ol li>:first-child,ul li ul:first-of-type,ol li ol:first-of-type,ul li ol:first-of-type,ol li ul:first-of-type{margin-top:0}ul ul,ul ol,ol ol,ol ul{margin-bottom:0}dl{padding:0}dl dt{font-size:14px;font-weight:bold;font-style:italic;padding:0;margin:15px 0 5px}dl dt:first-child{padding:0}dl dt>:first-child{margin-top:0}dl dt>:last-child{margin-bottom:0}dl dd{margin:0 0 15px;padding:0 15px}dl dd>:first-child{margin-top:0}dl dd>:last-child{margin-bottom:0}pre,code,tt{font-size:12px;font-family:Consolas,"Liberation Mono",Courier,monospace}code,tt{margin:0;padding:0;white-space:nowrap;border:1px solid #eaeaea;background-color:#f8f8f8;border-radius:3px}pre>code{margin:0;padding:0;white-space:pre;border:0;background:transparent}pre{background-color:#f8f8f8;border:1px solid #ccc;font-size:13px;line-height:19px;overflow:auto;padding:6px 10px;border-radius:3px}pre code,pre tt{background-color:transparent;border:0}blockquote{border-left:4px solid #DDD;padding:0 15px;color:#777}blockquote>:first-child{margin-top:0}blockquote>:last-child{margin-bottom:0}hr{clear:both;margin:15px 0;height:0;overflow:hidden;border:0;background:transparent;border-bottom:4px solid #ddd;padding:0}table th{font-weight:bold}table th,table td{border:1px solid #ccc;padding:6px 13px}table tr{border-top:1px solid #ccc;background-color:#fff}table tr:nth-child(2n){background-color:#f8f8f8}img{max-width:100%}
-->
Python | C# |
---|---|
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"; |
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) |