Python代码转c#部分参考样例

Posted 随遇而安

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python代码转c#部分参考样例相关的知识,希望对你有一定的参考价值。

最近在做一部分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#部分参考样例的主要内容,如果未能解决你的问题,请参考以下文章

使用ffmpeg实现转码样例(代码实现)

C#支付宝扫码支付代码完整版(转)

excel转json工具的制作(C#语言)

C#按钮实现网页跳转?

C#编程 10进制和26进制互转

[转]C#集合类型大揭秘