我正在尝试将HEX颜色代码转换为RGB但是当我在Arduino上运行代码时,它不会拾取RED. 难道我做错了什么? 在C compilator上工作得很好. void setup() { Serial.begin(115200); String hexstring = "B787B7"; int n
难道我做错了什么?
在C compilator上工作得很好.
void setup() { Serial.begin(115200); String hexstring = "B787B7"; int number = (int) strtol( &hexstring[1], NULL, 16); int r = number >> 16; int g = number >> 8 & 0xFF; int b = number & 0xFF; Serial.print("red is "); Serial.println(r); Serial.print("green is "); Serial.println(g); Serial.print("blue is "); Serial.println(b); } void loop() { }您的数字应为long类型,因为int类型以16位编码,并且不能超过32,767.
void setup() { Serial.begin(115200); String hexstring = "B787B7"; long number = (long) strtol( &hexstring[1], NULL, 16); int r = number >> 16; int g = number >> 8 & 0xFF; int b = number & 0xFF; Serial.print("red is "); Serial.println(r); Serial.print("green is "); Serial.println(g); Serial.print("blue is "); Serial.println(b); } void loop() { }