在蓝牙BLE传输过程中,BLE设备只能读取GB2312编码的汉字字符或者ASCII码的字符,我现在想知道Typescript方式编译的小程序中当使用input控件接受到用户输入的汉字时是哪一种编码方式?unicode或是utf-8?还有当直接写在ts文件中的汉字是哪一种编码方式?大概率是unicode吧?现在的问题是,有没有一个接口函数能快速将unicode编码的字符串转换为GB2312编码的number[]再转换为ArrayBuffer发送给BLE设备?
只能用户自己手写一份转换方法?
以下是我刚写的,是否可用?
/**将unicode字符映射为GB2312字符*/
static mapToGB2312Char(unicodechar: number) : number{
for(var i = 0;i < uincodeAndGb2312.length;i+=2){
if(uincodeAndGb2312[i] == unicodechar)
return uincodeAndGb2312[i+1]
}
return 0
}
/**将GB2312字符映射为unicode字符*/
static mapToUnicodeChar(gb2312char: number) : number{
for(var i = 0;i < uincodeAndGb2312.length;i+=2){
if(uincodeAndGb2312[i + 1] == gb2312char)
return uincodeAndGb2312[i]
}
return 0
}
/**unicode字符映射为对应的GB2312字符*/
static getGb2312Chars(unicodechars: number[]) : number[]{
let bts : number[] = []
unicodechars.forEach(c => {
var tc = this.mapToGB2312Char(c)
if(tc != 0) bts.push(tc)//返回0是映射失败
})
return bts
}
/**从字符串获取unicode字符集*/
static getUnicodeChars(unicodestr : string): number[]{
if(this.isUtf8String(unicodestr)) return this.getUnicodeCharsFromUtf8(unicodestr)
let bts : number[] = []
let nTotalChars = unicodestr.length// total chars to be processed.
var nOffset = 0; // processing point on strUtf8
while (nOffset < nTotalChars){
bts.push(unicodestr.charCodeAt(nOffset))
nOffset++
}
return bts
}
/**识别是否是utf8格式字符串*/
static isUtf8String(str: string): boolean {
var fc = str.length > 0 ? str.charCodeAt(0) : 0
return (fc & 0x80) == 0 || (fc & 0xE0) == 0xC0 || (fc & 0xF0) == 0xE0 || (fc & 0xF8) == 0xF0
}
/**从utf-8中获取unicode字符集*/
static getUnicodeCharsFromUtf8(strUtf8: string): number[]{
let bts : number[] = []
var nTotalChars = strUtf8.length// total chars to be processed.
var nOffset = 0; // processing point on strUtf8
var nRemainingBytes = nTotalChars// how many bytes left to be converted
var iCode, iCode1, iCode2, iCode3// the value of the unicode.
while (nOffset < nTotalChars){
iCode = strUtf8.charCodeAt(nOffset)
if ((iCode & 0x80) == 0){ // 1 byte.
if (nRemainingBytes < 1)break// not enough data
bts.push(iCode & 0x7F)
nOffset++
nRemainingBytes -= 1
}
else if ((iCode & 0xE0) == 0xC0){ // 2 bytes
iCode1 = strUtf8.charCodeAt(nOffset + 1)
// not enough data
if (nRemainingBytes < 2 || (iCode1 & 0xC0) != 0x80) break// invalid pattern
bts.push(((iCode & 0x3F) << 6) | ( iCode1 & 0x3F))
nOffset += 2
nRemainingBytes -= 2
}
else if ((iCode & 0xF0) == 0xE0){ // 3 bytes
iCode1 = strUtf8.charCodeAt(nOffset + 1);
iCode2 = strUtf8.charCodeAt(nOffset + 2);
// not enough data
if (nRemainingBytes < 3 || (iCode1 & 0xC0) != 0x80 || (iCode2 & 0xC0) != 0x80)break// invalid pattern
bts.push(((iCode & 0x0F) << 12) | ((iCode1 & 0x3F) << 6) | (iCode2 & 0x3F))
nOffset += 3
nRemainingBytes -= 3
}
else if ((iCode & 0xF8) == 0xF0){ // 4 bytes
iCode1 = strUtf8.charCodeAt(nOffset + 1);
iCode2 = strUtf8.charCodeAt(nOffset + 2);
iCode3 = strUtf8.charCodeAt(nOffset + 3);
// not enough data
if (nRemainingBytes < 4 || (iCode1 & 0xC0) != 0x80 || (iCode2 & 0xC0) != 0x80 || (iCode3 & 0xC0) != 0x80)break// invalid pattern
bts.push(((iCode & 0x0F) << 18) | ((iCode1 & 0x3F) << 12) | ((iCode2 & 0x3F) << 6) | (iCode3 & 0x3F))
nOffset += 4
nRemainingBytes -= 4
}
else break// 5 or more bytes -- unsupported
}
return bts
}
参考一下请问下当前版本下string怎么转ArrayBuffer?
https://developers.weixin.qq.com/community/develop/doc/00068888b1c0e8d199bdb801556400