Arduino - 将HEX转换为RGB的奇怪行为

red*_*100 3 rgb hex arduino

我正在尝试将HEX颜色代码转换为RGB但是当我在Arduino上运行代码时,它不会拾取RED.

难道我做错了什么?

在C++编译器上工作得很好.

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() {

}
Run Code Online (Sandbox Code Playgroud)

小智 8

当我运行你的代码时,我仍然没有获得红色的价值.但是使用MAC的相同代码

long number = (long) strtol( &hexstring[1], NULL, 16 );
Run Code Online (Sandbox Code Playgroud)

long number = (long) strtol( &hexstring[0], NULL, 16 );
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助有人挣扎RGB和HEX值


小智 7

number应该是类型,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() {

}
Run Code Online (Sandbox Code Playgroud)