解码base64并将其编码为十六进制

我有以下代码,该代码解码base64,然后将其编码为十六进制。</ p>

  doc_id:=“ Can35qPeFkm9Xgmp9 + aj3g ==” 
base64_decode,err:= base64.StdEncoding.DecodeString(doc_id)
if err!= nil {
log.Fatal(“ error:”,err)
}

base64_decoded:= fmt.Sprintf(“%q”,base64_decode)
fmt.Printf(“ base_decoded%v
”,base64_decoded)

src:= [] byte(base64_decoded)
fmt.Println (src)
hex_encode:= make([] byte,hex.EncodedLen(len(src)))
hex.Encode(hex_encode,src)
hex_encoded:= fmt.Sprintf(“%s”,hex_encode)
fmt。 Printf(“ hex_encoded%v”,hex_encoded)
</ code> </ pre>

其中doc_id是base64格式。
base64_decoded是其解码值。
我必须将其编码为十六进制,因此 我将其传递给src。</ p>

问题是,当我将base64_decoded标识符传递给src时,当我传递base64_decoded持有的值时,我得到了错误答案。
例如:

如果我将base64_decoded值设为“ \ x11z \ xc0 [d〜\ xfcK \ xb1 \ xf8 \ x11z \ xc0 [d〜“
如果我通过它的值为” \ x11z \ xc0 [d〜\ xfcK \ xb1 \ xf8 \ x11z \ xc0 [d〜“”,我会得到正确的答案117ac05b647efc4bb1f8117ac05b647e </ p>

如果我传递的变量持有“ \ x11z \ xc0 [d〜\ xfcK \ xb1 \ xf8 \ x11z \ xc0 [d〜””,我的答案将是错误的225c7831317a5c7863305b647e5c7866634b5c7862315c7866385c7831317a5c7863305b647e22n

此任务 base64_decoded:= fmt.Sprintf(“%q”,base64_decode)</ strong> </ p>

我在做什么错</ p>
</ div>

展开原文

原文

I have following code which decodes base64 and then encodes it to hex.

doc_id := "Can35qPeFkm9Xgmp9+aj3g=="
base64_decode, err := base64.StdEncoding.DecodeString(doc_id)
if err != nil {
log.Fatal("error:", err)
}
base64_decoded := fmt.Sprintf("%q", base64_decode)
fmt.Printf("base_decoded %v
", base64_decoded)



src := []byte(base64_decoded)
fmt.Println(src)
hex_encode := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(hex_encode, src)
hex_encoded := fmt.Sprintf("%s", hex_encode)
fmt.Printf("hex_encoded %v", hex_encoded)

where doc_id is base64 format. base64_decoded is its decoded value. I have to encode it to hex, so i pass it to src.

The problem is when i pass the identifier base64_decoded to src i get wrong when i pass in the value that base64_decoded is holding i get correct answer. for example: if i get base64_decoded value as "\x11z\xc0[d~\xfcK\xb1\xf8\x11z\xc0[d~" if i pass its value which is "\x11z\xc0[d~\xfcK\xb1\xf8\x11z\xc0[d~", i get correct answer 117ac05b647efc4bb1f8117ac05b647e

if i pass the variable holding "\x11z\xc0[d~\xfcK\xb1\xf8\x11z\xc0[d~" i get wrong answer 225c7831317a5c7863305b647e5c7866634b5c7862315c7866385c7831317a5c7863305b647e22dn

Has it got something with this assignment base64_decoded := fmt.Sprintf("%q", base64_decode)

what am i doing wrong

2个回答



使用以下代码:</ p>

  doc_id:=“ Can35qPeFkm9Xgmp9 + aj3g ==  “ 
base64_decode,错误:= base64.StdEncoding.DecodeString(doc_id)
if错误!= nil {
log.Fatal(”错误:“,错误)
}
fmt.Println(hex.EncodeToString(base64_decode))

</ code> </ pre>

直接将 base64_decode </ code>中的字节编码为十六进制。</ p>

的调用 fmt.Sprintf(“%q”,base64_decode)</ code>返回一个字符串,其中包含按照Go的字符串文字规则转义的字节。 通常情况下,返回的字符串中的字节与 base64_decode </ code>中的字节不相等。 例如,输入字节0x11被转义为四个字节 \ x11 </ code>。</ p>
</ div>

展开原文

原文

Use the following code:

doc_id := "Can35qPeFkm9Xgmp9+aj3g=="
base64_decode, err := base64.StdEncoding.DecodeString(doc_id)
if err != nil {
    log.Fatal("error:", err)
}
fmt.Println(hex.EncodeToString(base64_decode))

Directly encode the bytes in base64_decode as hex.

The call to fmt.Sprintf("%q", base64_decode) returns a string with the bytes escaped per Go's string literal rules. In the general case, the bytes in the returned string are not equal to the bytes in base64_decode. For example, the input byte 0x11 is escaped to the four bytes \x11.



发生的情况是fmt.Sprintf(“%q”,base64_decode)对base64_decode的转换,在此过程中将一些转换 内存中的字节丢失了。</ p>

在这里为您提供示例: https://play.golang.org/p/pdQBp7NsvQM </ p>

  doc_id:=” Can35qPeFkm9Xgmp9 + aj3g ==“ 
var_encode:= b64.StdEncoding .EncodeToString([] byte(doc_id))
fmt.Println(var_encode)
var_decode,_:= b64.StdEncoding.DecodeString(var_encode)
fmt.Println(string(var_decode))
</ code> </ pre >
</ div>

展开原文

原文

what happens is that the conversion that fmt.Sprintf("%q", base64_decode) makes to base64_decode, in this process to convert some bytes in the memory are lost.

here you below I leave an example: https://play.golang.org/p/pdQBp7NsvQM

doc_id := "Can35qPeFkm9Xgmp9+aj3g=="
var_encode := b64.StdEncoding.EncodeToString([]byte(doc_id))
fmt.Println(var_encode)
var_decode , _ := b64.StdEncoding.DecodeString(var_encode)
fmt.Println(string(var_decode))

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐