极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10281|回复: 3

请教一个char 转String的问题

[复制链接]
发表于 2015-3-25 21:23:21 | 显示全部楼层 |阅读模式
      
char类型的  tempw[0] =0x70(内码);
char类型的 tempw[1] =0x75(内码);

      
想要得到字符串String型 0x7075(内码)请问如何怎么操作?

outputString =  tempw[0] +temp[1];  //int 转换成String

用上面的语句outputString结果为112.....不是想要的
回复

使用道具 举报

发表于 2015-3-26 01:01:01 | 显示全部楼层
本帖最后由 Super169 于 2015-3-26 01:09 编辑

把 tempw 設定為 char tempw[3];
再加上 tempw[2] = 0;
直接把 tempw 當成 string 用就可以了.

補個程式給你吧.
  1. void setup() {
  2.   char tempw[3];
  3.   tempw[0] = 0x70;
  4.   tempw[1] = 0x75;
  5.   tempw[2] = 0;
  6.   Serial.begin(9600);
  7.   Serial.println(tempw);
  8. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-26 22:06:53 | 显示全部楼层
谢谢,不过好像还是不好用。
回复 支持 反对

使用道具 举报

 楼主| 发表于 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;
    }
  }
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-8 12:58 , Processed in 0.038792 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表