字符串文字转义十六进制值

I am attempting to escape a specific hex value in a golang string. The function call looks something like this:

Insert(0, "\x00\x00\x00IHDR\x00\x00\x000\x00\x00\x000\b\x03")
Insert(25, "\x00\x00\x00\x06PLTE")
Insert(43, "\x00\x00\x00\x02tRNS")
Insert(57, "\x00\x00\t;IDATx\xDA\x010\t\xCF\xF6") // problem line
Insert(2432, "\x00\x00\x00\x00IEND")

The problem arises when the language interprets the "\xDA" hex escape. Instead of correctly escaping to a Ú value, it is escaped to � (the Replacement Character).

I ensured this is what was occuring in the following playground example:

fmt.Println("\xDA")
i := 218
h := fmt.Sprintf("%x", i)
fmt.Printf("Hex conf of '%d' is '%s'
", i, h)  
fmt.Println(string(i))

This snippet, when run, prints

�
Hex conf of '218' is 'da'
Ú

Am I missing something here? The fact that "\xDA" is being escaped to a value of 65533 is throwing off my entire program, which relies on the CRC32 and some other checksums. This does not occur in the javascript version of this program (which itself is a translation from James compface program, written in C).

Here is the playground link: https://play.golang.org/p/c-XMK68maX

查看全部
douyue3800
douyue3800
2017/04/18 00:27
  • escaping
  • string
  • hex
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复