doufulian4076 2016-02-02 13:30
浏览 2517
已采纳

在go lang中将字符串转换为uint

I am trying to convert the string to uint on 32-bit ubuntu using the following code. But it always convert it in uint64 despite explicitly passing 32 as the argument in the function. Below in the code mw is the object of the image magick library. Which returns uint when mw.getImageWidth() and mw.getImageHeight() is called. Also, it accepts the uint type argument in the resize function.

    width :=  strings.Split(imgResize, "x")[0]
    height := strings.Split(imgResize, "x")[1]

    var masterWidth uint = mw.GetImageWidth() 
    var masterHeight uint = mw.GetImageHeight() 

    mw := imagick.NewMagickWand()
    defer mw.Destroy()

    err = mw.ReadImageBlob(img)
    if err != nil {
            log.Fatal(err)
        } 

    var masterWidth uint = mw.GetImageWidth() 
    var masterHeight uint = mw.GetImageHeight()

    wd, _ := strconv.ParseUint(width, 10, 32)
    ht, _ := strconv.ParseUint(height, 10, 32)

   if masterWidth < wd || masterHeight < ht { 
     err = mw.ResizeImage(wd, ht, imagick.FILTER_BOX, 1)
     if err != nil {
        panic(err)
    } 
   }

Error is :

# command-line-arguments
test.go:94: invalid operation: masterWidth < wd (mismatched types uint and uint64)
goImageCode/test.go:94: invalid operation: masterHeight < ht (mismatched types uint and uint64)
goImageCode/test.go:100: cannot use wd (type uint64) as type uint in argument to mw.ResizeImage
goImageCode/AmazonAWS.go:100: cannot use ht (type uint64) as type uint in argument to mw.ResizeImage
  • 写回答

1条回答 默认 最新

  • douhang5460 2016-02-02 13:32
    关注

    Package strconv

    func ParseUint

    func ParseUint(s string, base int, bitSize int) (n uint64, err error)
    

    ParseUint is like ParseInt but for unsigned numbers.

    func ParseInt

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

    ParseInt interprets a string s in the given base (2 to 36) and returns the corresponding value i. If base == 0, the base is implied by the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise.

    The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64.

    The errors that ParseInt returns have concrete type *NumError and include err.Num = s. If s is empty or contains invalid digits, err.Err = ErrSyntax and the returned value is 0; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange and the returned value is the maximum magnitude integer of the appropriate bitSize and sign.

    The bitSize argument specifies the integer type that the result must fit into. The uint type size is implementation defined, either 32 or 64 bits. The ParseUint return type is always uint64. For example,

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    func main() {
        width := "42"
        u64, err := strconv.ParseUint(width, 10, 32)
        if err != nil {
            fmt.Println(err)
        }
        wd := uint(u64)
        fmt.Println(wd)
    }
    

    Output:

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

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误