duankanjian4642 2017-02-28 03:27
浏览 132
已采纳

ParseInt()中使用的基本参数是什么?

I have come across many sample code whenever we try to convert string to int we use :

parseValue, err := strconv.ParseInt(value, 10, 64)

So the code above ParseInt() has three arguments. from the documentation code :

func ParseInt(s string, base int, bitSize int) (i int64, err error) {

I tried to understand the base inthere so I change the value from 0 to 16. PlayGolang.

The result is ok when the input are 0 and 10. Number other that 0 and 10 are panic. I still confused and not understand. Can someone explain what base is used for in ParseInt()?

  • 写回答

1条回答 默认 最新

  • dongxiaoke2018 2017-02-28 03:36
    关注

    You are converting a string to an integer. The method is asking you for the base (number system) the string is in. You can learn more about number systems here: https://en.wikipedia.org/wiki/Radix

    For example (using your code snippet provided here):

    var s string = "1111" // This string is in binary (Base 2)
    i, err := strconv.ParseInt(s, 2, 64) // Give the base as 2
    

    Result:

    Hello, 15 with type int64!
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题