Arduino char数组打印不同的值

时间:2013-06-19 21:03:06

标签: char arduino

这个问题占用了我一整天,我无法理解为什么会这样做。我的代码:

char products[11][14]= {   {'6','3','4','4','8','2','7','0','1','9','2','~','~'}, 
                           {'5','0','6','0','1','8','5','1','6','2','3','7','0'},
                           {'0','1','2','3','4','5','6','7','8','9','0','1','2'},
                           {'6','7','1','8','6','0','0','1','2','3','2','0','~'},
                           {'1','8','8','3','3','4','0','0','1','4','~','~','~'},
                           {'7','0','5','1','0','5','2','2','1','5','1','9','~'},
                           {'9','7','8','8','4','2','0','5','3','2','3','1','8'},
                           {'1','2','5','0','0','2','7','4','1','3','5','~','~'},
                           {'8','1','1','2','0','4','0','1','2','3','4','4','~'},
                           {'5','9','4','4','5','2','5','0','0','2','3','6','9'},
                           {'9','7','8','9','7','3','1','0','3','0','4','1','8'}
                          };
void setup(){
   Serial.begin(9600);
   for(int i=0; i<11; i++){
      for(int j=0; j<14; j++)
        Serial.print(products[i][j], DEC);
      Serial.println("\n");
   }
}

void loop(){}

当我运行它时,不打印我存储的实际值,而是打印出来:

54515252565055484957501261260

534854484956534954505155480

484950515253545556574849500

5455495654484849505150481260

495656515152484849521261261260

5548534948535050495349571260

575556565250485351505149560

49505348485055524951531261260

5649495048524849505152521260

535752525350534848505154570

575556575551494851485249560

起初我认为它是每个的ASCII码,但我的值应该在30到39之间。这是什么以及如何获得实际值?

2 个答案:

答案 0 :(得分:3)

您正在打印出数组中字符整数值的十进制表示。

Serial.Print('6', DEC) // will print out '54'

将打印出54,因为字符“6”的整数值为54.您可以在asciitable

上查看

如果你想打印出自己的字符,你应该省略DEC并使用Print而不用第二个参数:

Serial.Print('6') // will print out '6'

the API

了解有关Print()选项的详情

答案 1 :(得分:2)

这很简单。 根据{{​​3}},值是正确的。

如果要打印字符,请从打印中删除DEC

char products[11][14]= {   {'6','3','4','4','8','2','7','0','1','9','2','~','~'}, 
                           {'5','0','6','0','1','8','5','1','6','2','3','7','0'},
                           {'0','1','2','3','4','5','6','7','8','9','0','1','2'},
                           {'6','7','1','8','6','0','0','1','2','3','2','0','~'},
                           {'1','8','8','3','3','4','0','0','1','4','~','~','~'},
                           {'7','0','5','1','0','5','2','2','1','5','1','9','~'},
                           {'9','7','8','8','4','2','0','5','3','2','3','1','8'},
                           {'1','2','5','0','0','2','7','4','1','3','5','~','~'},
                           {'8','1','1','2','0','4','0','1','2','3','4','4','~'},
                           {'5','9','4','4','5','2','5','0','0','2','3','6','9'},
                           {'9','7','8','9','7','3','1','0','3','0','4','1','8'}
                          };
void setup(){
   Serial.begin(9600);
   for(int i=0; i<11; i++){
      for(int j=0; j<14; j++){
        Serial.print(products[i][j]);
        Serial.print(" ");
      }
      Serial.print("\n");
   }
}

void loop(){}

例如

Serial.print(78) gives "78"
Serial.print(1.23456) gives "1.23"
Serial.print('N') gives "N"
Serial.print("Hello world.") gives "Hello world."
Serial.print(78, BIN) gives "1001110"
Serial.print(78, OCT) gives "116"
Serial.print(78, DEC) gives "78"
Serial.print(78, HEX) gives "4E"
Serial.println(1.23456, 0) gives "1"
Serial.println(1.23456, 2) gives "1.23"
Serial.println(1.23456, 4) gives "1.2346"

了解更多信息http://www.asciitable.com/