convert string to uint in go lang

huangapple go评论78阅读模式
英文:

convert string to uint in go lang

问题

我正在尝试使用以下代码将字符串转换为32位Ubuntu上的uint。但是,尽管在函数中明确传递32作为参数,但它总是将其转换为uint64。在代码中,mw是image magick库的对象。当调用mw.getImageWidth()mw.getImageHeight()时,它返回uint类型。此外,它在resize函数中接受uint类型的参数。

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)
    }
}

错误信息为:

# 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
英文:

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, &quot;x&quot;)[0]
    height := strings.Split(imgResize, &quot;x&quot;)[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 &lt; wd || masterHeight &lt; 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 &lt; wd (mismatched types uint and uint64)
goImageCode/test.go:94: invalid operation: masterHeight &lt; 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

得分: 53

【Package strconv】

【func ParseUint】

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

ParseUint函数类似于ParseInt,但用于无符号数。

【func ParseInt】

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

ParseInt函数将给定进制(2到36)中的字符串s解释为相应的值i。如果base == 0,则基数由字符串的前缀隐含:对于"0x",基数为16;对于"0",基数为8;否则,基数为10。

bitSize参数指定结果必须适应的整数类型。位大小0、8、16、32和64对应于int、int8、int16、int32和int64。

ParseInt返回的错误具有具体类型*NumError,并且包含err.Num = s。如果s为空或包含无效的数字,则err.Err = ErrSyntax,并且返回值为0;如果s对应的值不能由给定大小的有符号整数表示,则err.Err = ErrRange,并且返回值为适当bitSize和符号的最大幅度整数。

bitSize参数指定结果必须适应的整数类型。uint类型的大小是实现定义的,可以是32位或64位。ParseUint的返回类型始终为uint64

示例:

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)
}

输出:

42

更多信息请参考:
Package strconv
func ParseUint
func ParseInt

英文:

> 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 (
	&quot;fmt&quot;
	&quot;strconv&quot;
)

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

Output:

42

答案2

得分: 1

这是将字符串转换为无符号整数的简便方法。

import (
	"strconv"
)

...

func StringToUint(s string) uint {
	i, _ := strconv.Atoi(s)
	return uint(i)
}

这段代码可以将字符串转换为无符号整数。

英文:

Here is short way to convert string to uint.

import (
	&quot;strconv&quot;
)

...

func StringToUint(s string) uint {
	i, _ := strconv.Atoi(s)
	return uint(i)
}

huangapple
  • 本文由 发表于 2016年2月2日 21:30:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/35154875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定