Go – 如何将二进制字符串转换为二进制字节?

huangapple go评论96阅读模式
英文:

Go - How to convert binary string as text to binary bytes?

问题

我有一个包含像这样的字符串的__text__转储文件:

x\x9cK\xb42\xb5\xaa.\xb6\xb2\xb0R\xcaK-\x09J\xccKOU

我需要将它们转换为[]byte

有人可以建议如何在Go中完成这个任务吗?
python中的等效方法是decode('string_escape')

英文:

I have a text dump file with strings like this one:

x\x9cK\xb42\xb5\xaa.\xb6\xb2\xb0R\xcaK-\x09J\xccKOU

I need to convert them to []byte.

Can someone please suggest how this can be done in Go?
The python equivalent is decode('string_escape').

答案1

得分: 3

这是一种方法。请注意,这不是对Python string_escape格式的完全解码,但可能足够给出你提供的示例。

playground链接

package main

import (
	"fmt"
	"log"
	"regexp"
	"strconv"
)

func main() {
	b := []byte(`x\x9cK\xb42\xb5\xaa.\xb6\xb2\xb0R\xcaK-\x09J\xccKOU`)
	re := regexp.MustCompile(`\\x([0-9a-fA-F]{2})`)
	r := re.ReplaceAllFunc(b, func(in []byte) []byte {
		i, err := strconv.ParseInt(string(in[2:]), 16, 64)
		if err != nil {
			log.Fatalf("Failed to convert hex: %s", err)
		}
		return []byte{byte(i)}
	})
	fmt.Println(r)
	fmt.Println(string(r))
}

我曾经考虑过使用json解码器的想法,但不幸的是它不理解\xYY语法。

英文:

Here is one way of doing it. Note this isn't a complete decode of the python string_escape format, but may be sufficient given the example you've given.

playground link

package main

import (
	"fmt"
	"log"
	"regexp"
	"strconv"
)

func main() {
	b := []byte(`x\x9cK\xb42\xb5\xaa.\xb6\xb2\xb0R\xcaK-\x09J\xccKOU`)
	re := regexp.MustCompile(`\\x([0-9a-fA-F]{2})`)
	r := re.ReplaceAllFunc(b, func(in []byte) []byte {
		i, err := strconv.ParseInt(string(in[2:]), 16, 64)
		if err != nil {
			log.Fatalf("Failed to convert hex: %s", err)
		}
		return []byte{byte(i)}
	})
	fmt.Println(r)
	fmt.Println(string(r))
}

I did have the idea of using the json decoder, but unfortunately it doesn't understand the \xYY syntax.

答案2

得分: 3

这是你可能会用来编写一个小解析器的方法(如果你将来需要支持其他转义字符):

import (
	"fmt"
	"encoding/hex"
)

func decode(bs string) ([]byte,error) {
	in := []byte(bs)
	res := make([]byte,0)
	esc := false
	for i := 0; i<len(in); i++ {
		switch {
		case in[i] == '\\':
			esc = true
			continue
		case esc:
			switch {
			case in[i] == 'x':
				b,err := hex.DecodeString(string(in[i+1:i+3]))
				if err != nil {
					return nil,err
				}
				res = append(res, b...)
			    i = i+2
            default:
    			res = append(res, in[i])
			}
			esc = false
		default:
			res = append(res, in[i])
		}
	}
	return res,nil
	
}

playground

英文:

Here's how you might approach write a little parser (if you needed to support other esc things in the future):

import (
	&quot;fmt&quot;
	&quot;encoding/hex&quot;
)

func decode(bs string) ([]byte,error) {
	in := []byte(bs)
	res := make([]byte,0)
	esc := false
	for i := 0; i&lt;len(in); i++ {
		switch {
		case in[i] == &#39;\\&#39;:
			esc = true
			continue
		case esc:
			switch {
			case in[i] == &#39;x&#39;:
				b,err := hex.DecodeString(string(in[i+1:i+3]))
				if err != nil {
					return nil,err
				}
				res = append(res, b...)
			    i = i+2
            default:
    			res = append(res, in[i])
			}
			esc = false
		default:
			res = append(res, in[i])
		}
	}
	return res,nil
	
}

playground

huangapple
  • 本文由 发表于 2013年4月9日 01:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/15885542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定