将十六进制字符串转换为浮点数组 [英] Converting a hexidecimal string to a floating point array
问题描述
嘿clc ++和/或cgaopengl海报,
如何将传统上用于定义
颜色的十六进制字符串转换为HTML一个浮点数组?
换句话说,我怎么转换它:
char * hex ="#FF9933";
到这个:
float fpa = {1.0,0.6,0.2};
Hey c.l.c++ and/or c.g.a.opengl posters,
How do I convert a hexidecimal string, traditionally used for defining
colors with HTML, into a floating point array?
In other words, how do I convert this:
char* hex = "#FF9933";
into this:
float fpa = { 1.0, 0.6, 0.2 };
推荐答案
Bushido Hacks写道:
Bushido Hacks wrote:
嘿clc ++和/或cgaopengl海报,
如何转换传统上用于定义的十六进制字符串
带有HTML的颜色,变成一个浮点数组?
换句话说,我怎么转换它:
char * hex ="#FF9933";
进入:
float fpa = {1.0,0.6,0.2};
Hey c.l.c++ and/or c.g.a.opengl posters,
How do I convert a hexidecimal string, traditionally used for defining
colors with HTML, into a floating point array?
In other words, how do I convert this:
char* hex = "#FF9933";
into this:
float fpa = { 1.0, 0.6, 0.2 };
跳过#,提取前两个十六进制数字并将其转换为
使用strtol从base-16字符串开始的整数,除以整数
到255.(双常数,别忘了点)。然后对其他两对十六进制数字重复
。把它包装成一个函数
,你可以得到一些可以反复使用的东西。或者
你可以在不提取数字的情况下完成。只需将
整数转换为long,然后将其分成三个整数
使用/和%,然后将除以255进行除法。就像之前一样。
V
Skip the #, extract the first two hex digits and convert it to
an integer from base-16 string using strtol, divide the integer
by 255. (double constant, don''t forget the dot). Then repeat
for the other two pairs of hex digits. Wrap it up in a function
and you get yourself something you can use over and over. Or
you could do it without extracting digits. Just convert the
entire number into a long, then split it into three integers
using / and %, then do the division by 255. like before.
V
Victor Bazarov写道:
Victor Bazarov wrote:
使用/和%,然后进行除法255.像以前一样。
using / and %, then do the division by 255. like before.
部门和模块很贵。更好的是按位屏蔽和
位移。这很奇怪,但我已经看到很多程序使用
除法和模块用于操作本来更好用b / b
用逐位运算完成。除了无知之外还有其他原因吗?
Wolfgang Draxinger
-
division and module are expensive. Better is bitwise masking and
bitshifting. It''s strange but I''ve seen a lot of programs using
division and module for operaions that would have been better
done with bitwise operations. Is there a certain reason other
than ignorance?
Wolfgang Draxinger
--
Wolfgang Draxinger写道:
Wolfgang Draxinger wrote:
Victor Bazarov写道:
Victor Bazarov wrote:
使用/和%,然后用255除法。像以前一样。
using / and %, then do the division by 255. like before.
分区和模块很贵。更好的是按位掩蔽和
位移。这很奇怪,但我已经看到很多程序使用
除法和模块进行操作,这些操作本来可以通过按位操作完成。除了无知之外还有其他原因吗?
division and module are expensive. Better is bitwise masking and
bitshifting. It''s strange but I''ve seen a lot of programs using
division and module for operaions that would have been better
done with bitwise operations. Is there a certain reason other
than ignorance?
听到,听到!一位真正的专家在说话!
Hear, hear! A real expert speaking!
这篇关于将十六进制字符串转换为浮点数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!