# 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 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`.

https://golang.org/ref/spec#Numeric_types

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 byte = 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 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]))
}
fmt.Printf("\n");

// 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: https://play.golang.org/p/DnBhrAVF5Fx

1 Like

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