Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reading bytes without offset #26

Closed
majimboo opened this issue Jun 28, 2014 · 3 comments
Closed

reading bytes without offset #26

majimboo opened this issue Jun 28, 2014 · 3 comments

Comments

@majimboo
Copy link

I want to read bytes in LE order

// coordinates data (walk)
// 100c 70e3 76ba 73c3
// 100c 92e3 af8a 1266
// 100c b1e3 d34b c2b8
// 100c d0e3 6168 aec3

// set to LE
buf.order(true)

var w = buf.readUint32(0)
var x = buf.readUint32()
var y = buf.readUint32()
var z = buf.readUint32()

The docs says:

Offset to read from. Will use and increase ByteBuffer#offset by 4 if omitted.

So why am I getting the error:

RangeError: Illegal offset: 0 <= 8 (+4) <= 11

Oh should be readUint16() but how to make it read LE?

@dcodeIO
Copy link
Member

dcodeIO commented Jun 28, 2014

Assuming that buf has been filled and properly flipped

var w = buf.readUint32() // also no offset
var x = buf.readUint32()
var y = buf.readUint32()
var z = buf.readUint32()

will read one Uint32 after another (relative). This is equal to (absolute):

var w = buf.readUint32(0)
var x = buf.readUint32(4)
var y = buf.readUint32(8)
var z = buf.readUint32(12)
buf.offset = 16;

Regarding byte order: Once set to LE, all operations following will use LE until you set it to BE again.

buf.LE();
...
...
...
buf.BE();

@majimboo
Copy link
Author

Yea, I realized. Got it solved. Except I don't get why I should properly FLIP it? What is the implications of flip? I didn't use flip but I'm getting proper results.

Thanks though, this is great :)

@dcodeIO
Copy link
Member

dcodeIO commented Jun 28, 2014

If you just wrap some data, flipping is not necessary. It is, though, if you fill the buffer successively just like you read it in the example above:

var buf = new ByteBuffer(6); // initializes with offset = 0, limit = 6
buf.writeUint32(123); // afterwards: offset = 4, limit = 6
buf.flip(); // afterwards: limit = offset = 4, offset = 0, ready for read operations
...

See

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants