0

这个问题已经占用了我一整天,我不知道为什么会这样。我的代码:

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 之间。那是什么以及如何获得实际值?

4

2 回答 2

3

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

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

会打印出来54,因为字符 '6' 的整数值是 54。你可以在asciitable上查看

如果你想打印出字符本身,你应该省略DEC并使用Print没有第二个参数:

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

从API了解更多关于Print()的选项

于 2013-06-19T21:15:09.100 回答
2

这真的很简单。根据http://www.asciitable.com/ ,这些值是正确的。

如果要打印字符,请从打印中删除 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"

有关更多信息,请参阅此链接

于 2013-06-19T21:15:01.807 回答