Python代码转c#部分参考样例
最近在做一部分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) |
Python代码转c#部分参考样例的更多相关文章
- 异步nodejs代码的同步样子写法样例
异步nodejs代码的同步样子写法样例 js的异步嵌套太深代码将不好看.尤其在用node的时候这种情况会大量出现. 这里用node连接redis,做一个用户注册的简单例子来说明.例如用redis做存储 ...
- Python Socket 编程——聊天室演示样例程序
上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和client的代码了解主要的 Python Socket 编程模型.本文再通过一个样例来加强一下对 Socket ...
- D3的参考样例
官网进去就可以看到很多样例了.但是最喜欢的是mbostock的http://bl.ocks.org 然后其它的也有一些: 看上去很酷--http://www.visualcinnamon.com/po ...
- python模块 - re模块使用演示样例
http://blog.csdn.net/pipisorry/article/details/46619179 re模块匹配规则见:http://blog.csdn.net/pipisorry/art ...
- 吴裕雄 python 神经网络——TensorFlow 图像预处理完整样例
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def distort_color(image, ...
- C++ Primer中文本查询演示样例Query的实现
近期在看C++ Primer复习C++的语法,看到书中15.9章中的文本查询演示样例时,认为设计得非常不错,于是便动手照着实现了一个,改动了非常久最终执行成功了,从中也学习到了非常多的语法.以下把实现 ...
- [Python] SQLBuilder 演示样例代码
用Python写一个SQLBuilder.Java版能够从 http://www.java2s.com/Code/Java/Database-SQL-JDBC/SQLBuilder.htm 看到. 附 ...
- Python Web框架Tornado的异步处理代码演示样例
1. What is Tornado Tornado是一个轻量级但高性能的Python web框架,与还有一个流行的Python web框架Django相比.tornado不提供操作数据库的ORM接口 ...
- Python 100个样例代码【爆肝整理 建议收藏】
本教程包括 62 个基础样例,12 个核心样例,26 个习惯用法.如果觉得还不错,欢迎转发.留言. 一. Python 基础 62 例 1 十转二 将十进制转换为二进制: >>> b ...
随机推荐
- JS笔记(三):数组、函数、类
(一) 数组 //创建数组 var the_array = [1,2,3,4,'5'] console.log(the_array[0]) //读取索引为0的数据 the_array[5] = '赋值 ...
- Hibernate 中的 idclass mapping 问题
关于出现 idclass mapping 运行错误 @IdClass 注释通常用于定义包含复合键id的Class.即多个属性的关键复合. @IdClass(CountrylanguageEntityP ...
- 4.1Python数据处理篇之Matplotlib系列(一)---初识Matplotlib
目录 目录 前言 (一)matplotlib的介绍 (二)画一个简单的画布 ==1.源代码== ==2.展示效果== (三)画布按键的功能介绍 ==1.对于画布功能键的排序== ==(1)主页== = ...
- python基础 - 变量与运算符
变量与运算符 变量 定义一个变量 a = [1,2,3,4,5,6] print(a) # [1,2,3,4,5,6] 变量命名要求: 首字母不能是数字 只能包含字符数字下划线 不能是关键字 type ...
- ]remove-duplicates-from-sorted-list-ii (删除)
题意略: 思路都在注解里: #include<iostream> #include<cstdio> using namespace std; struct ListNode { ...
- canvas.toDataURL 画布导出图片
<html> <meta http-equiv="X-UA-Compatible" content="chrome=1"> <he ...
- Excel中concatenate函数的使用方法
你还在为Excel中concatenate函数的使用方法而苦恼吗,今天小编教你Excel中concatenate函数的使用方法,让你告别Excel中concatenate函数的使用方法的烦恼. 经验主 ...
- PLSQL Developer12连接远程Oracle xe 11g
目标: .学习使用docker下载oracle xe 11g .使用PLSQL连上启动运行oracle xe 11g容器服务 1.准备工作 Linux 安装docker 下载PLSQL Develop ...
- docker in docker 出现 libltdl.so.7 问题
# docker ps -adocker: error while loading shared libraries: libltdl.so.7: cannot open shared object ...
- UCML异常提示:无效URI
UCML异常提示界面,点击确定后UCML退出无法使用,原因见图二 图一: 图二:源码路径错误导致找不到路径出异常提示,在数据库中将数据update回正确路径即可解决该问题