I need to perform a Java function in NodeJs.
string.getBytes()
In Java, this translates a string to a byte array byte[]
.
I need to perform a Java function in NodeJs.
string.getBytes()
In Java, this translates a string to a byte array byte[]
.
@Eranga Kapukotuwa I don't know if you still need an answer for this question but I have used the below code to get this work done in one of my projects and it works perfect.
var getBytes = function (string) {
var utf8 = unescape(encodeURIComponent(string));
var arr = [];
for (var i = 0; i < utf8.length; i++) {
arr.push(utf8.charCodeAt(i));
}
console.log('Array ', arr);
return arr;
}
You can try to do the same
Class Method: Buffer.byteLength(string[, encoding])#
Return: Number
Buffer.byteLength(str, 'utf8')
This should help:
> s = 'あhello'
'あhello'
> s.split('').forEach(function(c,i) { console.log(s.charCodeAt(i) + " " + c); });
12354 あ
104 h
101 e
108 l
108 l
111 o
undefined
// Or as one line
s.split('').map(function(c,i) { return s.charCodeAt(i) }).reduce(function(a, b) {return a.toString(16) + " " + b.toString(16)})