encoding/hex: 无效字节:U+0068 ‘h’ Golang

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

encoding/hex: invalid byte: U+0068 'h' Golang

问题

我正在尝试将一个字符串转换为包含其十六进制值的字节数组,以下是我编写的代码:

package main

import (
    "encoding/hex"
    "fmt"
    "os"
)

func main() {
    str := "abcdefhijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789"

    b, err := hex.DecodeString(str)

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    fmt.Printf("Decoded bytes %v \n", b)
}

这是Go PlayGround的链接:http://play.golang.org/p/8PMEFTCYSd

但是它给我返回了错误信息encoding/hex: invalid byte: U+0068 'h' Golang。这里有什么问题?我想将我的字符串转换为包含每个字符的十六进制值的字节数组。我希望b[n]包含str[n]的十六进制值。

英文:

I'm trying to convert a string into a byte array containing its hexadecimal values, here is the code I've written:

package main

import (
     "encoding/hex"
     "fmt"
     "os"
)

func main() {
     str :="abcdefhijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789"

     b, err := hex.DecodeString(str)

     if err != nil {
             fmt.Println(err)
             os.Exit(1)
     }

     fmt.Printf("Decoded bytes %v \n ", b)
}

Here is the link from Go PlayGround: http://play.golang.org/p/8PMEFTCYSd

But it's giving me the error ***encoding/hex: invalid byte: U+0068 'h' Golang ***. What's the problem here? I want to convert my string into byte array containing hexadecimal values of each character in the string. I want b[n] to contain hexadecimal value of str[n].

答案1

得分: 4

你可以通过简单的类型转换将一个字符串转换为字节切片(不完全等同于数组):

b := []byte(str)

这样就完成了!

如果你想将其作为十六进制字符串打印出来,可以使用fmt.Printf()函数同时使用字符串和字节切片:

fmt.Printf("%x", str)
// 或者:
fmt.Printf("%x", b)

提示:你可以使用格式字符串"% x"在每个字节/字符的十六进制形式之间打印一个空格:

fmt.Printf("% x", str)

如果你想将十六进制形式的结果作为字符串,可以使用fmt.Sprintf()变体:

hexst := fmt.Sprintf("%x", str)
// 或者:
hexst := fmt.Sprintf("%x", b)

或者作为替代,你可以使用encoding/hex包中的hex.EncodeToString()函数:

hexst := hex.EncodeToString(b)
英文:

> I want to convert my string into byte array containing hexadecimal values of each character in the string.

You can simply convert a string to a []byte (byte slice, not exactly the same as array!) by using a simple type conversion:

b := []byte(str)

And you're done!

If you want to print it as a hexadecimal string, you can do that with the fmt.Printf() function using both the string and the []byte:

fmt.Printf("%x", str)
// Or:
fmt.Printf("%x", b)

Tip: you can use the format string "% x" to have a space printed between each of the hexadecimal forms of the bytes/characters:

fmt.Printf("% x", str)

If you want the result of the hexadecimal form as a string, you can use the fmt.Sprintf() variant:

hexst := fmt.Sprintf("%x", str)
// Or:
hexst := fmt.Sprintf("%x", b)

Or as an alternative you can use the hex.EncodeToString() function from the encoding/hex package:

hexst := hex.EncodeToString(b)

答案2

得分: 2

你的代码正在尝试将字符串从十六进制解码。但是该字符串并不是十六进制编码的。

如果要将字符串编码为十六进制,请尝试以下代码:

b := fmt.Sprintf("%x", str)

fmt.Printf("编码后的字节:%v", []byte(b))
英文:

Your code is trying to decode from hex. That string is not hex encoded.

To encode to hex try this

b := fmt.Sprintf("%x", str)

fmt.Printf("Decoded bytes %v", []byte(b))

huangapple
  • 本文由 发表于 2015年3月31日 11:41:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/29360197.html
匿名

发表评论

匿名网友

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

确定