Golang – slice strings address to bytes


I’m trying to convert this javascript code into Golang with no success.

function hexToBase64(value) {
    const xxx = value.replace(/r|n/g, '').replace(/([da-fA-F]{2}) ?/g, '0x$1 ').replace(/ +$/, '').split(' ');
    const xx1 = String.fromCharCode.apply(null,xxx);
    return btoa(xx1);
}

const item = hexToBase64("8953bed713db2113b8e8215b444b7d3e4bca6c139d3d54bb77f8cf7f07ca5482");
console.log(item);

OUTPUT: iVO+1xPbIRO46CFbREt9PkvKbBOdPVS7d/jPfwfKVII=

My code in golang, but it doesn’t generate the same result

hexToBase64 := func(input string) string {
        x := regexp.MustCompile(`r|n`).ReplaceAllString(string(input), "")
        x1 := regexp.MustCompile(`([da-fA-F]{2}) ?`).ReplaceAllString(x, "0x$1 ")
        x2 := regexp.MustCompile(` +$`).ReplaceAllString(x1, "")
        x3 := strings.Split(x2, " ")
        var value string
        for _, v := range x3 {
            v = strings.Replace(v, "0x", "", -1)
            i, e := strconv.ParseUint(v, 16, 64)
            if e != nil {
                panic(e)
            }
            value = value + string(rune(i))
        }
        return base64.StdEncoding.EncodeToString([]byte(value))
    }

Can you help me?

Source: JavaSript – Stack Overflow

November 13, 2021
Category : News
Tags: base64 | encoding | go | javascript | node.js

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.