python 整数 1字节_Python程序打印代表整数的字节数组

python 整数 1字节

Given an integer number and we have to convert it into a byte array in Python.

给定一个整数,我们必须在Python中将其转换为字节数组。

To convert an integer number into bytes (byte array), we use to_bytes() method of int class, it is called with the number with three arguments and returns a byte array representing the number.

要将整数转换为字节(字节数组) ,我们使用int类的to_bytes()方法 ,将其与带有三个参数的数字一起调用,并返回表示该数字的字节数组。

Syntax:

句法:

    int.to_bytes(size, byteorder)

Here,

这里,

  • size is the maximum size (in bytes) of the number.

    size是数字的最大大小(以字节为单位)。

  • byteorder is the technique to print the bytes, it has two values big to print bytes array in big-endian format and little to print bytes array in little-endian format.

    byteorder是一种打印字节的技术,它具有两个值,分别大值以big-endian格式打印字节数组和很少值以little-endian格式打印字节数组。

Example:

例:

    Input:
num = 100
# function call
print(num.to_bytes(2, byteorder ='big'))
Output:
b'\x00d'

Python代码将整数转换为字节数组 (Python code to convert an integer number to bytes array )

# Python program to find number of bits 
# necessary to represent an integer in binary
# input a number
num = int(input("Enter an integer number: "))
# total bits to represent number
bits = num.bit_length()
print("bits required to store ", num, " = ", bits)
print("binary value of ", num, " is = ", bin(num))

Output

输出量

First run:
Enter an integer number: 67
bits required to store  67  =  7
binary value of  67  is =  0b1000011
Second run:
Enter an integer number: 3
bits required to store  3  =  2
binary value of  3  is =  0b11

If number is longer than 2 bytes value,

如果数字大于2个字节的值,

If the input number is larger than 2 bytes and we used size "2 bytes" with the to_bytes() method, then an "OverflowError" error ("int too big to convert") will occur.

如果输入数字大于2个字节,并且我们使用to_bytes()方法使用大小“ 2个字节” ,则会发生“ OverflowError”错误( “ int太大而无法转换” )。

Enter an integer number: 12387615
Traceback (most recent call last):
File "/home/main.py", line 8, in <module>
x = num.to_bytes(2, byteorder ='big')
OverflowError: int too big to convert

to_bytes()数组,大小为“ 4个字节” (to_bytes() array with size "4 bytes")

In the previous example, we used size "2 bytes", if the number is larger than it, we can increase the size. Here, in this example – we are using size "4 bytes", thus, we can convert an integer till 4 bytes.

在前面的示例中,我们使用大小“ 2个字节” ,如果数字大于它,则可以增加大小。 在此示例中,这里我们使用的大小为“ 4 bytes” ,因此,我们可以将整数转换为4个字节。

# Python program to print an array of bytes 
# representing an integer
# input an integer number
num = int(input("Enter an integer number: "))
# finding the byte array
x = num.to_bytes(4, byteorder ='big')
# printing byte array
print("x = ", x)

Output

输出量

First run:
Enter an integer number: 12387615
x =  b'\x00\xbd\x05\x1f'
Second run:
Enter an integer number: 9999876
x =  b'\x00\x98\x96\x04'

以little-endian格式打印bytes数组 (Printing the bytes array in little-endian format)

To print bytes array of an integer number, we can define byteorder value with little. In this example, we are converting an integer number (till 4 bytes) to bytes array in little-endian order.

要打印一个整数的字节数组,我们可以使用little定义字节序值。 在此示例中,我们将一个整数(至多4个字节)以little-endian顺序转换为bytes数组。

# Python program to print an array of bytes 
# representing an integer
# input an integer number
num = int(input("Enter an integer number: "))
# finding the byte array
x = num.to_bytes(4, byteorder ='little')
# printing byte array
print("x = ", x)

Output

输出量

First run:
Enter an integer number: 12387615
x =  b'\x1f\x05\xbd\x00'
Second run:
Enter an integer number: 9999876
x =  b'\x04\x96\x98\x00'

翻译自: https://www.includehelp.com/python/print-an-array-of-bytes-representing-an-integer.aspx

python 整数 1字节

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380814.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PHP的数据类型、浮点型比较

在介绍php的数据类型前&#xff0c;先说一说强数据类型和弱数据类型。 弱数据类型&#xff1a;变量的类型取决于存放值的类型 强数据类型&#xff1a;变量的类型取决于申明变量时的类型。比如申明变量是A类型就不能存放B类型 PHP是弱数据类型&#xff0c;php支持8种原始数据类型…

《测试驱动开发》读书笔记

最终目标是整洁可用的代码 我们不是从建立对象开始&#xff0c;而是从测试开始 了解需求-》设计测试 -》让测试通过 列出所有已知问题&#xff0c;然后一个一个解决&#xff1b; 培养将软件开发化为一小步一小步开发任务的能力 测试程序与代码所存在的问题不在于重复设计&#…

java sampling_Java机器学习库ML之三Sampling(采样)

场景&#xff1a;从样本集中采样80%用于训练&#xff0c;20%用于验证。参考代码如下&#xff1a;package com.gddx;import java.io.File;import java.util.Map;import libsvm.LibSVM;import net.sf.javaml.classification.Classifier;import net.sf.javaml.classification.eval…

puppeteer api_使用Node.js和Puppeteer API生成PDF文件

puppeteer apiPuppeteer is a Node library developed by Google and provides a high-level API for developers. Puppeteer是Google开发的Node库&#xff0c;并为开发人员提供了高级API。 With Node.js already up and running, we will install puppeteer via NPM (node pa…

php中进制转换

我们知道&#xff0c;进制有二进制、八进制、十进制、十六进制&#xff0c;但在php中只能存取八进制、十进制、十六进制 在讲进制转换之前&#xff0c;我们先说一下进制单词的缩写&#xff1a; 二进制&#xff1a;bin八进制&#xff1a;oct十进制&#xff1a;dec十六进制&…

java canvas画圆圈_java – 在视图上绘制一个圆圈(android)

几点意见&#xff1a;在确定圆的中心点和半径时,您需要考虑分配给视图的宽度和高度.您应该考虑分配给视图的填充,这样就不会绘制该保留部分.你应该避免在onDraw方法中分配对象,因为这会被调用很多.为了允许在XML布局中指定视图,您需要提供带有Context和AttributeSet的构造函数.…

第七章:项目成本管理

项目成本管理包括对成本进行估算、预算和控制的各过程&#xff0c;从而确保项目在批准的预算内完工。其包括 估算成本&#xff1a;对完成项目活动所需资金进行近似估算的过程制定预算&#xff1a;汇总所有单个活动或工作包的估算成本&#xff0c;建立一个经批准的成本基准的过程…

python rgb 图像_在Python中查找RGB图像的互补图像

python rgb 图像Complementary image is a transformed image such that it consists of complementary colours of the ones, which is present in the original image. 互补图像是一种变换后的图像 &#xff0c;它由原始图像中存在的互补色组成。 For finding the complemen…

php的字符串、双引号输出变量的问题、转义字符

字符串 php中字符串可以用单引号和双引号表示&#xff0c;但单引号效率比双引号高&#xff0c;因为单引号是真正的字符串&#xff0c;双引号要做运算&#xff0c;即将字符串中的变量替换成值&#xff0c;单引号不需要 看下面的例子 <?phpheader(content-type:text/html;…

jmeter从mysql取值_Jmeter获取数据库值并作为参数请求(转载)

转载自&#xff1a;https://www.cnblogs.com/mawenqiangios/p/11088672.html01Jmeter连接数据库1、添加JDBC Connection Configuration(右键测试计划-->配置元件-->JDBC Connection Configuration)2、配置数据库连接信息&#xff0c;其中DataBase URL&#xff1a;jdbc:my…

圣斗士星矢

一部漏洞百出&#xff0c;情节重复&#xff0c;对白肉麻啰唆&#xff0c;人物刻画单一的动漫绵延了近二十年80一代的情结&#xff0c;每一个人都曾用稚嫩的声音&#xff0c;以“庐山升龙霸”抑或是“凤翼天翔”怒吼&#xff0c;今天&#xff0c;讨论的问题是&#xff1a;他&…

用于将类型从double转换为int的C#程序

Given a double type of variable, we have to convert it into an integer in C#. 给定双重类型的变量&#xff0c;我们必须在C&#xff03;中将其转换为整数。 Syntax: 句法&#xff1a; int_variable (int)double_variable;Example: 例&#xff1a; Input:double a 123…

ASCII码

ASCII 码使用指定的7 位或8 位二进制数组合来表示128 或256 种可能的字符。标准ASCII 码也叫基础ASCII码&#xff0c;使用7 位二进制数&#xff08;剩下的1位二进制为0&#xff09;来表示所有的大写和小写字母&#xff0c;数字0 到9、标点符号&#xff0c;以及在美式英语中使用…

如果有一天生你养你的两个人都走了

如果有一天 生你养你的两个人都走了 这世间唯一与你有着最亲密血缘关系的人都不在了 所以 孩子们啊 人在世的时候 要对父母好点 别让父母总是为你们操心 父母不需要你挣多少钱 但他们很需要子女的陪伴 因为子女是父母最深的牵挂如果有一天 生你养你的两个人都走了 这世间…

MySQL 纯insert_MySQL使用INSERT插入多条记录

MySQL使用INSERT插入多条记录&#xff0c;应该如何操作呢&#xff1f;下面就为您详细介绍MySQL使用INSERT插入多条记录的实现方法&#xff0c;供您参考。看到这个标题也许大家会问&#xff0c;这有什么好说的&#xff0c;调用多次INSERT语句不就可以插入多条记录了吗&#xff0…

php中对ASCII码的处理ord() 、chr()

和ASCII有关的部分函数 ord(): 函数返回字符串的首个字符的 ASCII 值 <?phpheader(content-type:text/html;charsetutf-8);echo ord(A),<br>;echo ord(ABC),<br>; ?>chr():指定的 ASCII 值返回字符&#xff0c;ASCII 值可被指定为十进制值、八进制值或十…

c# sizeof_C#程序演示sizeof()运算符的示例

c# sizeofC&#xff03;sizeof()运算符 (C# sizeof() operator) It is used to obtain the size of a data type in bytes (primitive data type). 它用于获取以字节为单位的数据类型的大小(原始数据类型)。 Syntax: 句法&#xff1a; sizeof(type);sizeof(int); //returns 4…

高手要耐得住寂寞

应该有不少高手会抱怨&#xff1a;为什么CSDN首页推荐的都是一些没有技术含量的文章&#xff1f;而我自己写的含金量非常高的&#xff0c;却很少能获得推荐&#xff1f; 其实这是一个很有意思的问题。程序员这职业&#xff0c;和别的职业相比&#xff0c;所占的人口比率肯定不高…

PHP中字符串定界符

作用&#xff1a;为输出大量的文本提供简单方法 格式&#xff1a; <<<名字 //代码 名字;注意&#xff1a; 开始和结束的名字必须一样&#xff0c;名字的命名格式和变量一样&#xff0c;结束的名字必须顶格写 <?phpheader(content-type:text/html;charsetutf-8);…

c语言 函数的参数传递示例_nexttoward()函数以及C ++中的示例

c语言 函数的参数传递示例C nexttoward()函数 (C nexttoward() function) nexttoward() function is a library function of cmath header, it is used to get the next representable value after the given first number in the direction of given second number, it accep…