duanfu4446 2016-12-01 08:11
浏览 50
已采纳

在Golang中解码带引号的可打印电子邮件

When you type a two spaces in a row in an html email in Gmail it encodes it into the quoted-printable body as "=C2=A0 " if you look at the source of the email.

According to this stackoverflow answer, because of the UTF-8 encoding this should be converted to 00A0 (nbsp) when decoded: https://stackoverflow.com/a/2774507

However, in Golang, this isn't how it works:

s := `Text Text Text.=C2=A0 That's just two spaces`

r := strings.NewReader(s)

qpReader := quotedprintable.NewReader(r)

all, _ := ioutil.ReadAll(qpReader)

str := string(all)

fmt.Println(strings.Index(str, "\xC2\xA0"))

This outputs "15", here's the Playground link: https://play.golang.org/p/8n6L7dlZPt

Instead of it using an NBSP there, it will keep the \xC2 and result in "Text Text Text That's just two spaces".

What's the best way to correctly render this as \x00A0?

  • 写回答

1条回答 默认 最新

  • dqve65954 2016-12-01 10:47
    关注

    As Volker explained in his comment, a Go string is simply a slice of bytes. In your case, it's already encoded as UTF-8 which is Go's default encoding. To access the actual Unicode code points (runes in Go lingo), use something like:

    // Prints 15.
    fmt.Println(strings.IndexRune(str, '\xA0'))
    
    // Prints A0.
    fmt.Printf("%X
    ", []rune(str)[15]);
    

    How to correctly render the string depends on where you want to render it. But in most cases, you can pass it as is since it's already in UTF-8.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名