dongqiangou5724 2016-01-15 09:56
浏览 155
已采纳

Go中int到字符串的强制转换如何工作?

I only started Go today, so this may be obvious but I couldn't find anything on it.

What does var x uint64 = 0x12345678; y := string(x) give y?

I know var x uint8 = 65; y := string(x) would give y the byte 65, character A, and common sense would suggest (since types larger than uint8 are allowed to be cast to strings) that they would simply be packed in to native byte order (i.e little endian) and assigned to the variable.

This does not seem to be the case:

hex.EncodeToString([]byte(y)) ==> "efbfbd"

First thought says this is an address with the last byte being left off because of some weird null terminator thingy, but if I allocate two x and y variables with two different values and print them out I get the same result.

var x, x2 uint64 = 0x10000000, 0x20000000
y, y2 := string(x), string(x2)
fmt.Println(hex.EncodeToString([]byte(y))) // "efbfbd"
fmt.Println(hex.EncodeToString([]byte(y2))) // "efbfbd"

Maddeningly I can't find the implementation for the string type anywhere although I probably haven't looked hard enough.

  • 写回答

1条回答 默认 最新

  • dragon071111 2016-01-15 09:59
    关注

    This is covered in the Spec: Conversions: Conversions to and from a string type:

    Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer. Values outside the range of valid Unicode code points are converted to "\uFFFD".

    So effectively when you convert a numeric value to string, it can only yield a string having one rune (character). And since Go stores strings as the UTF-8 encoded byte sequences in memory, that is what you will see if you convert your string to []byte:

    Converting a value of a string type to a slice of bytes type yields a slice whose successive elements are the bytes of the string.

    When you try to conver the 0x12345678, 0x10000000 and 0x20000000 values to string, since they are outside of the range of valid Unicode code points, as per spec they are converted to "\uFFFD" which in UTF-8 encoding is []byte{239, 191, 189}; when encoded to hex string:

    fmt.Println(hex.EncodeToString([]byte("\uFFFD"))) // Output: efbfbd
    

    Or simply:

    fmt.Printf("%x", "\uFFFD") // Output: efbfbd
    

    Read the blog post Strings, bytes, runes and characters in Go for more details about string internals.

    And btw since Go 1.5 the Go runtime is implemented (mostly) in Go, so these conversions are now implemented in Go and can be found in the runtime package: runtime/string.go, look for the intstring() function.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog