Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 9731|回復: 8
打印 上一主題 下一主題

請問如何將 char 轉為 byte?

[複製鏈接]
跳轉到指定樓層
1#
發表於 2013-4-21 18:47:29 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
請問如何將
char c[] = "1001";轉為
B1001
2#
發表於 2013-4-23 23:44:34 | 只看該作者
您最近問的好幾個問題都是在格式的轉換,不過您似乎把資料類型給弄混了。
char c[] = "1001"; 您使用雙引號代表c 是一組字串,而字串是不能轉成BIN二進位、DEC十進位或HEX十六進位,單一字元char才可以。

而char和byte差異只有儲存的數值範圍不同而已(char:-128~127,byte:0~255),另外B1001也不是byte,B開頭的代表是BIN二進位,若要將char轉成byte其實是會有問題的,如下:
  1. char a1=100;
  2. char a2=-100;
  3. byte a3=a1;  //Convert char a1 to byte a3
  4. byte a4=a2;  //Convert char a2 to byte a4
  5. Serial.println(a3);  //print 100
  6. Serial.println(a4);  //print 156 (byte max value 255-100+1)
複製代碼
3#
 樓主| 發表於 2013-4-24 00:33:17 | 只看該作者
本帖最後由 pizg 於 2013-4-25 00:19 編輯

感謝babyfish0226老師的講解.

我只要把char c[] = "1001" 轉為 9,
它其實就是二進位的1001了.

而B1001只是一種二進位的表示法而已,
的確是沒有必要PRINT這樣的東西出來.

PS. 2013-04-25 這也是一種表示法--> unsigned long speedOfLight = 186000UL;

另外, 我在此提供一點個人的心得:
char c[] = "1001"; 它是一組字串,
String s = "1001"; 它也是一組字串,
若使用strcmp去比較它們是否相等, 會產生錯誤, 如下式:
if(strcmp(c, s) == 0) ...

此時可以用
if(c == s) ....
4#
發表於 2013-4-24 00:47:45 | 只看該作者
String 是 C++ Class 喔
不能用 strcmp(), strcpy(), strcat() ... 這些 C functions 來處理
所以這樣做是錯的:

if(strcmp(c, s) == 0) ...

之所以可以這樣用:

if(c == s) ....

是因為 == 是 String 的 operator, operator == 其實還是呼叫 equals() method 來做事的
除非很清楚正在操作的字串是 C 字串或是 C++ 字串
不然的話,建議只用一種會比較安全
5#
 樓主| 發表於 2013-4-24 12:46:06 | 只看該作者
本帖最後由 pizg 於 2013-4-25 00:40 編輯

感謝cooper老師的說明.
我剛剛才發現...Arduino資料夾裡竟然有很多寶藏,
特貼上跟大家分享,
誠摯盼望跟我一樣的初學者能先去看看,
才不會像我一樣在這兒東問西問地麻煩大家.

底下是其中之一:


String轉int
  1. /*
  2.   String to Integer conversion

  3. Reads a serial input string until it sees a newline, then converts
  4. the string to a number if the characters are digits.

  5. The circuit:
  6. No external components needed.

  7. created 29 Nov 2010
  8. by Tom Igoe

  9. This example code is in the public domain.
  10. */

  11. String inString = "";    // string to hold input

  12. void setup() {
  13.   // Open serial communications and wait for port to open:
  14.   Serial.begin(9600);
  15.   while (!Serial) {
  16.     ; // wait for serial port to connect. Needed for Leonardo only
  17.   }

  18.   // send an intro:
  19.   Serial.println("\n\nString toInt():");
  20.   Serial.println();
  21. }

  22. void loop() {
  23.   // Read serial input:
  24.   while (Serial.available() > 0) {
  25.     int inChar = Serial.read();
  26.     if (isDigit(inChar)) {
  27.       // convert the incoming byte to a char
  28.       // and add it to the string:
  29.       inString += (char)inChar;
  30.     }
  31.     // if you get a newline, print the string,
  32.     // then the string's value:
  33.     if (inChar == '\n') {
  34.       Serial.print("Value:");
  35.       Serial.println(inString.toInt());
  36.       Serial.print("String: ");
  37.       Serial.println(inString);
  38.       // clear the string for new input:
  39.       inString = "";
  40.     }
  41.   }
  42. }
複製代碼
6#
 樓主| 發表於 2013-4-26 10:17:26 | 只看該作者
String 是 C++ Class 喔
不能用 strcmp(), strcpy(), strcat() ... 這些 C functions 來處理
所以這樣做是 ...
coopermaa 發表於 2013-4-24 00:47


cooper老師:
除了 '==' , 我又發現了幾個可以比較String的方法:

1.equals
2.equalsIngoreCase
3.compareTo

甚至以下也可以

1.startsWith
2.endsWith

http://arduino.cc/en/Tutorial/StringComparisonOperators
7#
發表於 2013-4-26 14:34:42 | 只看該作者
cooper老師:
除了 '==' , 我又發現了幾個可以比較String的方法:

1.equals
2.equalsIngoreCase
3. ...
pizg 發表於 2013-4-26 10:17


haha~

還有一條路可以走,就是直接讀 source code,看一下這個資料夾:

C:\arduino-1.X.X\hardware\arduino\cores\arduino

裏面的 WString.h 和 WString.cpp 就是 String class 的 source code

對所有人來說,這條路不一定是捷徑,但卻是「想知其所以然」的唯一的一條路

讀一下,找看看 String class 把字元存在哪裏
8#
發表於 2013-4-26 14:36:49 | 只看該作者
對了,Arduino 的 source code 放在 github 上,所以也可以線上看:

https://github.com/arduino/Ardui ... duino/cores/arduino
9#
 樓主| 發表於 2013-4-29 23:16:05 | 只看該作者
對了,Arduino 的 source code 放在 github 上,所以也可以線上看:
coopermaa 發表於 2013-4-26 14:36


cooper老師, 您提供的資訊真是詳細,
感謝唷!!
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-9-29 07:44 , Processed in 0.151396 second(s), 8 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表