简体   繁体   English

Arduino 字节转 HEX (12Bit)

[英]Arduino byte to HEX (12Bit)

I am learning Arduino.我正在学习 Arduino。 Now I want to change the color of the background.现在我想改变背景的颜色。

I am using the Uno Board, and the Sparkfun Color LCD Shield.我正在使用 Uno Board 和 Sparkfun Color LCD Shield。 I am also using the SparkFunColorLCDShield Library.我也在使用 SparkFunColorLCDShield 库。

The easiest way the change the color is to do lcd.clean(HEXCODE);更改颜色的最简单方法是执行lcd.clean(HEXCODE); Example for white background: lcd.clean(0xFFF);白色背景示例: lcd.clean(0xFFF); (12Bit of 0xFFFFFF) It's also working with lcd.clean(16777215); (12Bit of 0xFFFFFF) 它也适用于lcd.clean(16777215);

The 16777215 is the decimal number of 0xFFFFFF. 16777215 是 0xFFFFFF 的十进制数。 Now I need a method, that gives me the decimal number of any HEX Number.现在我需要一种方法,它给我任何十六进制数的十进制数。

More examples:更多例子:

  • 00FFFFF -> 1048575 00FFFFF -> 1048575
  • 000FFFF -> 65535 000FFFF -> 65535

After much guessing and reading here :经过多番猜测和阅读这里

Maybe you want this:也许你想要这个:

#include <stdlib.h>
#include <stdio.h>


int Convert24bitColorStringTo12BitColor(const char *colorcode24)
{
  long color24 = strtol(colorcode24, NULL, 16);
  long color12 = ((color24 & 0xf0) >> 4) | ((color24 & 0xf000) >> 8) | ((color24 & 0xf00000) >> 12);
  return color12;
}

int main()
{
  printf("%x\n", Convert24bitColorStringTo12BitColor("abcdef"));
  printf("%x\n", Convert24bitColorStringTo12BitColor("ffff"));
}

This prints:这打印:

ace
ff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM