According to this page (see section Sending an Unicode SMS message), the 8bit encoding is in fact UCS-2.
I don't know enough about nodejs to give you the full implementation, but here is a .NET sample:
string EncodeSmsText(string text)
{
// Convert input string to a sequence of bytes in BigEndian UCS-2 encoding
// 'Hi' -> [0, 72, 0, 105]
var bytes = Encoding.BigEndianUnicode.GetBytes(text);
// Encode bytes to hex representation
// [0, 72, 0, 105] -> '00480069'
return BitConverter.ToString(bytes).Replace("-", "");
}
Please note that according to this post my code will not work for characters encoded as surrogate pairs, because Encoding.BigEndianEncoding
is UTF-16 (not UCS-2).
Edit
Here is NodeJS version that uses the built-in UCS2 converter in Buffer class:
function swapBytes(buffer) {
var l = buffer.length;
if (l & 0x01) {
throw new Error('Buffer length must be even');
}
for (var i = 0; i < l; i += 2) {
var a = buffer[i];
buffer[i] = buffer[i+1];
buffer[i+1] = a;
}
return buffer;
}
function encodeSmsText(input) {
var ucs2le = new Buffer(input, 'ucs2');
var ucs2be = swapBytes(ucs2le);
return ucs2be.toString('hex');
}
console.log(encodeSmsText('Hi'));
Inspired by these SO answers: