Home:ALL Converter>How convert *uint64 and *uint32 data types to the int data type?

How convert *uint64 and *uint32 data types to the int data type?

Ask Time:2020-03-12T18:21:22         Author:Nurzhan Nogerbek

Json Formatter

In my case, I have 2 variables with data types as *uint64 and *uint32. I need to convert them to the int data type.

When I try to convert them with int() function, it raise such error:

Cannot convert an expression of type *uint64 to type int.

I notice that the same int() function works without any problem if data types don't have * (asterisk) character.

So my question is how correctly convert *uint64 and *uint32 data types to the int data type?!

Author:Nurzhan Nogerbek,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60651999/how-convert-uint64-and-uint32-data-types-to-the-int-data-type
icza :

You can't (shouldn't) convert a pointer to int, what you most likely want to do is convert the pointed value to int.\n\nSo simply dereference the pointer to get an uint64 and uint32 value, which you can convert:\n\nvar i *uint64 = new(uint64)\nvar j *uint32 = new(uint32)\n*i = 1\n*j = 2\n\nvar k, l int\nk = int(*i)\nl = int(*j)\n\nfmt.Println(k, l)\n\n\nThis outputs (try it on the Go Playground):\n\n1 2\n\n\nNote that of course if the pointer is nil, attempting to dereference it (like *i) results in a runtime panic.\n\nAlso note that the the size (and thus the valid range) of int is platform dependent, it may be 64 and 32 bit. Thus converting any uint64 value to int may lose the upper 32 bits, and certain positive input numbers may turn into negative ones.",
2020-03-12T10:24:13
yy