js byte[] 和string 相互转换 UTF-8

本文介绍了两种JavaScript函数:stringToByte和byteToString,用于将字符串转换为字节数组及反之。这两种方法支持多种Unicode编码,适用于处理复杂的字符集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 function stringToByte(str) {
			var bytes = new Array();
			var len, c;
			len = str.length;
			for(var i = 0; i < len; i++) {
				c = str.charCodeAt(i);
				if(c >= 0x010000 && c <= 0x10FFFF) {
					bytes.push(((c >> 18) & 0x07) | 0xF0);
					bytes.push(((c >> 12) & 0x3F) | 0x80);
					bytes.push(((c >> 6) & 0x3F) | 0x80);
					bytes.push((c & 0x3F) | 0x80);
				} else if(c >= 0x000800 && c <= 0x00FFFF) {
					bytes.push(((c >> 12) & 0x0F) | 0xE0);
					bytes.push(((c >> 6) & 0x3F) | 0x80);
					bytes.push((c & 0x3F) | 0x80);
				} else if(c >= 0x000080 && c <= 0x0007FF) {
					bytes.push(((c >> 6) & 0x1F) | 0xC0);
					bytes.push((c & 0x3F) | 0x80);
				} else {
					bytes.push(c & 0xFF);
				}
			}
			return bytes;


		}


		 function byteToString(arr) {
			if(typeof arr === 'string') {
				return arr;
			}
			var str = '',
				_arr = arr;
			for(var i = 0; i < _arr.length; i++) {
				var one = _arr[i].toString(2),
					v = one.match(/^1+?(?=0)/);
				if(v && one.length == 8) {
					var bytesLength = v[0].length;
					var store = _arr[i].toString(2).slice(7 - bytesLength);
					for(var st = 1; st < bytesLength; st++) {
						store += _arr[st + i].toString(2).slice(2);
					}
					str += String.fromCharCode(parseInt(store, 2));
					i += bytesLength - 1;
				} else {
					str += String.fromCharCode(_arr[i]);
				}
			}
			return str;
		}

JavaScript 中,UTF-8 编码的字符串通常以字节序列的形式存在,例如从网络请求中接收到的数据。由于 JavaScript 的字符串内部使用 UTF-16 编码,因此需要手动实现 UTF-8 解码逻辑来将其转换为原始字符。 以下是一个基于 UTF-8 编码规则(如引用[2]所述)实现的 UTF-8 解码函数: ```javascript function utf8Decode(bytes) { let result = ''; for (let i = 0; i < bytes.length; i++) { const byte = bytes[i]; if (byte < 0x80) { // 1-byte 字符: 0xxxxxxx result += String.fromCharCode(byte); } else if ((byte & 0xE0) === 0xC0) { // 2-byte 字符: 110xxxxx 10xxxxxx const charCode = ((byte & 0x1F) << 6) | (bytes[++i] & 0x3F); result += String.fromCharCode(charCode); } else if ((byte & 0xF0) === 0xE0) { // 3-byte 字符: 1110xxxx 10xxxxxx 10xxxxxx const charCode = ((byte & 0x0F) << 12) | ((bytes[++i] & 0x3F) << 6) | (bytes[++i] & 0x3F); result += String.fromCharCode(charCode); } else if ((byte & 0xF8) === 0xF0) { // 4-byte 字符: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx const charCode = ((byte & 0x07) << 18) | ((bytes[++i] & 0x3F) << 12) | ((bytes[++i] & 0x3F) << 6) | (bytes[++i] & 0x3F); result += String.fromCharCode(0xD800 + (charCode >> 10)); result += String.fromCharCode(0xDC00 + (charCode & 0x3FF)); } else { // 非法字节 result += String.fromCharCode(0xFFFD); // 替换字符 } } return result; } ``` 此外,也可以利用 `TextDecoder` API 来完成 UTF-8 解码,这是现代浏览器提供的标准方法: ```javascript function decodeUtf8Array(data) { const decoder = new TextDecoder('utf-8'); return decoder.decode(data); } // 示例:将 Uint8Array 数据解码为字符串 const utf8Bytes = new Uint8Array([0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD]); // "你好" const decodedString = decodeUtf8Array(utf8Bytes); console.log(decodedString); // 输出 "你好" ``` 上述代码片段展示了如何将 UTF-8 编码的字节数组还原成 JavaScript 字符串,适用于处理非 ASCII 文本的情况[^2]。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值