Arduino UCS2为UTF文本格式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino UCS2为UTF文本格式相关的知识,希望对你有一定的参考价值。
我在UCS2中输出了我的设备。这个UCS2是十六进制的,我用ucs2 hex utf8 hex。,这些hexes是String形式。问题是,如何从这个hexes普通文本形式?我试着做点什么,一个人给我发了一个代码片来将String
转换成byte
,但是,我还有一个问题。我发现,当你使用byte f = {0x56};
然后将其打印到序列中时,它会写"V"
,但问题是,字节中的utf8十六进制只是"56"
...而且我不知道如何到达那里"0x"
。 .. 谢谢你的帮助 :)
我的代码:
byte charToVal(char c)
{
switch (c)
{
case 'a'...'f':
return (byte)(c - 'a' + 10);
case 'A'...'F':
return (byte)(c - 'A' + 10);
case '0'...'9':
return (byte)(c - '0');
}
}
String toUTF8(String ucs2in)
{
String utf8;
for(int i; i < (ucs2in.length()/4);i++)
{
String endstr[4],startstr[4], ucs2[4];
boolean ok = false;
ucs2[0] = ucs2in.substring(i*4,i*4+1);
ucs2[1] = ucs2in.substring(i*4+1,i*4+2);
ucs2[2] = ucs2in.substring(i*4+2,i*4+3);
ucs2[3] = ucs2in.substring(i*4+3,i*4+4);
if(ucs2[1] == "4") endstr[0] = "d";
else endstr[0] = "c";
endstr[3] = ucs2[3];
if(ucs2 == NULL) break;
if(ucs2[2].toInt() < 8) if(ucs2[2].toInt() > 0 || ucs2[2] == "0") ok=true;
if(ucs2[1] == "0" && ok)
{
endstr[0]="";
endstr[1]="";
endstr[2] = ucs2[2];
endstr[3] = ucs2[3];
}
else
{
if(ucs2[2] == "0" || ucs2[2] == "1" || ucs2[2] == "2" || ucs2[2] == "3")
{
if(ucs2[2] == "0") endstr[2] = "8";
else if(ucs2[2] == "1") endstr[2] = "9";
else if(ucs2[2] == "2") endstr[2] = "a";
else if(ucs2[2] == "3") endstr[2] = "b";
if(ucs2[1] == "1") endstr[1] = "4";
else if(ucs2[1] == "2") endstr[1] = "8";
else if(ucs2[1] == "3") endstr[1] = "c";
}
else if(ucs2[2] == "4" || ucs2[2] == "5" || ucs2[2] == "6" || ucs2[2] == "7")
{
if(ucs2[2] == "4") endstr[2] = "8";
else if(ucs2[2] == "5") endstr[2] = "9";
else if(ucs2[2] == "6") endstr[2] = "a";
else if(ucs2[2] == "7") endstr[2] = "b";
if(ucs2[1] == "1") endstr[1] = "5";
else if(ucs2[1] == "2") endstr[1] = "9";
else if(ucs2[1] == "3") endstr[1] = "d";
}
else if(ucs2[2] == "8" || ucs2[2] == "9" || ucs2[2] == "a" || ucs2[2] == "b")
{
if(ucs2[2] == "8") endstr[2] = "8";
else if(ucs2[2] == "9") endstr[2] = "9";
else if(ucs2[2] == "a") endstr[2] = "a";
else if(ucs2[2] == "b") endstr[2] = "b";
else if(ucs2[1] == "1") endstr[1] = "6";
else if(ucs2[1] == "2") endstr[1] = "a";
else if(ucs2[1] == "3") endstr[1] = "e";
}
else if(ucs2[2] == "C" || ucs2[2] == "D" || ucs2[2] == "E" || ucs2[2] == "F")
{
if(ucs2[2] == "C") endstr[2] = "8";
else if(ucs2[2] == "D") endstr[2] = "9";
else if(ucs2[2] == "E") endstr[2] = "a";
else if(ucs2[2] == "F") endstr[2] = "b";
if(ucs2[1] == "0") endstr[1] = "3";
else if(ucs2[1] == "1") endstr[1] = "7";
else if(ucs2[1] == "2") endstr[1] = "b";
else if(ucs2[1] == "3") endstr[1] = "f";
}
}
String finalstr = endstr[0] + endstr[1] + endstr[2] + endstr[3];
char finalchar, val, hex[10];
finalstr.toCharArray(hex,10);
boolean onlyone=false;
if(finalstr.length() <= 2) onlyone = true;
if(!onlyone)
{
finalchar = (charToVal(hex[0]) << 4) + charToVal(hex[1]);
utf8 += String(finalchar);
}
finalchar = (charToVal(hex[2]) << 4) + charToVal(hex[3]);
utf8 += String(finalchar);
}
return utf8;
}
void setup()
{
Serial.begin(9600);
Serial.println(toUTF8("005600E1017e0065006e00Fd0020007a00E1006b0061007a006e00Ed006b002c00200063006800630065006d00650020005600E1006d0020006f007a006e00E1006d00690165002c0020017e0065002000560061016100610020007a00E1007300690065006c006b00610020006a006500200070007200690070007200610076006e00E10020006e0061002000760079007a0064007600690068006e0075007400690065002000210020007700770077002e0061006c007a0061002e0073006b"));
}
void loop()
{
}
输出是⸮áž⸮⸮ý⸮⸮á⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮á⸮⸮⸮⸮⸮á⸮⸮ť⸮⸮⸮⸮ť⸮⸮⸮⸮⸮⸮⸮⸮ť⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮ ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮一个⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
我想亲爱的顾客,我们想通知你....我无法解决这个问题。谢谢。
答案
我修好了......有问题......
if(finalstr.length() == 4) onlyone = true;
if(onlyone)
{
finalchar = (charToVal(hex[0]) << 4) + charToVal(hex[1]);
utf8 += String(finalchar);
finalchar = (charToVal(hex[2]) << 4) + charToVal(hex[3]);
utf8 += String(finalchar);
}
else
{
finalchar = (charToVal(hex[0]) << 4) + charToVal(hex[1]);
utf8 += String(finalchar);
}
以上是关于Arduino UCS2为UTF文本格式的主要内容,如果未能解决你的问题,请参考以下文章
在VB中,如果检测到某文本文件为非UTF8格式则自动转换为UTF8格式?