The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
displayabc

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

  •  
  •   displayabc · Oct 27, 2016 · 2593 views
    This topic created in 3513 days ago, the information mentioned may be changed or developed.
    在将一个加密的算法从 php 翻译到 go ,最后发现数字转换为 ascii 的时候,两个语言不一致, php 用的 chr(int), go 是直接用的 string(int),求教正确的姿势
    Supplement 1  ·  Oct 31, 2016
    都不对。。。。最后用 lua 改写了
    9 replies    2016-10-28 08:52:40 +08:00
    horsley
        1
    horsley  
       Oct 27, 2016   ❤️ 1
    姿势应该是正确的,倒是可以关注下这个 int 是不是超过 128
    displayabc
        2
    displayabc  
    OP
       Oct 27, 2016
    @horsley 就是超过 128 的就不正确了
    hst001
        3
    hst001  
       Oct 27, 2016   ❤️ 1
    []byte(strconv.Itoa(int))
    reus
        4
    reus  
       Oct 27, 2016   ❤️ 1
    string([]rune{rune(i % 256)})
    zeayes
        5
    zeayes  
       Oct 27, 2016   ❤️ 1
    char --> int rune('a')
    int --> char string([]byte{97})
    zwh8800
        6
    zwh8800  
       Oct 27, 2016   ❤️ 1
    golang 的 string 使用的是 utf8 编码
    reus
        7
    reus  
       Oct 27, 2016   ❤️ 1
    @zeayes 没有 char 这个类型,'a'和'你'的类型都是 int32 ,和 rune 一样。
    string 也不是 char ,这里根本就不应该出现[]byte 。 rune 到 string 只需要 string([]rune{97, 30000})这样。
    reus
        8
    reus  
       Oct 27, 2016   ❤️ 1
    @zwh8800 看文档, PHP 的 chr 是要先取模的
    zeayes
        9
    zeayes  
       Oct 28, 2016   ❤️ 1
    @reus 恩,明白。 golang 里的确没有 char 这个类型,楼主的意思应该字符和 int 的转换, python 里面对应的是 chr 和 ord , golang 里面就是 rune 和 string 。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5641 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 03:35 · PVG 11:35 · LAX 20:35 · JFK 23:35
    ♥ Do have faith in what you're doing.