Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.JS Big-Endian UCS-2

Node.JS Big-Endian UCS-2

Problem

I'm working with Node.JS. Node's buffers support little-endian UCS-2, but not big-endian, which I need. How would I do so?

Problem courtesy of: skeggse

Solution

According to wikipedia, UCS-2 should always be big-endian so it's odd that node only supports little endian. You might consider filing a bug. That said, switching endian-ness is fairly straight-forward since it's just a matter of byte order. So just swap bytes around to go back and forth between little and big endian, like so:

function swapBytes(buffer) {
  var l = buffer.length;
  if (l & 0x01) {
    throw new Error('Buffer length must be even');
  }
  for (var i = 0; i 
Solution courtesy of: broofa

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.JS Big-Endian UCS-2

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×