Your function says r,g,b but your code says num?
Your works, if you actually pass it an integer num and not three integers.
So, this works ( after adjusting for some case errors):
cpp
string ConvertRGBtoHex(int num) {
string rgb;
char c;
for (int i=2*sizeof(int) - 1; i>=0; i--) {
c = "0123456789ABCDEF"[((num >> i*4) & 0xF)];
rgb += c;
}
return rgb;
}
However, you probably only want to see three hex bytes, so you might want to fix then length. Here's the same function, altered slightly.
cpp
string ConvertRGBtoHex(int num) {
static string hexDigits = "0123456789ABCDEF";
string rgb;
for (int i=(3*2) - 1; i>=0; i--) {
rgb += hexDigits[((num >> i*4) & 0xF)];
}
return rgb;
}
Now, if you actually want to pass three values, you can reuse the prior function with this one:
CODE
string ConvertRGBtoHex(int r, int g, int B) {
int rgbNum = ((r & 0xff) << 16)
| ((g & 0xff) << 8)
| (b & 0xff);
return ConvertRGBtoHex(rgbNum);
}
Hope this helps.
EDIT: Bloody thing won't turn emo crap off on edit.
This post has been edited by baavgai: 20 Jul, 2008 - 06:06 AM