Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string to uint in go lang

Tags:

go

go-imagick

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 
like image 996
Naresh Avatar asked Feb 02 '16 13:02

Naresh


People also ask

How to convert string to integer type in Golang?

In order to convert string to integer type in Golang, you can use the following methods. 1. Atoi () Function: The Atoi stands for ASCII to integer and returns the result of ParseInt (s, 10, 0) converted to type int.

What is INT in Go language?

Int is a generic type that can accommodate values based on the platform Operating System. You can check other posts on Convert Int to String. How to Convert String to Int in Go Language? There are 3 ways to convert a string to an Integer type in the Go language. strconv.parseInt () the function takes a string parameter and returned an integer value

What are signed and unsigned integers in Go language?

Both signed and unsigned integers are accessible in four sizes in the Go language. We may occasionally receive a number in string format and need to perform a mathematical operation on it. To conduct any mathematical action, we must explicitly convert this string type to an integer type.

How to convert a string to an unsigned int in Python?

To convert a string to an unsigned int you can use the method strconv.ParseUint. ParseUint interprets a string s in the given base (2 to 36) and returns the corresponding value i.


1 Answers

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 
like image 108
peterSO Avatar answered Sep 20 '22 22:09

peterSO