【发布时间】:2016-12-19 18:01:54
【问题描述】:
我想将字符串转换为字节,如果我将值分配给var str 为$scope.event[0],它工作正常,它只给我第一个索引的字节,如果我分配$scope.event[] 它会抛出错误str.charCodeAt is not a function。我怎样才能得到数组的总字节数?
ctrl.js
$scope.event = ["lorem ipsum", "lorem ipsum","lorem ipsum"]
function jsonToArray() {
var total = 0;
var str = $scope.event;
var bytes = []; // char codes
var bytesv2 = []; // char codes
for (var i = 0; i < str.length; ++i) {
var code = str.charCodeAt(i);
bytes = bytes.concat([code]);
bytesv2 = bytesv2.concat([code & 0xff, code / 256 >>> 0]);
}
var totalBytes = 0;
console.log('bytes', bytes.join(', '));
for ( var i = 0 ; i < bytes.length ; i++ ) {
totalBytes += bytes[i];
totalCurrentBytes.push(totalBytes);
}
console.log('Total bytes', totalBytes);
formatBytes(totalBytes);
}
jsonToArray();
【问题讨论】:
标签: javascript arrays binary