ROT13 using bytes rather than strings

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

ROT13 using bytes rather than strings

问题

我正在尝试为我的工具添加一个rot13翻译功能。

客户端基本代码:

....
buf := make([]byte, 1024)

for {
    n, err := in.Read(buf)
    .....

    for _, chunk := range split(r, buf[:n], sizes) {
        if _, err := out.Write(chunk); err != nil {
        ......

由于我希望它能处理字节而不是字符串,我想出了以下解决方案:

https://play.golang.org/p/m3NvoZIyS8g

这个示例可以工作,但有一些我不理解的额外字节,它们被下划线标记为***:

bytes after  ***{***[89 111 117 32 99 114 97 99 107 101 100 32 116 104 101 32 99 111 
100 101 
32 32 32 32 32 32 32 239 191 189 85 239 191 189 239 191 189 239 191 189 74 40 239 191 
189 47 239 191 189 86 239 191 189 45 239 191 189 33] ***0 0}***

string after ***{***You cracked the code       �U���J(�/�V�-�! ***%!s(int=0)*** 
***%!s(bytes.readOp=0)}***

请忽略包含"�U���J(�/�V�-�!"的部分,我只是需要一些关于这个"字符串"的确认。

我该如何去掉这些额外的"东西"?有没有更好的方法来实现我的想法?

英文:

I'm trying to add a rot13 translation to my tool.
Client basic code:

....
buf := make([]byte, 1024)

for {
    n, err := in.Read(buf)
    .....

    for _, chunk := range split(r, buf[:n], sizes) {
        if _, err := out.Write(chunk); err != nil {
        ......

Since I want it to work with bytes rather than strings, I came up with this:

https://play.golang.org/p/m3NvoZIyS8g

This example works, expect for some extra bytes which I don't understand, they are underlined with ***:

bytes after  ***{***[89 111 117 32 99 114 97 99 107 101 100 32 116 104 101 32 99 111 
100 101 
32 32 32 32 32 32 32 239 191 189 85 239 191 189 239 191 189 239 191 189 74 40 239 191 
189 47 239 191 189 86 239 191 189 45 239 191 189 33] ***0 0}***

string after ***{***You cracked the code       �U���J(�/�V�-�! ***%!s(int=0)*** 
***%!s(bytes.readOp=0)}***

Please ignore the part with "�U���J(�/�V�-�!", I just needed some confirmation with this "string"

How do I get rid of this extra "stuff"?
Is there any way to do my idea in a better way?

答案1

得分: 2

这是fub的后备数组中剩余的可用字节。

由于你以原始方式打印它,它显示了它的全部内部内容。

如果你只想打印已写入的部分,可以使用fmt.Printf("bytes after %v\n", fub.Bytes())

对于字符串表示,同样适用,使用fmt.Printf("string after %s\n\n", fub.String())

英文:

Those are remaining available bytes into the backing array of fub.

And because you print it raw, it shows all its internal.

You want to write : fmt.Printf("bytes after %v\n", fub.Bytes()) to get only the portion written onto it.

The same aplpies for the string representation, use fmt.Printf("string after %s\n\n", fub.String())

答案2

得分: 1

你所看到的是fmt.Printf通过反射尽力将这个结构体类型的值渲染为字符串。

你需要的是:

var fub bytes.Buffer

// fmt.Printf("string after  %s\n\n", fub)

fmt.Printf("string after  %s\n\n", &fub) // 修正
fmt.Printf("string after  %s\n\n", fub.String()) // 或者使用这种方式

为什么这样可以工作?查看bytes.Buffer文档,你会发现String() string方法需要一个指针接收器,所以如果你想在像fmt.Printf这样基于反射的函数中使用它,你必须传入一个*bytes.Buffer类型的值。

英文:

What you are seeing is fmt.Printf's best attempt via reflection to render this struct type's value as a string.

You want:

var fub bytes.Buffer

// fmt.Printf("string after  %s\n\n", fub)

fmt.Printf("string after  %s\n\n", &fub) // fix
fmt.Printf("string after  %s\n\n", fub.String()) // or this

Why does this work? Checking the bytes.Bufferdocs you can see the String() string method expects a pointer receiver, so if you want to avail of this with a reflection-based function like fmt.Printf, you must pass in a *bytes.Buffer typed value.

huangapple
  • 本文由 发表于 2021年7月28日 18:50:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/68559024.html
匿名

发表评论

匿名网友

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

确定