实现文本编解码功能鸿蒙示例代码
·
本文原创发布在华为开发者社区,更多鸿蒙场景化示例请见华为开发者联盟官网“行业实践与常见问题”专题页。
介绍
本示例通过util模块下的textDecoder和textEncoder对文本进行编解码,统一自带toString编码方式及TestEncoder支持的编码方式。
效果预览

使用说明
点击对应模式按钮即可展示结果。
实现思路
- 支持方式汇总。
static BUFFER_ENCODING: string[] =
['ascii', 'utf8', 'utf-8', 'utf16le', 'ucs2', 'ucs-2', 'base64', 'base64url', 'latin1', 'binary', 'hex'];
static TEXT_ENCODING: string[] =
['utf-8ibm866', 'iso-8859-2', 'iso-8859-3', 'iso-8859-4', 'iso-8859-5', 'iso-8859-6', 'iso-8859-7', 'iso-8859-8',
'iso-8859-8-i', 'iso-8859-10', 'iso-8859-13', 'iso-8859-14', 'iso-8859-15', 'koi8-r', 'koi8-u', 'macintosh',
'windows-874', 'windows-1250', 'windows-1251', 'windows-1252', 'windows-1253', 'windows-1254', 'windows-1255',
'windows-1256', 'windows-1257', 'windows-1258', 'x-mac-cyrillic', 'gbk', 'gb18030', 'big5', 'euc-jp',
'iso-2022-jp', 'shift_jis', 'euc-kr', 'utf-16be', 'utf-16le', 'UTF-8', 'GBK', 'GB2312', 'gb2312', 'GB18030',
'iso-8859-1']
- decodeBufferToString()与encodeToBuffer()分别调用textDecoder和textEncoder对文本进行编解码。
// 解码
public static decodeBufferToString(buf: buffer.Buffer, encoding: string): string {
let lowerCaseEncoding = encoding.toLowerCase();
if (BufferUtil.BUFFER_ENCODING.indexOf(lowerCaseEncoding) != -1) {
return buf.toString(lowerCaseEncoding)
}
if (BufferUtil.TEXT_ENCODING.indexOf(encoding) === -1) {
throw new Error(`Currently, encoding ${encoding} is not supported`);
}
return BufferUtil.decodeBufferToStringByTextDecoder(buf, encoding)
}
//编码
public static encodeToBuffer(str: string, encoding: string): buffer.Buffer {
let lowerCaseEncoding = encoding.toLowerCase();
if (BufferUtil.BUFFER_ENCODING.indexOf(lowerCaseEncoding) != -1) {
return buffer.from(str, lowerCaseEncoding as buffer.BufferEncoding);
}
if (BufferUtil.TEXT_ENCODING.indexOf(encoding) === -1) {
throw new Error(`Currently, encoding ${encoding} is not supported`);
}
return BufferUtil.encodeToBufferByTextEncoder(str, encoding)
}
//使用TextDecoder解码
private static decodeBufferToStringByTextDecoder(buf: buffer.Buffer, encoding: string): string {
let textDecoderOptions: util.TextDecoderOptions = {
fatal: false,
ignoreBOM: true
}
let textDecoder = util.TextDecoder.create(encoding, textDecoderOptions);
return textDecoder.decodeToString(new Uint8Array(buf.buffer));
}
//使用TextEncoder编码
private static encodeToBufferByTextEncoder(str: string, encoding: string): buffer.Buffer {
let textEncoder = new util.TextEncoder(encoding);
let result = textEncoder.encodeInto(str);
return buffer.from(result);
}
- 演示页面,对输入文本编解码。
TextInput({ text: this.inputText, placeholder: '请输入文本' })
.onChange((value: string) => {
this.inputText = value
})
.margin({top:20})
Column(){
Button('Encode to Buffer')
.onClick(()=>{
this.outputBuffer=BufferUtil.encodeToBuffer(this.inputText,'GB18030')
this.outputText='Encode Buffer:'+this.outputBuffer
})
.margin({top:20})
Button('Decode Buffer to String')
.onClick(()=>{
if(this.outputBuffer){
this.outputText=BufferUtil.decodeBufferToString(this.outputBuffer,'GB18030')
}else{
this.outputText='No buffer to decode'
}
})
.margin({top:10})
}
.margin({top:20})
Text(this.outputText)
.fontSize(16)
.margin({top:30})
}
更多推荐
所有评论(0)