4

I am using a third party tool in Go with the help of exec.Command and that program will print out a large integer value which obviously is in string format. I having trouble converting that string to int (or more specifically uint64).

Details: (You can ignore what program it is etc. but after running it will return me a large integer)

cmd := exec.Command(app, arg0, arg1, arg3)
stdout, err := cmd.Output()
if err != nil {
    fmt.Println(err.Error())
    return
}
temp := string(stdout)

After I ran above, I am trying to parse it as below

myanswer, err = strconv.Atoi(temp)  //I know this is not for uint64 but I am first trying for int but I actually need uint64 conversion
if err != nil {
    fmt.Println(err)
    return
}

Problem here is that the stdout is appending \r\n to its output which AtoI is not able to parse and gives the error

strconv.Atoi: parsing "8101832828\r\n": invalid syntax

Can someone pls help me on how to convert this string output to a uint64 and also int format pls?

1

1 Answer 1

2

The output contains special characters. First, you need to strip any these characters (e.g. \n or \r).
You can use strings.TrimSpace.

Then to parse a string as an int64, you would use:

if i, err := strconv.ParseInt(s, 10, 64); err == nil {
    fmt.Printf("i=%d, type: %T\n", i, i)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.