请教一个char 转String的问题
char类型的tempw =0x70(内码);
char类型的 tempw =0x75(内码);
想要得到字符串String型 0x7075(内码)请问如何怎么操作?
outputString =tempw +temp;//int 转换成String
用上面的语句outputString结果为112.....不是想要的 本帖最后由 Super169 于 2015-3-26 01:09 编辑
把 tempw 設定為 char tempw;
再加上 tempw = 0;
直接把 tempw 當成 string 用就可以了.
補個程式給你吧.
void setup() {
char tempw;
tempw = 0x70;
tempw = 0x75;
tempw = 0;
Serial.begin(9600);
Serial.println(tempw);
}
谢谢,不过好像还是不好用。 本帖最后由 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;//定义数组,转换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 & 0x01) << 30;
e |= (p & 0x3f) << 24;
e |= (p & 0x3f) << 18;
e |= (p & 0x3f) << 12;
e |= (p & 0x3f) << 6;
e |= (p & 0x3f);
n = 6;
}
else if(*p >= 0xf8)
{
e = (p & 0x03) << 24;
e |= (p & 0x3f) << 18;
e |= (p & 0x3f) << 12;
e |= (p & 0x3f) << 6;
e |= (p & 0x3f);
n = 5;
}
else if(*p >= 0xf0)
{
e = (p & 0x07) << 18;
e |= (p & 0x3f) << 12;
e |= (p & 0x3f) << 6;
e |= (p & 0x3f);
n = 4;
}
else if(*p >= 0xe0)
{
e = (p & 0x0f) << 12;
e |= (p & 0x3f) << 6;
e |= (p & 0x3f);
n = 3;
}
else if(*p >= 0xc0)
{
e = (p & 0x1f) << 6;
e |= (p & 0x3f);
n = 2;
}
else
{
e = p;
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;
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 = char (r_unicode>>8);
tempw = char (r_unicode);
Serial.print("tempw=");
Serial.print(tempw,HEX);Serial.print(" tempw= ");Serial.println(tempw,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;
}
}
}
页:
[1]