What's the appropriate way to pack an integer of finite length into a []byte? So far, I've just been using byte((val >> 24) & 0xff), etc. But, I need a more efficient way. Is there a Go specific interface that handles endianness for me?
Thanks,D--
Tried that but encoding/binary only deals with int64. I get a type error when I try and use, say, the length of a string, which is an int.
--
The point of Rob's article (the way I interpret it) is that there is no "platform appropriate" and "platform inappropriate" unsigned integers (or, more precisely, there shouldn't be). "Platform inappropriate" is not an unsigned integer, just a sequence of bytes, that is inappropriately accessed through a variable of type unsigned integer, when it should be an array of bytes.
I think a large part of the confusion comes from the family of functions mentioned in the thread title: ntohl/htonl. Actually, implementing an "ntohl/htonl equivalent" (converting between uint32 and "potentially platform-inappropriate uint32") does indeed require knowing the machine byte order. But the point is that converting between uint32 and "potentially platform-inappropriate uint32" is the wrong approach in the first place, with the right approach being converting between uint32 and []byte and letting the compiler take care of endianness (as well as alignment and optimization).
On Saturday, October 6, 2012 9:17:05 AM UTC-4, speter wrote:The point of Rob's article (the way I interpret it) is that there is no "platform appropriate" and "platform inappropriate" unsigned integers (or, more precisely, there shouldn't be). "Platform inappropriate" is not an unsigned integer, just a sequence of bytes, that is inappropriately accessed through a variable of type unsigned integer, when it should be an array of bytes.But that's just silly. How you gonna do math with just a bag of out-of-order bytes?
I think a large part of the confusion comes from the family of functions mentioned in the thread title: ntohl/htonl. Actually, implementing an "ntohl/htonl equivalent" (converting between uint32 and "potentially platform-inappropriate uint32") does indeed require knowing the machine byte order. But the point is that converting between uint32 and "potentially platform-inappropriate uint32" is the wrong approach in the first place, with the right approach being converting between uint32 and []byte and letting the compiler take care of endianness (as well as alignment and optimization).That is the lesson I need to take. Otherwise, I might as well be doing the thing in Java.
--
Obviously, as it is, you can't and you don't. When you receive a "bag of bytes" through the network, you treat them like a bag of bytes (and not like a bag of uint32's or whatever they represent): you keep them in a []byte. (Actually in Go you could use channel of bytes as well.) When you need to do math with the values, as explained below, convert them from []byte to ("platform appropriate") uint32, either using the snippet above in the thread, or using encoding/binary. The point is that you shouldn't ever have to have (and access data through) "platform-inappropriate" uint32 variables.
Peter
On Sat, Oct 6, 2012 at 11:02 PM, David Wright <d.wrig...@gmail.com> wrote:
> This may now constitute a threadjacking because Java is way off topic. If
> so, sorry.
>
> The shift operators are always going to assume big-endian, because they're
> for dealing with Java types.
This statement demonstrates a profound misunderstanding. Shift
operators work on integers. They have no byte order and "assume"
nothing about it.
Byte order is defined by whether successive byte addresses in a
computer correspond to higher or lower significant bytes of the
aliased multibyte integer occupying those several addresses.
--
On Sat, Oct 26, 2013 at 1:00 AM, <zubin....@gmail.com> wrote:I don't doubt this is true in your case, but I have to say that it
>
> Granted, the unsafe method is not endian-safe, but I don't really care about
> that since the B-Tree will only ever be running on the same architecture it
> was created on.
pains me to recall the number of times in my career I have had to
clean up after such assumptions. Systems change, but some code lives
forever.
No.
> Is there any way other than using unsafe to achieve the
> high performance reading and writing of integers to and from a byte slice?
Since you are reading from disk, it seems mildly implausible that the
hot spot in your program is the time it takes to encode an integer
into a byte slice.
If it were the hot spot for me I would try writing a []int and use
unsafe to convert that to a []byte.
Ian
But it still bothers me that there is no efficient way to make use of processor instruction like intel bswap to do the translation for me so one never has to worry about it.