在GO中得到错误的Base64编码结果

huangapple go评论81阅读模式
英文:

Getting wrong Base64 encoded result in GO

问题

我有以下十六进制数据,这是将5个值(包括名称、数字和日期字段)转换为TLV格式后得到的:

0115426f627320426173656d656e74205265636f726473020f3130303032353930363730303030330314323032322d30342d32355431353a33303a30305a040a323130303130302e393905093331353031352e3135

这个十六进制数据需要进一步编码为Base64格式。我写了下面的代码来实现:

func TLVsToBase64(v string) string { // v - 十六进制格式的TLV数据

encodedTLV := b64.StdEncoding.EncodeToString([]byte(v))
return encodedTLV

}

上述十六进制数据的输出结果(错误的)如下:

MDExNTQyNmY2MjczMjA0MjYxNzM2NTZkNjU2ZTc0MjA1MjY1NjM2ZjcyNjQ3MzAyMGYzMTMwMzAzMDMyMzUzOTMwMzYzNzMwMzAzMDMwMzMwMzE0MzIzMDMyMzIyZDMwMzQyZDMyMzU1NDMxMzUzYTMzMzAzYTMwMzA1YTA0MGEzMjMxMzAzMDMxMzAzMDJlMzkzOTA1MDkzMzMxMzUzMDMxMzUyZTMxMzU=

期望的输出结果是:

ARVCb2JzIEJhc2VtZW50IFJlY29yZHMCDzEwMDAyNTkwNjcwMDAwMwMUMjAyMi0wNC0yNVQxNTozMDowMFoECjIxMDAxMDAuOTkFCTMxNTAxNS4xNQ==

我是Go语言的新手,所以请帮我解决这个问题。我可能漏掉了什么。

英文:

I have the following hex data created by converting 5 values(consists of name, numeric and date field) to TLV

0115426f627320426173656d656e74205265636f726473020f3130303032353930363730303030330314323032322d30342d32355431353a33303a30305a040a323130303130302e393905093331353031352e3135

This hex data needs to be further encoded to Base64. I wrote the below code for that

func TLVsToBase64(v string) string { // v - the TLV in hex format
	
	encodedTLV := b64.StdEncoding.EncodeToString([]byte(v))
	return encodedTLV

}

The output(which is wrong) of the aforementioned hex data is below:

MDExNTQyNmY2MjczMjA0MjYxNzM2NTZkNjU2ZTc0MjA1MjY1NjM2ZjcyNjQ3MzAyMGYzMTMwMzAzMDMyMzUzOTMwMzYzNzMwMzAzMDMwMzMwMzE0MzIzMDMyMzIyZDMwMzQyZDMyMzU1NDMxMzUzYTMzMzAzYTMwMzA1YTA0MGEzMjMxMzAzMDMxMzAzMDJlMzkzOTA1MDkzMzMxMzUzMDMxMzUyZTMxMzU=

The desired output is:

ARVCb2JzIEJhc2VtZW50IFJlY29yZHMCDzEwMDAyNTkwNjcwMDAwMwMUMjAyMi0wNC0yNVQxNTozMDowMFoECjIxMDAxMDAuOTkFCTMxNTAxNS4xNQ==

I am new to Go, so please help me to troubleshoot the issue. I might missed something

答案1

得分: 2

你的输入是一些数据的十六进制表示。你期望的输出不是该十六进制表示的UTF-8数据的Base64编码,而是该十六进制编码所代表的数据(字节),因此首先需要解码字节,例如使用hex.DecodeString()函数:

func TLVsToBase64(v string) (string, error) { // v - 十六进制格式的TLV
    data, err := hex.DecodeString(v)
    if err != nil {
        return "", err
    }

    encodedTLV := base64.StdEncoding.EncodeToString(data)
    return encodedTLV, nil
}

进行测试:

s := "0115426f627320426173656d656e74205265636f726473020f3130303032353930363730303030330314323032322d30342d32355431353a33303a30305a040a323130303130302e393905093331353031352e3135"
fmt.Println(TLVsToBase64(s))

输出结果与你期望的一致(可以在Go Playground上尝试):

ARVCb2JzIEJhc2VtZW50IFJlY29yZHMCDzEwMDAyNTkwNjcwMDAwMwMUMjAyMi0wNC0yNVQxNTozMDowMFoECjIxMDAxMDAuOTkFCTMxNTAxNS4xNQ== <nil>
英文:

Your input is the hexadecimal representation of some data. And your expected output is not the Base64 encoding of the UTF-8 data of the hex representation, but rather the data (the bytes) the hex encoding represent, so first decode the bytes e.g. using hex.DecodeString():

func TLVsToBase64(v string) (string, error) { // v - the TLV in hex format
	data, err := hex.DecodeString(v)
	if err != nil {
		return &quot;&quot;, err
	}

	encodedTLV := base64.StdEncoding.EncodeToString(data)
	return encodedTLV, nil
}

Testing it:

s := &quot;0115426f627320426173656d656e74205265636f726473020f3130303032353930363730303030330314323032322d30342d32355431353a33303a30305a040a323130303130302e393905093331353031352e3135&quot;
fmt.Println(TLVsToBase64(s))

Output is what you expect (try it on the Go Playground):

ARVCb2JzIEJhc2VtZW50IFJlY29yZHMCDzEwMDAyNTkwNjcwMDAwMwMUMjAyMi0wNC0yNVQxNTozMDowMFoECjIxMDAxMDAuOTkFCTMxNTAxNS4xNQ== &lt;nil&gt;

huangapple
  • 本文由 发表于 2022年3月1日 15:44:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/71305183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定