• 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
    
    • 1
    • 2

    Golang:

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

    1.2 String => Bytes

    Python:

    s = 'hello'
    byte_array = s.encode() # 默认utf-8
    byte_array = bytes(s, encoding='utf-8')
    byte_array = b'hello'
    
    • 1
    • 2
    • 3
    • 4

    Golang:

    s := "hello"
    byteArray := []byte(s)
    
    • 1
    • 2

    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'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Golang:

    byteArray := []byte("中国")
    
    hexStr := hex.EncodeToString(byteArray)   // e4b8ade59bbd
    
    • 1
    • 2
    • 3

    1.4 Hex => Bytes

    Python:

    byte_array = bytes.fromhex(hex_str)
    
    • 1

    Golang:

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

    1.5 字符串长度

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

    s = 'Hello, 世界'
    len(s)             # 9
    len(s.encode())    # 13
    
    • 1
    • 2
    • 3

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2. Int & String

    2.1 Int => String

    Python:

    n = 10
    s = str(n)
    s = '{}'.format(n)
    
    • 1
    • 2
    • 3

    Golang:

    n := 10
    s := strconv.Itoa(n)
    
    • 1
    • 2

    2.2 Int => String with base

    Python:

    # 16 进制
    s = hex(n)
    
    # 2 进制
    s = bin(n)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.3 String => Int

    Python:

    n = int('10')
    
    n = int('0xa', 16)
    n = int('a', 16)
    
    n = int('0b1010', 2)
    n = int('1010', 2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    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'
    
    • 1
    • 2
    • 3

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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]
    
    • 1
    • 2
    • 3
    • 4

    Golang:

    byteArray := []byte{0xd2, 0x04, 0x00, 0x00}
    
    var n int32
    bytesBuffer := bytes.NewBuffer(byteArray)
    binary.Read(bytesBuffer, binary.LittleEndian, &n)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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()   # '中国'
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Golang:

    byteArray := []byte{0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0x00, 0x00, 0x00, 0x00}
    
    n := bytes.IndexByte(byteArray[:], 0)
    s := string(byteArray[:n])   // 中国
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    【pen200-lab】10.11.1.75
    第四章 文件管理 十、文件系统的全局结构
    自定义实现乘风破浪的小船
    蓝桥杯(3.15 刷真题)
    剑指offer——JZ84 二叉树中和为某一值的路径(三) 解题思路与具体代码【C++】
    AI艺术的背后:详解文本生成图像模型【基于GAN】
    【BUG】记录使用PageHelper分页工具出现的问题?全是细节~
    [JAVA]通过HashMap实现的增删改查小例子
    100个python算法超详细讲解:哥德巴赫猜想
    Redis 强化之一
  • 原文地址:https://blog.csdn.net/elihe2011/article/details/125533567