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 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?