Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"panic: strconv.ParseInt: parsing "�": invalid syntax" –> when parting int64 to convert into time

In a DB I have a field of type int64 in which I store unix timestamp. Then I want to present it to the user as a normal datetime, as a string. However, it’ll fail. Here’s a simple example.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var a int64
    a = 1658545089
    tm, err := strconv.ParseInt(string(a), 10, 64)
    if err != nil {
        panic(err)
    }

    fmt.Println(tm)
}

===>

panic: strconv.ParseInt: parsing "�": invalid syntax

What’s going on here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It is because you are trying to convert int64 with string
try it with strconv.FormatInt(a, 10)

package main

import (
    "fmt"
    "strconv"
)

func main() {
    var a int64
    a = 1658545089
    tm, err := strconv.ParseInt(strconv.FormatInt(a, 10), 10, 64)
    if err != nil {
        panic(err)
    }

    fmt.Println(tm)
}

when you try to convert an interger covering with string(), in golang it will get the corresponding ascii character
https://en.cppreference.com/w/cpp/language/ascii

after a certain interger it will only show �

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading