V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
haython
V2EX  ›  Go 编程语言

在 go 里如何将数字转换为 ascii 码?

  •  
  •   haython · 2016-10-27 10:20:12 +08:00 · 1894 次点击
    这是一个创建于 2731 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在将一个加密的算法从 php 翻译到 go ,最后发现数字转换为 ascii 的时候,两个语言不一致, php 用的 chr(int), go 是直接用的 string(int),求教正确的姿势
    第 1 条附言  ·  2016-10-31 15:23:04 +08:00
    都不对。。。。最后用 lua 改写了
    9 条回复    2016-10-28 08:52:40 +08:00
    horsley
        1
    horsley  
       2016-10-27 10:24:25 +08:00   ❤️ 1
    姿势应该是正确的,倒是可以关注下这个 int 是不是超过 128
    haython
        2
    haython  
    OP
       2016-10-27 10:28:45 +08:00
    @horsley 就是超过 128 的就不正确了
    hst001
        3
    hst001  
       2016-10-27 10:35:42 +08:00   ❤️ 1
    []byte(strconv.Itoa(int))
    reus
        4
    reus  
       2016-10-27 11:29:02 +08:00   ❤️ 1
    string([]rune{rune(i % 256)})
    zeayes
        5
    zeayes  
       2016-10-27 11:29:21 +08:00   ❤️ 1
    char --> int rune('a')
    int --> char string([]byte{97})
    zwh8800
        6
    zwh8800  
       2016-10-27 15:11:22 +08:00   ❤️ 1
    golang 的 string 使用的是 utf8 编码
    reus
        7
    reus  
       2016-10-27 15:18:56 +08:00   ❤️ 1
    @zeayes 没有 char 这个类型,'a'和'你'的类型都是 int32 ,和 rune 一样。
    string 也不是 char ,这里根本就不应该出现[]byte 。 rune 到 string 只需要 string([]rune{97, 30000})这样。
    reus
        8
    reus  
       2016-10-27 15:20:18 +08:00   ❤️ 1
    @zwh8800 看文档, PHP 的 chr 是要先取模的
    zeayes
        9
    zeayes  
       2016-10-28 08:52:40 +08:00   ❤️ 1
    @reus 恩,明白。 golang 里的确没有 char 这个类型,楼主的意思应该字符和 int 的转换, python 里面对应的是 chr 和 ord , golang 里面就是 rune 和 string 。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5237 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:42 · PVG 15:42 · LAX 00:42 · JFK 03:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.