HEX 0x string to []byte to string DecodeString
https://play.golang.org/p/i90_qsN2Sz-
package main
import (
"fmt"
"encoding/hex"
)
func main() {
id := "0x1dd84569ad60faa1b6838526ba7156388cf7c8d376ed0ccda9bce0419c2c3519"
fmt.Printf("Ori ID: %v \n\n", id)
fmt.Printf("Ori ID[2:]: %v \n\n", id[2:])
byteid := []byte(id)
fmt.Printf("===== Byte id ===== Decimal \n")
fmt.Printf("Byte ID: %v \n", byteid)
fmt.Printf("Byte ID 0x%x \n\n", byteid)
fmt.Printf("===== Decode(Byte id[2:]) ===== Decimal HEX \n")
byteid = []byte(id[2:])
fmt.Printf("Byte ID: %v \n", byteid)
fmt.Printf("Byte ID 0x%x \n\n", byteid)
n, _ := hex.Decode(byteid, byteid)
fmt.Printf("Byte ID[2:]: %v \n", byteid)
fmt.Printf("Byte ID[2:] 0x%x \n", byteid)
fmt.Printf("Byte ID[2:] [:n]: %v \n", byteid[:n])
fmt.Printf("Byte ID[2:] [:n] 0x%x \n\n", byteid[:n])
fmt.Printf("===== id ===== Decimal \n")
idbyte32 := covertStringTByte32(id)
fmt.Printf("Byte32 ID: %v \n", idbyte32 )
//fmt.Printf("String ID: %s \n", idbyte32 )
fmt.Printf("HEX ID: 0x%x \n\n", idbyte32 )
fmt.Printf("===== id[2:] ===== Decimal \n")
idbyte32 = covertStringT2Byte32(id)
fmt.Printf("Byte32 ID: %v \n", idbyte32 )
//fmt.Printf("String ID: %s \n", idbyte32 )
fmt.Printf("HEX ID: 0x%x \n\n", idbyte32 )
fmt.Printf("===== DecodeString(id[2:]) ===== HEX \n")
idbyte32 = covertStringDecodeStringByte32(id)
fmt.Printf("Byte32 ID: %v \n", idbyte32 )
//fmt.Printf("String ID: %s \n", idbyte32 )
fmt.Printf("HEX ID: 0x%x \n", idbyte32 )
}
func covertStringTByte32(t string) [32]byte {
var b32 [32]byte
copy(b32[:], t)
return b32
}
func covertStringT2Byte32(t string) [32]byte {
var b32 [32]byte
copy(b32[:], t[2:]) //remove 0x
return b32
}
func covertStringDecodeStringByte32(t string) [32]byte {
data, err := hex.DecodeString(t[2:])
if err != nil {
fmt.Printf("ERR \n")
}
fmt.Printf("DecodeString data: %v \n", data)
fmt.Printf("DecodeString data length: %v \n\n", len(data))
var b32 [32]byte
copy(b32[:], data)
return b32
}
Ori ID: 0x1dd84569ad60faa1b6838526ba7156388cf7c8d376ed0ccda9bce0419c2c3519
Ori ID[2:]: 1dd84569ad60faa1b6838526ba7156388cf7c8d376ed0ccda9bce0419c2c3519
===== Byte id ===== Decimal
Byte ID: [48 120 49 100 100 56 52 53 54 57 97 100 54 48 102 97 97 49 98 54 56 51 56 53 50 54 98 97 55 49 53 54 51 56 56 99 102 55 99 56 100 51 55 54 101 100 48 99 99 100 97 57 98 99 101 48 52 49 57 99 50 99 51 53 49 57]
Byte ID 0x307831646438343536396164363066616131623638333835323662613731353633383863663763386433373665643063636461396263653034313963326333353139
===== Decode(Byte id[2:]) ===== Decimal HEX
Byte ID: [49 100 100 56 52 53 54 57 97 100 54 48 102 97 97 49 98 54 56 51 56 53 50 54 98 97 55 49 53 54 51 56 56 99 102 55 99 56 100 51 55 54 101 100 48 99 99 100 97 57 98 99 101 48 52 49 57 99 50 99 51 53 49 57]
Byte ID 0x31646438343536396164363066616131623638333835323662613731353633383863663763386433373665643063636461396263653034313963326333353139
Byte ID[2:]: [29 216 69 105 173 96 250 161 182 131 133 38 186 113 86 56 140 247 200 211 118 237 12 205 169 188 224 65 156 44 53 25 56 99 102 55 99 56 100 51 55 54 101 100 48 99 99 100 97 57 98 99 101 48 52 49 57 99 50 99 51 53 49 57]
Byte ID[2:] 0x1dd84569ad60faa1b6838526ba7156388cf7c8d376ed0ccda9bce0419c2c35193863663763386433373665643063636461396263653034313963326333353139
Byte ID[2:] [:n]: [29 216 69 105 173 96 250 161 182 131 133 38 186 113 86 56 140 247 200 211 118 237 12 205 169 188 224 65 156 44 53 25]
Byte ID[2:] [:n] 0x1dd84569ad60faa1b6838526ba7156388cf7c8d376ed0ccda9bce0419c2c3519
===== id ===== Decimal
Byte32 ID: [48 120 49 100 100 56 52 53 54 57 97 100 54 48 102 97 97 49 98 54 56 51 56 53 50 54 98 97 55 49 53 54]
HEX ID: 0x3078316464383435363961643630666161316236383338353236626137313536
===== id[2:] ===== Decimal
Byte32 ID: [49 100 100 56 52 53 54 57 97 100 54 48 102 97 97 49 98 54 56 51 56 53 50 54 98 97 55 49 53 54 51 56]
HEX ID: 0x3164643834353639616436306661613162363833383532366261373135363338
===== DecodeString(id[2:]) ===== HEX
DecodeString data: [29 216 69 105 173 96 250 161 182 131 133 38 186 113 86 56 140 247 200 211 118 237 12 205 169 188 224 65 156 44 53 25]
DecodeString data length: 32
Byte32 ID: [29 216 69 105 173 96 250 161 182 131 133 38 186 113 86 56 140 247 200 211 118 237 12 205 169 188 224 65 156 44 53 25]
HEX ID: 0x1dd84569ad60faa1b6838526ba7156388cf7c8d376ed0ccda9bce0419c2c3519
Program exited.