Converting RGB to Hex.

This is a discussion on Converting RGB to Hex. within the C Programming forums, part of the General Programming Boards category; Hi again folks, Currently I am trying to make a program that will give the hexadecimal value of a colour ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    45

    Converting RGB to Hex.

    Hi again folks,

    Currently I am trying to make a program that will give the hexadecimal value of a colour based upon the RGB values. This is the code that I have:

    Code:
    /* RGBtoHEX - A program designed to convert colours in RGB to Hexadecimal value */
    #include <stdio.h>
    
    main()
    {
          int red;
          int green;
          int blue;
          
          printf("Please enter the value for Red: ");                                           
          scanf("%d",&red);
          printf("Please enter the value for Green: ");
          scanf("%d", &green);
          printf("Please enter the value for Blue: ");
          scanf("%d", &blue);
          
          printf("\nThe hexadecimal value for your colour is #%X%X%X.", red, green, blue);   
          
          return 0;
    }
    Currently, the line

    Code:
    printf("\nThe hexadecimal value for your colour is #%X%X%X.", red, green, blue);
    ,

    will only correctly work for high values of R, G and B. I would like to know if you can transform, for example F to 0F, just for clarity in the output.

    Any help is greatly appreciated,
    mintsmike

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Would "%02X" not work?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    Thanks.

    I take it if i were to put.

    %03X or %04X it would display like 3 and 4 characters,

    Thanks anyway and thanks in advance,
    mintsmike

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting an int into hex and memcpy
    By puppy in forum C Programming
    Replies: 10
    Last Post: 07-03-2006, 11:39 AM
  2. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  3. converting hex to dec
    By jibbles in forum C Programming
    Replies: 20
    Last Post: 08-07-2004, 11:40 PM
  4. Stopwatch program (need help!)
    By modnar in forum C Programming
    Replies: 9
    Last Post: 03-21-2004, 11:42 PM
  5. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 10:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21