Javascript将字符串转换为kb格式
我是javascript新手,我只想将字符串转换成像我这样的人可以阅读的格式。这是一个我正在尝试做的例子 string2size(字符串){ //一些很棒的编码,我不知道怎么做 回敬 } 现在返回应该给我56字节或12kb或1mb,这取决于字符串的大小 所以如果字符串是。。。 string=“有一位住在鞋子里的老妇人”; 然后string2size(string)应该返回类似3kb的值 现在我知道有一个utf8的谈话,我不会反对和增加的功能 我尝试过谷歌和雅虎搜索,但他们谈到使用php,但我真的需要它的javascript。我非常感谢任何人抽出时间。Javascript将字符串转换为kb格式,javascript,string,byte,Javascript,String,Byte,我是javascript新手,我只想将字符串转换成像我这样的人可以阅读的格式。这是一个我正在尝试做的例子 string2size(字符串){ //一些很棒的编码,我不知道怎么做 回敬 } 现在返回应该给我56字节或12kb或1mb,这取决于字符串的大小 所以如果字符串是。。。 string=“有一位住在鞋子里的老妇人”; 然后string2size(string)应该返回类似3kb的值 现在我知道有一个utf8的谈话,我不会反对和增加的功能 我尝试过谷歌和雅虎搜索,但他们谈到使用php,但我真的
-Teske首先列出要使用的单位。例如:
// 1024-based units. Kibibyte, Mebibyte etc.
//
var BINARY_UNITS= [1024, 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yo'];
// SI units, also Hard Disc Manufacturers' rip-off kilobytes
//
var SI_UNITS= [1000, 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
然后制作一个函数,查找并除以数字的最大合适单位:
function unitify(n, units) {
for (var i= units.length; i-->1;) {
var unit= Math.pow(units[0], i);
if (n>=unit)
return Math.floor(n/unit)+units[i];
}
return n; // no prefix, single units
}
然后,访问一个长度:
var desc= 'File, '+unitify(content.length, UNITS_BINARY)+'B';
desc+= ' or in SI, '+unitify(content.length, UNITS_SI)+'B';
// eg. File, 977KiB or in SI 1MB
我不知道你说的UTF-8是什么意思,但是如果你想知道编码为字节的字符串的长度,你必须自己将该字符串编码为UTF-8。幸运的是,在JavaScript中获得UTF-8编码器有一个便宜的技巧:
var bytes= unescape(encodeURIComponent(chars));
alert(unitify(bytes, BINARY_UNITS)+'B');
首先列出要使用的单位。例如:
// 1024-based units. Kibibyte, Mebibyte etc.
//
var BINARY_UNITS= [1024, 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yo'];
// SI units, also Hard Disc Manufacturers' rip-off kilobytes
//
var SI_UNITS= [1000, 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
然后制作一个函数,查找并除以数字的最大合适单位:
function unitify(n, units) {
for (var i= units.length; i-->1;) {
var unit= Math.pow(units[0], i);
if (n>=unit)
return Math.floor(n/unit)+units[i];
}
return n; // no prefix, single units
}
然后,访问一个长度:
var desc= 'File, '+unitify(content.length, UNITS_BINARY)+'B';
desc+= ' or in SI, '+unitify(content.length, UNITS_SI)+'B';
// eg. File, 977KiB or in SI 1MB
我不知道你说的UTF-8是什么意思,但是如果你想知道编码为字节的字符串的长度,你必须自己将该字符串编码为UTF-8。幸运的是,在JavaScript中获得UTF-8编码器有一个便宜的技巧:
var bytes= unescape(encodeURIComponent(chars));
alert(unitify(bytes, BINARY_UNITS)+'B');
像这样的东西会对你有帮助
function getStringBytes(string) {
var bytes = 0;
var i;
for (i = 0; i < string.length; i++) {
var c = fixedCharCodeAt(string, i);
// in accordance with http://en.wikipedia.org/wiki/UTF-8#Description
bytes += c === false ? 0 :
c <= 0x007f ? 1 :
c <= 0x07FF ? 2 :
c <= 0xFFFF ? 3 :
c <= 0x1FFFFF ? 4 :
c <= 0x3FFFFFF ? 5 : 6;
}
return bytes;
}
function fixedCharCodeAt (str, idx) {
// ex. fixedCharCodeAt ('\uD800\uDC00', 0); // 65536
// ex. fixedCharCodeAt ('\uD800\uDC00', 1); // false
idx = idx || 0;
var code = str.charCodeAt(idx);
var hi, low;
if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters)
hi = code;
low = str.charCodeAt(idx + 1);
if (isNaN(low)) {
throw new Error('High surrogate not followed by low surrogate');
}
return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
}
if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate
return false;
}
return code;
}
函数getStringBytes(字符串){
var字节=0;
var i;
对于(i=0;ifunction getStringBytes(string) {
var bytes = 0;
var i;
for (i = 0; i < string.length; i++) {
var c = fixedCharCodeAt(string, i);
// in accordance with http://en.wikipedia.org/wiki/UTF-8#Description
bytes += c === false ? 0 :
c <= 0x007f ? 1 :
c <= 0x07FF ? 2 :
c <= 0xFFFF ? 3 :
c <= 0x1FFFFF ? 4 :
c <= 0x3FFFFFF ? 5 : 6;
}
return bytes;
}
function fixedCharCodeAt (str, idx) {
// ex. fixedCharCodeAt ('\uD800\uDC00', 0); // 65536
// ex. fixedCharCodeAt ('\uD800\uDC00', 1); // false
idx = idx || 0;
var code = str.charCodeAt(idx);
var hi, low;
if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters)
hi = code;
low = str.charCodeAt(idx + 1);
if (isNaN(low)) {
throw new Error('High surrogate not followed by low surrogate');
}
return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
}
if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate
return false;
}
return code;
}
函数getStringBytes(字符串){
var字节=0;
var i;
对于(i=0;i