>  기사  >  백엔드 개발  >  Python 공통 유형 변환 구현

Python 공통 유형 변환 구현

Guanhui
Guanhui앞으로
2020-07-28 18:22:142800검색

Python 공통 유형 변환 구현

1. byte와 str

b = b"example" 
s = "example" 
bytes(s, encoding = "utf8") 
str(b, encoding = "utf-8")


2.


3. byte와 float

b=b'\x01\x02'
num=int.from_bytes(b,'little')
b1=num.to_bytes(2,'little')

4. str과 bytearray

import struct
s=b'@zQ\x16'
def byteToFloat(b):
  return struct.unpack('!f',s)[0]

def floatToBytes(f):
  bs = struct.pack("f",f)
  return bytes((bs[3],bs[2],bs[1],bs[0]))
f1=byteToFloat(s)
floatToBytes(f1)
권장 튜토리얼: "Python Tutorial

"

위 내용은 Python 공통 유형 변환 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 jb51.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제