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

char array #28

Closed
majimboo opened this issue Jul 6, 2014 · 5 comments
Closed

char array #28

majimboo opened this issue Jul 6, 2014 · 5 comments
Labels

Comments

@majimboo
Copy link

majimboo commented Jul 6, 2014

How would this be on byte buffer?

UINT8 data[32]
@dcodeIO
Copy link
Member

dcodeIO commented Jul 7, 2014

Well, it's 32 bytes. You may either #slice(offset, offset+32) it or read it sequentially through calling #readUint8(offset++) successively.

You might also want to take a look at the API docs.

@majimboo
Copy link
Author

Sorry, I still don't get it. I currently need to write it. It's actually a string. Let us say

UINT8 data[16]
UINT8 data[16]

Could be

42 75 66 66 65 72 00 00 00 00 00 00 00 00 00 00 4d 61 74 74 65 72 00 00 00 00 00 00 00 00 00 00

If I try to

writeString('Buffer', 16) // suggestion
writeString('Matter', 16) // suggestion

@dcodeIO
Copy link
Member

dcodeIO commented Jul 13, 2014

  1. Remember the current offset
  2. Write the string
  3. While 16 bytes are not reached, write 0
var s = "Buffer",
    o = bb.offset;
bb.writeString(s);
while (bb.offset < o + 16)
    bb.writeUint8(0);
...

@majimboo
Copy link
Author

Would love to see this get supported with bytebuffer. Cheers, great as always.

@dcodeIO
Copy link
Member

dcodeIO commented Jul 14, 2014

You may also extend the prototype with your use case specific code for comfort and a bit of speed:

mybytebuffer.js

var ByteBuffer = require("bytebuffer");

ByteBuffer.prototype.writeStringPadded = function(s, len) {
    var end = this.offset + len;
    this.ensureCapacity(end);
    this.writeString(s);
    while (this.offset < end)
        this.writeUint8(0);
    return this;
};

module.exports = ByteBuffer;
var ByteBuffer = require("mybytebuffer.js");
var bb = new ByteBuffer();
bb.writeStringPadded("Buffer", 16);
...

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

No branches or pull requests

2 participants