C/C++;十六进制字符*到字节数组 我一直在努力解决如何在嵌入式C++中做这一段时间,现在我在RGB88的网站上有十六进制颜色,例如“我想把它转换成C++ RGB55”十六进制值,例如0x177c/p>
目前,我已从字符串中删除了#,并一直在将其转换为可用于创建RGB555的类型 我的代码目前看起来像C/C++;十六进制字符*到字节数组 我一直在努力解决如何在嵌入式C++中做这一段时间,现在我在RGB88的网站上有十六进制颜色,例如“我想把它转换成C++ RGB55”十六进制值,例如0x177c/p>,c++,c,hex,scanf,C++,C,Hex,Scanf,目前,我已从字符串中删除了#,并一直在将其转换为可用于创建RGB555的类型 我的代码目前看起来像 p_led_struct->color = "#ba00ff"; char hexString[7] = {}; memmove(hexString, p_led_struct->color+1, strlen(p_led_struct->color)); byte colorBytes[3]; sscanf(hexString,"%x%x%x",&colorB
p_led_struct->color = "#ba00ff";
char hexString[7] = {};
memmove(hexString, p_led_struct->color+1, strlen(p_led_struct->color));
byte colorBytes[3];
sscanf(hexString,"%x%x%x",&colorBytes);
尽管colorBytes数组的数据不正确,但hexString值正确变为“ba00ff”
任何关于我应该如何进行此转换的帮助都将非常棒:)
谢谢 sscanf(十六进制字符串、%x%x%x“、&colorBytes)的问题代码>是:
sscanf
希望您给出3int
s作为参数,但只给出了一个数组,而不是int
%x
读取超过2个字符sscanf
希望您给出3int
s作为参数,但只给出了一个数组,而不是int
%x
读取超过2个字符有关scanf系列的非常有用的信息:使用
hh
修饰符直接扫描到1字节
p_led_struct->color = "#ba00ff";
byte colorBytes[3];
int result;
result = sscanf( p_led_struct->color, "#%2hhx%2hhx%2hhx", &colorBytes[0],
&colorBytes[1], &colorBytes[2]);
if (result != 3) {
; // handle problem
}
成功扫描3个RGB 8位字节后,重新计算3x5位结果
int r,g,b;
r = colorBytes[0] >> 3;
g = colorBytes[1] >> 3;
b = colorBytes[2] >> 3;
printf("%04X", (r << 10) | (g << 5) | (b << 0));
intr,g,b;
r=colorBytes[0]>>3;
g=彩色字节[1]>>3;
b=彩色字节[2]>>3;
printf(“%04X”),(r使用hh
修饰符直接扫描到1字节
p_led_struct->color = "#ba00ff";
byte colorBytes[3];
int result;
result = sscanf( p_led_struct->color, "#%2hhx%2hhx%2hhx", &colorBytes[0],
&colorBytes[1], &colorBytes[2]);
if (result != 3) {
; // handle problem
}
成功扫描3个RGB 8位字节后,重新计算3x5位结果
int r,g,b;
r = colorBytes[0] >> 3;
g = colorBytes[1] >> 3;
b = colorBytes[2] >> 3;
printf("%04X", (r << 10) | (g << 5) | (b << 0));
intr,g,b;
r=colorBytes[0]>>3;
g=彩色字节[1]>>3;
b=彩色字节[2]>>3;
printf(“%04X”),(r将p\u-led\u-struct->color
转换为整数
p_led_struct->color = "#ba00ff";
unsigned int colorValue = strtoul(p_led_struct->color+1, NULL, 16);
并将此RGB值转换为RGB555。RGB整数包含0000.0000.rrrrrr.rrrrrr.gggggg.gggggg.bbbbbb.bbbb字段,RGB555包含0rrr.rrgg.gggb.bbbbbb字段,因此我们只需要位移位:
unsigned short rgb555 = ((colorValue & 0x00f80000) >> 9) + // red
((colorValue & 0x0000f800) >> 7) + // green
((colorValue & 0x000000f8) >> 3); // blue
将p\u led\u struct->color
转换为整数
p_led_struct->color = "#ba00ff";
unsigned int colorValue = strtoul(p_led_struct->color+1, NULL, 16);
并将此RGB值转换为RGB555。RGB整数包含0000.0000.rrrrrr.rrrrrr.gggggg.gggggg.bbbbbb.bbbb字段,RGB555包含0rrr.rrgg.gggb.bbbbbb字段,因此我们只需要位移位:
unsigned short rgb555 = ((colorValue & 0x00f80000) >> 9) + // red
((colorValue & 0x0000f800) >> 7) + // green
((colorValue & 0x000000f8) >> 3); // blue
如果跳过第一个字符,难道不需要从字符串长度中减去1吗?我相信字符串'/n'的结尾会变成“ba00ff”7个字符,我不确定在sscanf中没有字符串结尾字符的字符数组是否会导致错误。编辑抱歉,我的意思是“Null终止”\0
不是/n
“/n”是换行符,而不是终止符。字符串中没有换行符。但我很少使用C字符串,所以可能我对字符串长度有错误。memmove
是fine;最好在字符串的末尾有'\0'
。如果跳过第一个字符,不需要从字符串长度中减去1吗?我相信字符串'/n'的结尾会变成“ba00ff”7个字符,我不确定在sscanf中没有字符串结尾字符的字符数组是否会导致错误。编辑抱歉,我的意思是“Null终止”\0
不是/n
“/n”是换行符,而不是终止符。字符串中没有换行符。但我很少使用C字符串,所以可能我对字符串长度有错误。memmove
是fine;字符串末尾有'\0'
很好。太棒了,这很好用,谢谢你解释了如何正确使用sscanf!太棒了,这很好用,谢谢你解释了如何正确使用sscanf!在你的最后一行输入错误,现在你得到了一个红色的灰度:)@Jongware谢谢你,已经修好了。现在唯一的红色是我的脸。:)谢谢Chux,我正在尝试理解解决方案应该是result=sscanf(p#u-led_-struct->color,“#%2hhhx%2hhhx%2hhhx”、&colorBytes[0]、&colorBytes[0];
是&colorBytes[0]、&colorBytes[1]、&colorBytes[2]
还是第一个地址的所有引用?@Tim Kalinowski又一个糟糕的问题。谢谢你指出。在你的最后一行输入错误,现在你得到了一个基于红色的灰度:)@Jongware谢谢并修复了。现在唯一的红色是我的脸。:)谢谢Chux,我试图理解解决方案应该是result=sscanf(p\u led\u struct->color,“#%2hhx%2hhx%2hhx”、&colorBytes[0]、&colorBytes[0]、&colorBytes[0]);
be&colorBytes[0]、&colorBytes[1]、&colorBytes[2]
或对第一个地址的所有引用?@Tim Kalinowski又是一个糟糕的问题。感谢您指出这一点。