String, Int 和 Byte数组

1. Bytes & String

1.1 Bytes => String

Python:

byte_array = bytes([104, 101, 108, 108, 111])
s = byte_array.decode()  # 默认utf-8

Golang:

byteArray := []byte{104, 101, 108, 108, 111}
s := string(byteArray)

1.2 String => Bytes

Python:

s = 'hello'
byte_array = s.encode() # 默认utf-8
byte_array = bytes(s, encoding='utf-8')
byte_array = b'hello'

Golang:

s := "hello"
byteArray := []byte(s)

1.3 Bytes => Hex

Python:

s = '中国'

byte_array = s.encode()              # b'\xe4\xb8\xad\xe5\x9b\xbd'
hex_str = byte_array.hex()           # 'e4b8ade59bbd'

byte_array1 = binascii.b2a_hex(byte_array)   # b'e4b8ade59bbd'
byte_array = binascii.a2b_hex(byte_array1)   # b'\xe4\xb8\xad\xe5\x9b\xbd'

Golang:

byteArray := []byte("中国")

hexStr := hex.EncodeToString(byteArray)   // e4b8ade59bbd

1.4 Hex => Bytes

Python:

byte_array = bytes.fromhex(hex_str)

Golang:

byteArray, _ = hex.DecodeString(hexStr)   // []byte{0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd}

1.5 字符串长度

Python: len(v): Return the length (the number of items) of an object

s = 'Hello, 世界'
len(s)             # 9
len(s.encode())    # 13

Golang: len(v): Return the number of bytes in String, the number of elements in Slice

s := "Hello, 世界"

len(s)             // 13
len([]byte(s))     // 13

len([]rune(s))                // 9
utf8.RuneCountInString(s)     // 9

2. Int & String

2.1 Int => String

Python:

n = 10
s = str(n)
s = '{}'.format(n)

Golang:

n := 10
s := strconv.Itoa(n)

2.2 Int => String with base

Python:

# 16 进制
s = hex(n)

# 2 进制
s = bin(n)

Golang:

# 16 进制
s := strconv.FormatInt(int64(n), 16)   // a
s := fmt.Sprintf("0x%x", n)            // 0xa

# 2 进制
s := strconv.FormatInt(int64(n), 2)    // 1010
s := fmt.Sprintf("0b%b", n)            // 0b1010

2.3 String => Int

Python:

n = int('10')

n = int('0xa', 16)
n = int('a', 16)

n = int('0b1010', 2)
n = int('1010', 2)

Golang:

s = "10"
n, _ := strconv.Atoi(s)
n, _ := strconv.ParseInt(s, 10, 64)   // int64

s := "a"
n, _ := strconv.ParseInt(s, 16, 32)   // int32

s := "1010"
n, _ := strconv.ParseInt(s, 2, 8)     // int8

3. Int & Bytes

3.1 Int => Bytes

Python:

n = 1234
my_bytes = n.to_bytes(length=4, byteorder='little', signed=False)   # b'\xd2\x04\x00\x00'
my_bytes = struct.pack('<I', n)                                     # b'\xd2\x04\x00\x00'

pack 参数:

参数含义
>大端
<小端
Buint8
bint8
Huint16
hint16
Iuint32
iint32
Luint64
lint64
sascii

Golang:

n := 1234

bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.LittleEndian, int32(n))    // int32 => length=4
byte_array := bytesBuffer.Bytes()    // d2040000

3.2 Bytes => Int

Python:

byte_array = b'\xd2\x04\x00\x00'

n = int.from_bytes(byte_array, byteorder='little', signed=False)
n = struct.unpack('<I', byte_array)[0]

Golang:

byteArray := []byte{0xd2, 0x04, 0x00, 0x00}

var n int32
bytesBuffer := bytes.NewBuffer(byteArray)
binary.Read(bytesBuffer, binary.LittleEndian, &n)

4. Zero bytes in Array

Python:

byte_array = b'\xe4\xb8\xad\xe5\x9b\xbd\x00\x00\x00\x00'
s = byte_array.decode()       # '中国\x00\x00\x00\x00'

n = byte_array.find(b'\x00')
s = byte_array[:n].decode()   # '中国'

Golang:

byteArray := []byte{0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0x00, 0x00, 0x00, 0x00}

n := bytes.IndexByte(byteArray[:], 0)
s := string(byteArray[:n])   // 中国
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值