在 go lang 中将字符串转换为 uint
convert string to uint in go lang
我正在尝试使用以下代码将字符串转换为 32 位 ubuntu 上的 uint。但它总是在 uint64 中转换它,尽管明确地将 32 作为函数中的参数传递。下面的代码 mw 是 image magick library. Which returns uint
when mw.getImageWidth()
and mw.getImageHeight()
is called. Also, it accepts the uint
type argument in the resize 函数的对象。
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
func ParseUint(s string, base int, bitSize int) (n uint64, err error)
ParseUint is like ParseInt but for unsigned numbers.
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.
bitSize
参数指定结果必须的整数类型
适合。 uint
类型大小是实现定义的,32 位或 64 位。 ParseUint
return 类型总是 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
我正在尝试使用以下代码将字符串转换为 32 位 ubuntu 上的 uint。但它总是在 uint64 中转换它,尽管明确地将 32 作为函数中的参数传递。下面的代码 mw 是 image magick library. Which returns uint
when mw.getImageWidth()
and mw.getImageHeight()
is called. Also, it accepts the uint
type argument in the resize 函数的对象。
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
func ParseUint(s string, base int, bitSize int) (n uint64, err error)
ParseUint is like ParseInt but for unsigned numbers.
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.
bitSize
参数指定结果必须的整数类型
适合。 uint
类型大小是实现定义的,32 位或 64 位。 ParseUint
return 类型总是 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