Python代码转c#部分参考样例
最近在做⼀部分Pyhton代码转c#代码的⼯作,以下案例亲⾃都测试过,现整理出来希望对有帮助的同学提供参考:
Python | C#
Python C#
datetime.datetime.strftime(w(), '%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 =
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 =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++)
{
return raw_memory[:encLen]byteNew[i] = aaab[i]; }
return byteNew;
szPara = create_string_buffer('/0'*buf_size)
decLen = Objdll.decode(byref(szPara), buf_size,decodeInput,len(decodeInput)) #de为c++调⽤#
return szPara.value[:decLen]byte[] outsting = new byte[0x1000];python转java代码
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#
转载于:wwwblogs/cby-love/p/8260908.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论