Re: [go-nuts] Int64 to []byte and vice-versa

2022-11-21 Thread 'Axel Wagner' via golang-nuts
Use `encoding/binary`. Yes, you have to care about byte order. But that's
inherent to the question. There is no canonical way in which an integer
correspond to a slice of bytes, you actually have to say how to encode it.
That also doesn't cause any problems - on the contrary, being explicit
about that means you *don't* get into issues (for not knowing what byte
order data is in). See also:
https://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html

So, you probably want to do
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(v))
and
v := int64(binary.BigEndian.Uint64(b))

On Mon, Nov 21, 2022 at 9:57 AM Nikhilesh Susarla 
wrote:

> Hi,
>
> I have an int64 value say 12
> I want to convert that to []byte array.
> How do we do that. There were lot of ways on the internet. Nothing which
> golang specifies or has a built-package for direct conversions. Lot of them
> dealt with Endiean's, but that might cause issues to I believe.
>
> So, is there any one recommended and safe way?
> Thank you
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/dffa4b81-9c7b-48d8-8b8b-262804756606n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfEfhYYOvLdvZri1_jJ%3DVYeGGyYB9UjXJX3Ud%2BKya%2BiUpg%40mail.gmail.com.


Re: [go-nuts] Int64 to []byte and vice-versa

2022-11-21 Thread Jan Mercl
On Mon, Nov 21, 2022 at 9:57 AM Nikhilesh Susarla
 wrote:

> I have an int64 value say 12
> I want to convert that to []byte array.

Such conversion is not supported. Also, []byte is a slice.

However, the desired result can be computed in code. You can use the
encoding/binary package for that. Endianness matters, you cannot avoid
choosing one or another as that selects different results. In some
contexts the right endianness is platform independent, like in many
network/serialization protocols. In some other cases one has to follow
the platform endianness.

HTH.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAA40n-WOn0gNK3N9Y93-7VictwAtRQuuQZ7bdBbhb8MjngZaGg%40mail.gmail.com.


[go-nuts] Int64 to []byte and vice-versa

2022-11-21 Thread Nikhilesh Susarla
Hi, 

I have an int64 value say 12
I want to convert that to []byte array. 
How do we do that. There were lot of ways on the internet. Nothing which 
golang specifies or has a built-package for direct conversions. Lot of them 
dealt with Endiean's, but that might cause issues to I believe.

So, is there any one recommended and safe way? 
Thank you

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/dffa4b81-9c7b-48d8-8b8b-262804756606n%40googlegroups.com.