Convert unicode char to UTF-8

In this code https://play.golang.org/p/UGxgwfA3uJf

package main

import (
	"encoding/hex"
	"fmt"
)

func main() {
	decodeHex("53D6D7")
	decodeHex("6CV4GC")
}

func decodeHex(s string) {
	b, err := hex.DecodeString(s)
	if err != nil {
		fmt.Printf("error: %v\n", err)
		return
	}
	fmt.Println(b)
}

//Output
[83 214 215]
error: encoding/hex: invalid byte: U+0056 'V'

Is there a shortcut to convert the unicode char U+0056 ā€˜Vā€™ to UTF-8 without going through the charset.DetermineEncoding?

I think, you misuse hex.DecodeString

DecodeString expects that src contain only hexadecimal characters and that src should have an even length. If the input is malformed, DecodeString returns a string containing the bytes decoded before the error.

In your example, how is V a hex char?

1 Like

You right! It is not, I just had some bad hashes.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.