Go Hex 处理

// hexTest project main.go
package main

import (
	"encoding/hex"
	"fmt"
)

func main() {
	str := []byte("Hello World")

	dstlen := hex.EncodedLen(len(str))
	dst := make([]byte, dstlen)

	outlen := hex.Encode(dst, str)
	fmt.Println("dstlen = ", dstlen, ", outlen = ", outlen)
	/*H 的 16 进制为  0x48 ->输出到 []byte 里面对于的为 52 和 58
	  4 对应 ASCII 码表中的10进制  52
	*/
	fmt.Println("hex = ", dst)

	hex2strlen := hex.DecodedLen(len(dst))
	str2 := make([]byte, hex2strlen)
	hex.Decode(str2, dst)
	fmt.Println("str2 = ", string(str2), ", dst = ", dst)

	estring := hex.EncodeToString([]byte(str))
	fmt.Println("estring = ", estring)
	fmt.Println("estring = ", []byte(estring))

	ebyte, err := hex.DecodeString(estring)
	if err != nil {
		fmt.Println("hex.DecodeString Error! ", err.Error())
	} else {
		fmt.Println("ebyte = ", string(ebyte))
	}

	fmt.Println(hex.Dump(ebyte))
}

编译输出:

C:/Go/bin/go.exe build -i [D:/golang/src/hexTest]
成功: 进程退出代码 0.
D:/golang/src/hexTest/hexTest.exe  [D:/golang/src/hexTest]
dstlen =  22 , outlen =  22
hex =  [52 56 54 53 54 99 54 99 54 102 50 48 53 55 54 102 55 50 54 99 54 52]
str2 =  Hello World , dst =  [52 56 54 53 54 99 54 99 54 102 50 48 53 55 54 102 55 50 54 99 54 52]
estring =  48656c6c6f20576f726c64
estring =  [52 56 54 53 54 99 54 99 54 102 50 48 53 55 54 102 55 50 54 99 54 52]
ebyte =  Hello World
00000000  48 65 6c 6c 6f 20 57 6f  72 6c 64                 |Hello World|

成功: 进程退出代码 0.

 

转载于:https://my.oschina.net/tsh/blog/1621624

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值