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 ...
随机推荐
- etcd raft如何实现leadership transfer
leadership transfer可以把raft group中的leader身份转给其中一个follower.这个功能可以用来做负载均衡,比如可以把leader放在性能更好的机器或者离客户端更近的 ...
- 4.7 Sublime Text3 中配置 Python环境 --之上安装Sublime 3
返回总目录 目录: 1.展示效果: 2.缺优分析: 3.下载Sublime Text3 (一)展示效果: 1.能够交互式编写Python代码: 2.可以编写文件式Python代码: 3.能够自动补齐代 ...
- <!DOCTYPE>标签与table高度100% (转)
<!DOCTYPE>标签可声明三种DTD类型,分别表示严格版本.过渡版本以及基于框架的 HTML 文档. 三种HTML文档类型: HTML 4.01 规定了三种文档类型:Strict.Tr ...
- 3星|《AI极简经济学》:AI的预测、决策、战略等方面的应用案例介绍
AI极简经济学 主要内容是AI的各种应用案例介绍.作者把这些案例分到五个部分介绍:预测.决策.工具.战略.社会. 看书名和介绍以为会从经济学的角度解读AI,有更多的新鲜的视角和观点,读后比较失望,基本 ...
- java操作elasticsearch实现基本的增删改查操作
一.在进行java操作elasticsearch之前,请确认好集群的名称及对应的ES节点ip和端口 1.查看ES的集群名称 #进入elasticsearch.yml配置文件/opt/elasticse ...
- VS2015安装与C++进行简单单元测试
1:VS2015是微软最新发布的编译器,http://www.itellyou.cn/这是我们的北航大神助教提供的下载网址,以前我们都是自己在网上找,找到的总不是那么如意,这下大神助教提供的网址就好好 ...
- 获取href连接并跳转
获取href连接: <!DOCTYPE html> <html> <head> <script src="/jquery/jquery-1.11.1 ...
- svn 更新
dev更新流程: 1.打开软件,文件—>打开,弹出右边对话框链接dev地址 2.在窗口输入cd /var/www/user 回车 3.输入svn up 则更新dev代码完成 本地提交到 ...
- 怎么配置 Oracle 侦听器来使用SQL操作ST_Geometry
关于这个内容,其实从ArcSDE9.2推出ST_Geometry就让用户感到很有吸引力,而且特别是在ArcSDE9.3之后,用户使用SQL操作ST_geometry越来越多,但是在配置Oracle监听 ...
- CPU的内部架构和工作原理-原文
CPU从逻辑上可以划分成3个模块,分别是.和,这三部分由CPU内部总线连接起来.如下所示: 控制单元:控制单元是整个CPU的指挥控制中心,由指令寄存器IR(Instruction Register). ...