Python code to c # reference example,

Source: Internet
Author: User
Tags hmac

Python code to c # reference example,

Recently, I am working on converting Pyhton code to c # code. The following cases have been tested in person. Now I want to provide a reference for helpful students:

 

Python | C #

Python C #
Datetime. datetime. strftime (datetime. datetime. now (), '% Y % m % d % H % M % s ') DateTime. Now. ToString ("yyyyMMddHHmmss ")
Random. choice ('123 ') 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 is c ++ call #
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 ))
# Call Objdll. encode as c ++ #
Return szPara. value [: decLen]
Bytes [] 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)

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.