当前位置:网站首页>获取字符串字节大小
获取字符串字节大小
2022-06-21 05:46:00 【八月的猪猪】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
function getSize(str) {
const bytes = unescape(encodeURIComponent(str)).length; // 获取字节
const bytes1 = new Blob([str]).size; // 获取字节
console.log("字节大小...", bytes, bytes1);
const size = convert("B", bytes);
return `${str} 字符大小:${size}`;
}
function convert(unit, bytes) {
const units = ["B", "KB", "MB", "GB", "TB"];
const i = units.indexOf(unit);
const size = (bytes / pow1024(i)).toFixed(2); // 字节转其他单位
return `${size} ${units[i]}`;
}
// 求次幂
function pow1024(num) {
return Math.pow(1024, num);
}
// 1个字节是8个二进制位(8个二进制位组成1个字节),即1Byte=8bit(八个位的组合,共有256种电路状态)。
// 1个数字=1个英文字母(字符)=1字节(Byte),即一个标准英文字母就是一个字节,它有8个比特(bit),(如A为10D,用00001010来表示,8个二进制位)。1个汉字=2个字节,即一个标准汉字有2个字节,8个bit一个字节,也就是说在计算机里1个汉字要用16个bit来表示。另外中文标点符号占2个字节,英文标点符号(,.?!%&+-*/),占1个字节,中文省略号(……)和括(破折)号(——)各占4个字节。 文件的大小其实就是文件内容实际具有的字节数,它以Byte为衡量单位,只要文件内容和格式不发生变化,文件大小就不会发生变化。
console.log(getSize("aaa"));
console.log(getSize(123));
console.log(getSize("蓝忘机"));
// 参考文档:
// 1. https://segmentfault.com/q/1010000007847667
// 2. https://blog.csdn.net/qq_23994787/article/details/86609659
// 3. https://aixiaodou.blog.csdn.net/article/details/108885082?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-108885082-blog-106854329.pc_relevant_downloadblacklistv1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-108885082-blog-106854329.pc_relevant_downloadblacklistv1&utm_relevant_index=1
</script>
</body>
</html>
边栏推荐
- How to remove the prohibition of copying and pasting set by teachers
- Privacy sandbox helps enterprises: how privacy technology protects user data and promotes business growth
- You can have a three piece set without winter. I don't allow you to have PPT, word and excel
- PhpMyAdmin (MySQL) common shell writing methods
- Latex adds a strikeout (horizontal line) to the entire row of the table
- 基于CNN的音乐流派分类
- Error 1030 (HY000): got error 168 from storage engine
- SSH copy ID batch password free script
- [introduction to practice] CRM project practice tutorial -- CRM project based on SSM framework
- The writing style of the construction and interpretation of the mini LISP interpreter
猜你喜欢

Vscode+platformio development STM32 (VII)

科创人·数智未来私董会第4期:转型的实证-幸存者偏差与盲人摸象

js 封装函数,多次调用结果追加

UE4/5 碰撞器 On Begin overlap与 On End Overlap 同时触发解决

Redis cache penetration, cache breakdown, cache avalanche

VMware new oraclelinux6.5 virtual machine

In the NLP field, what are the most practical and effective skills or ideas used in text classification, NER, QA, generation, relationship extraction, etc?

Matlab GUI编程技巧(十九)【实战二】:超强科学计算器的设计,功能齐全

File contains vulnerability - allow_ url_ Fopen and allow_ url_ Include details

Blog forum system based on ssm+mysql+layui
随机推荐
Program optimization with multi-core and multi thread
Oracle笔记 之 表空间使用情况查询
mac os MAMP 安装redis 报错问题 ./common.h:12:10: fatal error: ‘zend_smart_str.h‘ file not found
SQL deadlock
Global and Chinese markets for dual input K-type thermometers 2022-2028: Research Report on technology, participants, trends, market size and share
Randomly create circular, triangular or rectangular objects, store them in the array, and calculate the area and perimeter of each shape
SSH copy ID batch password free script
Mac NAMP Pro comes with MySQL 5.7 setup SQL_ Model remove only_ FULL_ GROUP_ Invalid by may be caused by:,
js 封装函数,多次调用结果追加
armcm3权威指南笔记----arm编程中地址未对齐方面的影响
Usage of async/await in JS
科创人·数智未来私董会第4期:转型的实证-幸存者偏差与盲人摸象
LaTeX给表格的一整行加删除线(横线)
Music genre classification based on CNN
Emotron伊爾通軟啟動器維修MSF370/MSF450
Record of error prone problems in PHP signature
Network security - the difference between DDoS and CC attacks
Principle and implementation of instanceof
Error 1030 (HY000): got error 168 from storage engine
[open source tutorial] DIY tutorial of step-by-step electric adjustment [Author: I love Laurie, Laurie]