JS实现unicode和UTF-8之间的互相转换互转
有一设备,为短信网关。需将PC送过来的UTF-8转换成UNICODE才能将内容通过短信发送出去,同样,接收到的短信为unicode编码,也许转换成UTF-8才能在PC端软件显示出来。程序很简单,只是走了不少弯路:
//unicode为1个接收数据,串口收到的字符编码放在该数组中
functionUnicodeToUtf8(unicode){
varuchar;
varutf8str="";
vari;
for(i=0;i>8)&0xff;
temp[j++]=uCode&0xff;
}elseif(uCode<0x1000000){
temp[j++]=(uCode>>16)&0xff;
temp[j++]=(uCode>>8)&0xff;
temp[j++]=uCode&0xff;
}elseif(uCode<0x100000000){
temp[j++]=(uCode>>24)&0xff;
temp[j++]=(uCode>>16)&0xff;
temp[j++]=(uCode>>8)&0xff;
temp[j++]=uCode&0xff;
}else{
break;
}
}
temp.length=j;
returntemp;
}
以上所述是小编给大家介绍的JS实现unicode和UTF-8之间的互相转换互转,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!