[go-nuts] Re: int64 -> []byte writing 9 bytes?

2017-02-27 Thread zeebo
You're using the variable width encoding. The number of bytes of a variable 
width encoded int64 will depend on the magnitude of the value. If you use 
binary.BigEndian or binary.LittleEndian you can use the PutUint64 method 
which will always be 8 bytes.

On Monday, February 27, 2017 at 11:27:07 AM UTC-7, Guillermo Estrada wrote:
>
> Hey Gophers! I'm having a bit of trouble understanding something about the 
> standard library, I'm pretty sure either it is not wrong, or there is a 
> reason behind it, but either way I don't understand which one. As the title 
> suggests, I'm using encode/binary to write a int64 into a byte slice, but 
> apparently... it won't fit into an 8 byte slice... (64 bits right?). Well, 
> here's a bit of code:
>
> import (
> "encoding/binary"
> "fmt"
> "time"
> )
>
> func main() {
> t := time.Now()
> b1 := make([]byte, 10)
> b2 := make([]byte, 10)
> u1 := binary.PutVarint(b1, t.Unix())
> u2 := binary.PutVarint(b2, t.UnixNano())
> i1, v1 := binary.Varint(b1)
> i2, v2 := binary.Varint(b2)
> fmt.Println("Unix:", t.Unix(), b1, "/", u1, "->", i1, "/", v1)
> fmt.Println("UnixNano:", t.UnixNano(), b2, "/", u2, "->", i2, "/", v2)
> }
>
>
> *Sample output:*
>
> Unix: 1488220019 [230 189 163 139 11 0 0 0 0 0] /* 5* -> 1488220019 / *5*
> UnixNano: 1488220019858895600 [224 203 131 245 163 142 156 167 41 0] / *9* 
> -> 1488220019858895600 / *9* 
>
> As you can see, both ways reports 5/9 bytes being written in and from the 
> byte slice. I'm trying to use Unix times as ordered keys in a DB, I can 
> totally use the 9 byte slices, I just don't understand why a 64 bit number 
> would write 9 bytes (or a 32bit into 5 for Unix()). Is there something I'm 
> missing??
>
> Thank you for your time!
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: int64 -> []byte writing 9 bytes?

2017-02-27 Thread Guillermo Estrada
On Monday, February 27, 2017 at 12:41:43 PM UTC-6, zeebo wrote:
>
> You're using the variable width encoding. The number of bytes of a 
> variable width encoded int64 will depend on the magnitude of the value. If 
> you use binary.BigEndian or binary.LittleEndian you can use the 
> *PutUint64* method which will always be 8 bytes.
>

Thank you! I can see I can find them here if I go into the code:

https://golang.org/src/encoding/binary/binary.go

Although those functions are NOT documented here:

https://golang.org/pkg/encoding/binary/

Shouldn't they be?

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: int64 -> []byte writing 9 bytes?

2017-02-27 Thread howardcshaw
You are missing that it is encoding. Specifically, using VarInt is asking 
for Variable integer 
encoding: https://developers.google.com/protocol-buffers/docs/encoding#varints

The important bit here is that the Varint stores 7-bits of data and 1-bit 
of metadata in each byte (the metadata being whether there are more bits). 
Have a look at the value you stored, 1488220019858895600. In binary, that 
is 1 0100 1010 0111 0011 1000 0011 1001 0001  0101  0111 0010  
, which takes 61 bits. Using 7 bits per byte, this takes 8.7 bytes 
to store, in other words, long enough that it takes 9 bytes because the 
encoding does not use partial by

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: int64 -> []byte writing 9 bytes?

2017-02-27 Thread howardcshaw
Look a little further down - they are documented in the ByteOrder type.

A ByteOrder specifies how to convert byte sequences into 16-, 32-, or 
64-bit unsigned integers.

type ByteOrder interface {
Uint16([]byte ) uint16 

Uint32([]byte ) uint32 

Uint64([]byte ) uint64 

PutUint16([]byte , uint16 
)
PutUint32([]byte , uint32 
)
PutUint64([]byte , uint64 
)
String() string 
}


and the other piece is up near the top:

Variables ΒΆ 

BigEndian is the big-endian implementation of ByteOrder.

var BigEndian bigEndian

LittleEndian is the little-endian implementation of ByteOrder.
var LittleEndian littleEndian 

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: int64 -> []byte writing 9 bytes?

2017-02-27 Thread Guillermo Estrada
Thanks a lot! This works as expected, I think functions for BigEndian and 
LittleEndian should be documented or at least referenced in the binary 
documentation.

import (
"encoding/binary"
"fmt"
"time"
)

func main() {
t := time.Now()
b1 := make([]byte, 8)
b2 := make([]byte, 8)
binary.BigEndian.PutUint64(b1, uint64(t.Unix()))
binary.BigEndian.PutUint64(b2, uint64(t.UnixNano()))
i1 := binary.BigEndian.Uint64(b1)
i2 := binary.BigEndian.Uint64(b2)
fmt.Println("Unix:", t.Unix(), b1, "->", i1)
fmt.Println("UnixNano:", t.UnixNano(), b2, "->", i2)
}


On Monday, February 27, 2017 at 12:44:56 PM UTC-6, Guillermo Estrada wrote:
>
> On Monday, February 27, 2017 at 12:41:43 PM UTC-6, zeebo wrote:
>>
>> You're using the variable width encoding. The number of bytes of a 
>> variable width encoded int64 will depend on the magnitude of the value. If 
>> you use binary.BigEndian or binary.LittleEndian you can use the 
>> *PutUint64* method which will always be 8 bytes.
>>
>
> Thank you! I can see I can find them here if I go into the code:
>
> https://golang.org/src/encoding/binary/binary.go
>
> Although those functions are NOT documented here:
>
> https://golang.org/pkg/encoding/binary/
>
> Shouldn't they be?
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: int64 -> []byte writing 9 bytes?

2017-02-27 Thread Guillermo Estrada


On Monday, February 27, 2017 at 12:48:53 PM UTC-6, howar...@gmail.com wrote:
>
> You are missing that it is encoding. Specifically, using VarInt is asking 
> for Variable integer encoding: 
> https://developers.google.com/protocol-buffers/docs/encoding#varints
>
> The important bit here is that the Varint stores 7-bits of data and 1-bit 
> of metadata in each byte (the metadata being whether there are more bits). 
> Have a look at the value you stored, 1488220019858895600. In binary, that 
> is 1 0100 1010 0111 0011 1000 0011 1001 0001  0101  0111 0010  
> , which takes 61 bits. Using 7 bits per byte, this takes 8.7 bytes 
> to store, in other words, long enough that it takes 9 bytes because the 
> encoding does not use partial by
>

This is actually really interesting, thanks a lot! 

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: int64 -> []byte writing 9 bytes?

2017-02-27 Thread Guillermo Estrada
Thanks! I guess I missed them cause of the ordering variables being on the 
very top and ByteOrder interface being the last. I knew there had to be a 
way and I was missing something.

On Monday, February 27, 2017 at 12:51:08 PM UTC-6, howar...@gmail.com wrote:
>
> Look a little further down - they are documented in the ByteOrder type.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.