Char:修訂版本之間的差異

從 YFRobotwiki
跳到: 導覽搜尋
(以“ {| border="0" cellpadding="20" width="100%" |- |width="100%" valign="top" align="left"| '''char()''' <font color="orange" size="+1">'''描述'''</font> Converts ...”为内容创建页面)
 
第 8 行: 第 8 行:
 
<font color="orange" size="+1">'''描述'''</font>
 
<font color="orange" size="+1">'''描述'''</font>
  
Converts a value to the char data type. --  
+
A data type that takes up 1 byte of memory that stores a character value. Character literals are written in single quotes, like this: 'A' (for multiple characters - strings - use double quotes: "ABC").
  
 +
Characters are stored as numbers however. You can see the specific encoding in the ASCII chart. This means that it is possible to do arithmetic on characters, in which the ASCII value of the character is used (e.g. 'A' + 1 has the value 66, since the ASCII value of the capital letter A is 65). See Serial.println reference for more on how characters are translated to numbers.
  
 +
The char datatype is a signed type, meaning that it encodes numbers from -128 to 127. For an unsigned, one-byte (8 bit) data type, use the byte data type.
  
<font color="orange" size="+1">'''语法'''</font>
+
译:
  
<pre style="color:dimgray">
+
一个数据类型,占用1个字节的内存存储一个字符值。字符都写在单引号,如'A'(多个字符(字符串)使用双引号,如“ABC”)。
  
return;
+
字符以编号的形式存储。你可以在ASCII表中看到对应的编码。这意味着字符的ASCII值可以用来作数学计算。(例如'A'+ 1,因为大写A的ASCII值是65,所以结果为66)。如何将字符转换成数字参考serial.println命令。
  
return value; // both forms are valid
+
char 据类型是有符号的类型,这意味着它的编码为-128到127。对于一个无符号一个字节(8位)的数据类型,使用byte数据类型。
 
+
</pre>
+
 
+
 
+
<font color="orange" size="+1">'''参 '''</font>
+
 
+
value: any variable or constant type
+
  
  
第 33 行: 第 28 行:
  
 
<pre style="color:dimgray">
 
<pre style="color:dimgray">
 
+
  char myChar = 'A';
int checkSensor(){     
+
  char myChar = 65;     // both are equivalent
    if (analogRead(0) > 400) {
+
        return 1;
+
    else{
+
        return 0;
+
    }
+
}
+
 
+
 
</pre>
 
</pre>
  
The return keyword is handy to test a section of code without having to "comment out" large sections of possibly buggy code.
 
  
<pre style="color:dimgray">
+
<font color="orange" size="+1">'''扩展阅读'''</font>
 +
:- [[byte]]
 +
:- [[int]]
 +
:- [[array]]
 +
:- [[Serial.println]]
  
void loop(){
+
|}
  
// brilliant code idea to test here
 
  
return;
 
 
// the rest of a dysfunctional sketch here
 
// this code will never be executed
 
}
 
 
</pre>
 
 
|}
 
  
 
----
 
----

2016年12月26日 (一) 21:06的修訂版本

char()

描述

A data type that takes up 1 byte of memory that stores a character value. Character literals are written in single quotes, like this: 'A' (for multiple characters - strings - use double quotes: "ABC").

Characters are stored as numbers however. You can see the specific encoding in the ASCII chart. This means that it is possible to do arithmetic on characters, in which the ASCII value of the character is used (e.g. 'A' + 1 has the value 66, since the ASCII value of the capital letter A is 65). See Serial.println reference for more on how characters are translated to numbers.

The char datatype is a signed type, meaning that it encodes numbers from -128 to 127. For an unsigned, one-byte (8 bit) data type, use the byte data type.

譯:

一個數據類型,佔用1個位元組的內存存儲一個字符值。字符都寫在單引號,如'A'(多個字符(字符串)使用雙引號,如「ABC」)。

字符以編號的形式存儲。你可以在ASCII表中看到對應的編碼。這意味着字符的ASCII值可以用來作數學計算。(例如'A'+ 1,因為大寫A的ASCII值是65,所以結果為66)。如何將字符轉換成數字參考serial.println命令。

char數據類型是有符號的類型,這意味着它的編碼為-128到127。對於一個無符號一個位元組(8位)的數據類型,使用byte數據類型。


示例

A function to compare a sensor input to a threshold

  char myChar = 'A';
  char myChar = 65;      // both are equivalent


擴展閱讀

- byte
- int
- array
- Serial.println




返回Arduino語法參考列表

更多建議和問題歡迎反饋至 YFRobot論壇