php2go icon indicating copy to clipboard operation
php2go copied to clipboard

Hex2Bin error

Open zhoupufelix opened this issue 4 years ago • 2 comments

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!

zhoupufelix avatar Sep 04 '20 11:09 zhoupufelix

Have the same issue

townsymush avatar Nov 24 '20 12:11 townsymush

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 }

zhoupufelix avatar Nov 25 '20 10:11 zhoupufelix