简体   繁体   English

将 int 转换为字符串 ascii 值

[英]convert an int to string ascii value

hello I have an int varible in python and would like to get the value of the integer in a string.您好,我在 python 中有一个 int 变量,并且想在字符串中获取 integer 的值。 For example if I have this variable var:int = 0 How to put the value '\0' in a str variable?例如,如果我有这个变量 var:int = 0 如何将值 '\0' 放入 str 变量中? Thanks for your help.谢谢你的帮助。

U can convert most variable types in python by using a simple function: string_variable= str(int) U 可以使用简单的 function 转换 python 中的大多数变量类型: string_variable= str(int)

Hope this helps希望这可以帮助

Say you have variable var1m that has integer value, to convert it into a string you just need to pass str()假设您有具有 integer 值的变量 var1m,要将其转换为字符串,您只需传递 str()

var1 = 1
var2 = '\\' + str(var1)
print(var2)
print(type(var2))

this will output这将 output

\1
<class 'str'>

I'm stil not entirely sure what you want but you can try bytes :我仍然不完全确定您想要什么,但您可以尝试bytes

>>> bytes([0])
b'\x00'
>>> bytes([1])
b'\x01'
>>> bytes([0, 1])
b'\x00\x01'

This will create a byte object from a list of bytes, meaning numbers between 0 and 255. To get a string use str(bytes(...)) .这将从字节列表中创建一个字节 object,表示 0 到 255 之间的数字。要获取字符串,请使用str(bytes(...))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM