php2go
php2go copied to clipboard
Hex2Bin error
package main
import ( "fmt" "strconv" )
// Hex2bin hex2bin() func Hex2bin(data string) (string, error) { i, err := strconv.ParseInt(data, 16, 0) if err != nil { return "", err } return strconv.FormatInt(i, 2), nil }
func main(){ fmt.Println(Hex2bin("48656c6c6f20576f726c6421")) }
When I used this method, it returned an error below. //strconv.ParseInt: parsing "48656c6c6f20576f726c6421": value out of range
If I use php function hex2bin()
:It shows us ,
Hello World!
Have the same issue
Have the same issue
I create a new function ,just use this function,It will help u
func Hex2binStr(h string)(str string,err error){ l := len(h) length,k := l,0 bHex := make([]byte,l/2) for i:=0;i<length;i+=2 { if l == 1 { break } //every two byte s := string(h[i:i+2]) //hex bt,err:= strconv.ParseInt(s,16,32) if err != nil { return "",err } bHex[k] = byte(bt) k++ l-=2 } return string(bHex),nil }