Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,048 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,565 people online right now. Registration is fast and FREE... Join Now!




Converting RGB to Hex

 
Reply to this topicStart new topic

Converting RGB to Hex, i'm new at this i just want to get thru it can someome pls help

passionfruit
19 Jul, 2008 - 03:50 PM
Post #1

New D.I.C Head
*

Joined: 19 Jul, 2008
Posts: 4

create a function that will accept 3 interger parameters (r,b,g) and return a hexadecimal value. The final hexadecimal value will be 3 2 character answers concatenated together...i'm failing this class but i want to know it so baddddd... i just need help
cpp

stringConvertRGBtohex(int r, int g, int cool.gif//all ints 255 MAX
{
string RGB
char c;

for (int i=2*sizeof(int) - 1; i>=0; i--)
{
c = "0123456789ABCDEF"[((num >> i*4) & 0xF)];
rgb += c;
}
return RBG;
}
int main()
{
int n;
string hStr;

while (cin >> n)
{
cout << "decimal: " << n << endl;
hStr = ConvertRGBtohex n);
cout << "RGB: " << hStr << endl << endl;
}
return hex;
}


This post has been edited by born2c0de: 20 Jul, 2008 - 09:57 AM
User is offlineProfile CardPM
+Quote Post

RodgerB
RE: Converting RGB To Hex
19 Jul, 2008 - 06:43 PM
Post #2

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,131



Thanked: 16 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
This worked for me:

cpp

#include <iostream>
#include <string.h>

using namespace std;
string RGBToHex(int rNum, int gNum, int bNum);

int main()
{
cout << RGBToHex(255, 255, 255).c_str();
return 0;
}

string RGBToHex(int rNum, int gNum, int bNum)
{
string result;

char r[255];
sprintf_s(r, "%.2X", rNum);
result.append(r );

char g[255];
sprintf_s(g, "%.2X", gNum);
result.append(g );

char b[255];
sprintf_s(b, "%.2X", bNum);
result.append(b );

return result;
}

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Converting RGB To Hex
19 Jul, 2008 - 06:59 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,917



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Please use code.gif tags when posting code.
User is online!Profile CardPM
+Quote Post

baavgai
RE: Converting RGB To Hex
20 Jul, 2008 - 06:05 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,018



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
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. tongue.gif


This post has been edited by baavgai: 20 Jul, 2008 - 06:06 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 05:30PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month