pack

Demo

packbase64encode
42b44ba7914754901374e4887a2fd51367fa63d0b7d2f48fd78e87054e0992b3c18ca0336d735d5dc9fd8781023e5be63db1121d179976c02940973d9e5c482a12c2e57f78ddd7ec550569afb5bbf6f62b045b5360423457d6ba0b675f5603fa8aa2fd254a99cfcd6435c2a6d20a07456d575ed36ada80b766932b2fcd07ca64

PHP 的实现方式是这样的:

$testStr = "42b44ba7914754901374e4887a2fd51367fa63d0b7d2f48fd78e87054e0992b3c18ca0336d735d5dc9fd8781023e5be63db1121d179976c02940973d9e5c482a12c2e57f78ddd7ec550569afb5bbf6f62b045b5360423457d6ba0b675f5603fa8aa2fd254a99cfcd6435c2a6d20a07456d575ed36ada80b766932b2fcd07ca64"

echo base64_encode(pack('H', $testStr)); // H 十六进制字符串,高位在前

最终输出:

QrRLp5FHVJATdOSIei/VE2f6Y9C30vSP146HBU4JkrPBjKAzbXNdXcn9h4ECPlvmPbESHReZdsApQJc9nlxIKhLC5X943dfsVQVpr7W79vYrBFtTYEI0V9a6C2dfVgP6iqL9JUqZz81kNcKm0goHRW1XXtNq2oC3ZpMrL80HymQ=

Golang 的实现方式

实现一个函数,以每两个字符为单位,将16位的字符串转换成10进制,以下函数:

// 字符串转16进制
func StringTo16BaseByte(s string) []byte {
	var str16 []byte
	for i := 0; i < len(s); i++ {
		if i%2 == 0 {
			n := i + 2
			x, _ := strconv.ParseUint(s[i:n], 16, 32)
			str16 = append(str16, byte(x))
		}
	}
	return str16
}
encoding/binary

验证:

// 测试字符串

testStr := "42b44ba7914754901374e4887a2fd51367fa63d0b7d2f48fd78e87054e0992b3c18ca0336d735d5dc9fd8781023e5be63db1121d179976c02940973d9e5c482a12c2e57f78ddd7ec550569afb5bbf6f62b045b5360423457d6ba0b675f5603fa8aa2fd254a99cfcd6435c2a6d20a07456d575ed36ada80b766932b2fcd07ca64"

fmt.Println(base64.StdEncoding.EncodeToString(StringTo16BaseByte(testStr)))

最终输出:

QrRLp5FHVJATdOSIei/VE2f6Y9C30vSP146HBU4JkrPBjKAzbXNdXcn9h4ECPlvmPbESHReZdsApQJc9nlxIKhLC5X943dfsVQVpr7W79vYrBFtTYEI0V9a6C2dfVgP6iqL9JUqZz81kNcKm0goHRW1XXtNq2oC3ZpMrL80HymQ=

尾巴

如果我写的内容对您有用,谢谢大家了