|
|

楼主 |
发表于 2015-3-26 22:20:33
|
显示全部楼层
本帖最后由 bigwolf 于 2015-3-26 22:26 编辑
Arduino内部把汉字都是转成UTF-8。程序把UTF-8转成Unicode,再用字符串格式存储方便串口输出。
测试的输入字符串是 temp = "灵龙123abc"; 输出内码应该是0x (7075 9F99 0031 0032 0033 0061 0062 0063)的字符串。
现在问题是卡在 7075 9F99,后面的输出就没了。
请大家有兴趣的帮我研究研究,谢谢。
——————————————————————————————————————————————
String inputString = "",temp; // a string to hold incoming data
String outputString = "";
boolean stringComplete = false; // whether the string is complete
unsigned char * charpoint; //定义指针,转换String用
char chartemp[100];//定义数组,转换String用
int UTF8ToUnicode (unsigned char *ch, unsigned int *unicode) //这个函数没问题,可以不用看
{
unsigned char *p = NULL;
int e = 0, n = 0;
if((p = ch) && unicode)
{
if(*p >= 0xfc)
{
e = (p[0] & 0x01) << 30;
e |= (p[1] & 0x3f) << 24;
e |= (p[2] & 0x3f) << 18;
e |= (p[3] & 0x3f) << 12;
e |= (p[4] & 0x3f) << 6;
e |= (p[5] & 0x3f);
n = 6;
}
else if(*p >= 0xf8)
{
e = (p[0] & 0x03) << 24;
e |= (p[1] & 0x3f) << 18;
e |= (p[2] & 0x3f) << 12;
e |= (p[3] & 0x3f) << 6;
e |= (p[4] & 0x3f);
n = 5;
}
else if(*p >= 0xf0)
{
e = (p[0] & 0x07) << 18;
e |= (p[1] & 0x3f) << 12;
e |= (p[2] & 0x3f) << 6;
e |= (p[3] & 0x3f);
n = 4;
}
else if(*p >= 0xe0)
{
e = (p[0] & 0x0f) << 12;
e |= (p[1] & 0x3f) << 6;
e |= (p[2] & 0x3f);
n = 3;
}
else if(*p >= 0xc0)
{
e = (p[0] & 0x1f) << 6;
e |= (p[1] & 0x3f);
n = 2;
}
else
{
e = p[0];
n = 1;
}
*unicode = e;
}
return n;
} //函数结束
void setup() {
// initialize serial:
Serial.begin(9600);
int n,i,k,l;
delay(2000);
temp = "灵龙123abc";
unsigned char tempw[2];
unsigned int r_unicode,r1,r2;
n=temp.length();
Serial.println(temp);
temp.toCharArray(chartemp,n+1); //n+1
// UTF-8 to unicode
k = 0;
for(i=0;i<n;i= i+k)
{
k = UTF8ToUnicode (charpoint+i, &r_unicode) ; //UTF-8转成Unicode,返回转换个数,结果双字节存在r_unicode
//下面把结果Unicode的双字节r_unicode 重新转为字符串String格式输出
r1= r_unicode>>8;
tempw[0] = char (r_unicode>>8);
tempw[1] = char (r_unicode);
Serial.print("tempw[0]=");
Serial.print(tempw[0],HEX);Serial.print(" tempw[1]= ");Serial.println(tempw[1],HEX);
String ss3 ;
ss3 = (const char*)tempw; //关键好像卡在这儿,0x00 0x31以及后面的输不出来
outputString = outputString +ss3;
Serial.print(" ss3 =");
Serial.println(ss3);
}
Serial.println("UTF-8 is :");
Serial.println(temp);
Serial.println("Unicode is :"); //想得到的字符串为0x (7075 9F99 0031 0032 0033 0061 0062 0063)
Serial.println(outputString);
inputString.reserve(200);
}
//-------这下面的可以不用看,Arduino例子里面的源码------------------------
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
// if (inChar == '\n')
{
stringComplete = true;
}
}
}
|
|