How to compare hexadecimal and string

i have a hex representation and string version of this hex and I wanna compare them, if i will convert them both to byte and compare their bytes would it be correct???

and one more how to convert into fixed sized byte array for example [32]byte

actually i have a []uint8 representation of the hash and string of this hash but unfortunately if i convert this string into byte and compare this bytes with []uint8 they are different absolutekly different but they both same hash

First, byte is simply an alias for uint8.

Just use one or the other.

Iā€™m not sure I understand your question, but the following example may have what you need:

package main

import "fmt"

// A hexidecimal value ...
// ... as a simple integer
var x_int int =   0x1fe7d6df
// ... as a string
var x_string string = "1fe7d6df"
// ... as an array of bytes
var x_array [32]byte = [32]byte { '1', 'f', 'e', '7', 'd', '6', 'd', 'f', }

// the string version will be converted to an array of bytes and stored here:
var b_array [32]byte

func main() {
        var i int

        // print the integer and string versions

        fmt.Printf("integer: %x\n", x_int)
        fmt.Printf("string:  %s\n", x_string)

        // print the array version. Each byte is converted to a string

        fmt.Printf("array:   ")
        for i = 0; x_array[i] != 0; i++ {
                fmt.Printf("%s", string(x_array[i]))

        // compare the string version to the array version

        for i = 0; x_array[i] != 0; i++ {
                if x_string[i] != x_array[i] {
                        fmt.Printf("\nThe string and array differ at position %d\n",i+1)

        if x_array[i] == 0 { fmt.Printf("\nThe string and array are the same\n") }

        // convert the string version into an array of bytes

        for i = 0; i < len(x_string); i++ {
                b_array[i] = x_string[i]

        // convert it back into another string and print the string

        s := string(b_array[:len(x_string)])
        fmt.Printf("\nstring:  %s\n",s)

        // compare the two arrays

        for i = 0; x_array[i] != 0; i++ {
                if b_array[i] != x_array[i] {
                        fmt.Printf("\nThe arrays differ at position %d\n",i+1)

        if x_array[i] == 0 { fmt.Printf("\nThe arrays are the same\n") }

The above code is at the Go Playground:

1 Like

I think he wants to compare []byte{0xFF} against "FF".

If this is true hex.DecodeString should be able to help you:

1 Like

That looks reasonable. Akezhan, if neither of these answers work for you, please show us examples of the data types you are working with (how they are declared, and how you use them), and if you can, explain more clearly what you are trying to do.

@Akezhan_Esbolatov Why are you flagging your own post as off-topic?

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