How to convert uint8 to string

后端 未结 5 1218
感动是毒
感动是毒 2020-11-27 08:10

http://play.golang.org/p/BoZkHC8_uA

I want to convert uint8 to string but can\'t figure out how.

  package main

  import \"fmt\"
  import \"strconv\         


        
5条回答
  •  自闭症患者
    2020-11-27 08:19

    There is a difference between converting it or casting it, consider:

    var s uint8 = 10
    fmt.Print(string(s))
    fmt.Print(strconv.Itoa(int(s)))
    

    The string cast prints '\n' (newline), the string conversion prints "10". The difference becomes clear once you regard the []byte conversion of both variants:

    []byte(string(s)) == [10] // the single character represented by 10
    []byte(strconv.Itoa(int(s))) == [49, 48] // character encoding for '1' and '0'
    
    see this code in play.golang.org

提交回复
热议问题