博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【ReactNative/JS】uint8array转string convert uint8array to string
阅读量:4197 次
发布时间:2019-05-26

本文共 3666 字,大约阅读时间需要 12 分钟。

客户端/服务器使用的protobuffer交互,客户端收到的是uint8array,面临着从unit8array转string,我使用的是下面的Crossplatform method

Working with node.js or other javascript platforms (multioperability), you'll find that some methods decide to return something different than a string. An Uint8Array is a typed array that represents an array of 8-bit unsigned integers.

The following list of methods , provide a collection of efficient ways to convert an Uint8Array to a regular string in javascript.

Little strings

The following method is acceptable for little arrays (>=100k characters), otherwise you'll get exceptions as RangeError : Maximum call stack size exceeded or Out of stack space. However you need to know that this method doesn't play good with UTF-8 characters.

function uint8arrayToStringMethod(myUint8Arr){
return String.fromCharCode.apply(null, myUint8Arr);}

Browser implementation

If your javascript is being executed in a browser, you can make use of the TextEncoder and TextDecoder native functions which allow you to choose which codification you want to use. The Encoding API helps solving the string conversion problem.

/** * Convert an Uint8Array into a string. * * @returns {String} */function Decodeuint8arr(uint8array){
return new TextDecoder("utf-8").decode(uint8array);}/** * Convert a string into a Uint8Array. * * @returns {Uint8Array} */function Encodeuint8arr(myString){
return new TextEncoder("utf-8").encode(myString);}

Crossplatform method

The following method is very useful and works pretty good. It doesn't use any hacks nor depends on Browser JS functions, e.g. works also in other JS environments. Works through the UTF-8 convention.

// http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt/* utf.js - UTF-8 <=> UTF-16 convertion * * Copyright (C) 1999 Masanao Izumo 
* Version: 1.0 * LastModified: Dec 25 1999 * This library is free. You can redistribute it and/or modify it. */function Utf8ArrayToStr(array) {
var out, i, len, c; var char2, char3; out = ""; len = array.length; i = 0; while(i < len) {
c = array[i++]; switch(c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: // 0xxxxxxx out += String.fromCharCode(c); break; case 12: case 13: // 110x xxxx 10xx xxxx char2 = array[i++]; out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); break; case 14: // 1110 xxxx 10xx xxxx 10xx xxxx char2 = array[i++]; char3 = array[i++]; out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); break; } } return out;}

Large blocks

If your data block is huge, you may want to use a method that works properly with huge data (an async method). In this case we'll create blob that contains our Uint8Array and then we'll use the file reader to read it as text.

/** * Converts an array buffer to a string * * @param {Uin8} uint8arr | The buffer to convert * @param {Function} callback | The function to call when conversion is complete */function largeuint8ArrToString(uint8arr, callback) {
var bb = new Blob([uint8arr]); var f = new FileReader(); f.onload = function(e) {
callback(e.target.result); }; f.readAsText(bb);}// Usage example// "Hello" in Uint8Array formatvar myuint8Arr = new Uint8Array([72, 101, 108, 108, 111, 32, 33]);largeuint8ArrToString(myuint8Arr,function(text){
// Hello console.log(text);});

Note: as said before, this method is recommendable only if you handle huge datasets.

Have fun 

原文连接:

转载地址:http://wbyli.baihongyu.com/

你可能感兴趣的文章
高级性能测试(翻译)
查看>>
Web安全测试解决方案
查看>>
今天开始上班
查看>>
开源测试研究方案泡汤了
查看>>
晒一下我培训的课程——应用系统性能测试规划、实施与分析
查看>>
自动化测试框架之控制界面的关键
查看>>
自动化测试框架指南
查看>>
利用 STAF 实现程序更新包的自动部署测试
查看>>
软件安全性测试转载自小龙虾博客
查看>>
周末参加“北京干部管理职业技术学院”关于高职课程改革的专家讨论会
查看>>
软件测试框架介绍
查看>>
软件自动化测试框架的发展
查看>>
nginx反向代理的缓存
查看>>
基于Keepalived+Haproxy+Varnish+LNMP企业级架构
查看>>
实现haproxy+LNMT负载均衡架构
查看>>
常感冒的小朋友的应对
查看>>
centos单机安装Hadoop2.6
查看>>
centos单机安装Spark1.4.0
查看>>
java - 日期相减、四舍五入
查看>>
java - mysql连接
查看>>