How do I convert a byte into BigEndian or LittleEndian?

1,803 views
Skip to first unread message

Glen Newton

unread,
Feb 27, 2017, 4:45:19 PM2/27/17
to golang-nuts


Given a byte b, how do I convert it to a byte of particular endianness?
It is not clear to me looking at
"encoding/binary"

(I am assuming that golang byte endianness varies across
architectures. If this is wrong please educate me). :-)

Thanks,
Glen

Caleb Spare

unread,
Feb 27, 2017, 4:50:39 PM2/27/17
to Glen Newton, golang-nuts
What do you mean by the endianness of a single byte? Endianness is
about the order of a sequence of bytes.
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ayan George

unread,
Feb 27, 2017, 4:50:41 PM2/27/17
to golan...@googlegroups.com
Endianness only makes sense when talking about multiple bytes. One byte
is the same in little and big endian. :P

Still, I assume you're talking about multiple bytes so you should check
out the encoding/binary package:

https://godoc.org/encoding/binary



Glen Newton

unread,
Feb 27, 2017, 4:54:48 PM2/27/17
to golang-nuts
Ack! Sorry.

I was referring to bit endianness https://en.wikipedia.org/wiki/Bit_numbering

Wim Lewis

unread,
Feb 27, 2017, 5:01:25 PM2/27/17
to Glen Newton, golang-nuts
On Feb 27, 2017, at 1:54 PM, Glen Newton <glen....@gmail.com> wrote:
Ack! Sorry.

I was referring to bit endianness https://en.wikipedia.org/wiki/Bit_numbering


I think the golang package only deals with byte-oriented interfaces. Most CPUs can't address units smaller than a byte anyway, so endianness isn't really observable from software. So the bit-endianness of individual bytes is determined by whatever you are passing those bytes to (presumably some hardware port?).


Glen Newton

unread,
Feb 27, 2017, 5:08:20 PM2/27/17
to golang-nuts
Ah, I've been thinking about this issue too much and have confused myself.

And, I have expressed it as an XY Problem!  :-(

My real question should have been: if I write a byte to a file on one architecture, will the byte be the same on all other architectures (i.e. 386, amd64, arm, s390x, ppc64le) if read by a Go program on them?

Sorry for my confusion.  :-)

Thanks,
Glen


On Monday, February 27, 2017 at 4:45:19 PM UTC-5, Glen Newton wrote:

Caleb Spare

unread,
Feb 27, 2017, 5:10:10 PM2/27/17
to Glen Newton, golang-nuts
That's much easier: yes.

Glen Newton

unread,
Feb 27, 2017, 5:11:40 PM2/27/17
to golang-nuts
Thanks!  :-)


On Monday, February 27, 2017 at 4:45:19 PM UTC-5, Glen Newton wrote:
Reply all
Reply to author
Forward
0 new messages