dongwuli5105 2019-09-13 22:06
浏览 17
已采纳

rot13密码有什么问题?

I'm having some trouble with tour.golang.org/methods/23

package main

import (
    "io"
    "os"
    "strings"
)

type rot13Reader struct {
    r io.Reader
}

func (r rot13Reader) Read(s []byte) (int, error) {
    a, e := r.r.Read(s)
    for i := 0; i < a; i++ {
        if(s[i] >= 'a') {
            s[i] = ((s[i] - 'a') + 13) % 26 + 'a'   
        }
        if(s[i] >= 'A') {
            s[i] = ((s[i] - 'A') + 13) % 26 + 'A'   
        }
    }
    return a, e
}

func main() {
    s := strings.NewReader("Lbh penpxrq gur pbqr!")
    r := rot13Reader{s}
    io.Copy(os.Stdout, &r)
}

I'm adding then modding but the results don't quite look right after accounting for the gap between the ascii codes for lower and upper case

YHN VKTVDXW MAX VHWX!

  • 写回答

3条回答 默认 最新

  • drqja5919276 2019-09-13 22:31
    关注

    'A' is (numerically speaking) 65. Letters deeper in the alphabet increase, up to 'Z' which is 90.

    'a' is (numerically speaking) 97. Letters deeper in the alphabet increase, up to 'z' which is 122.

    (Don't ask why I have some ASCII memorized. I did have to look up the z values. :-) )

    Exercise 1: if s[i] is 'a', i.e., 97, and you rot-13 it to 'n' (110), is this greater than 65? What happens when you get to your second if statement?

    Exercise 2: if s[i] is '_' (decimal 95), what happens here?

    (You keep changing your question, so by now these exercises are somewhat moot.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度