Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:30:29+00:00 2026-06-14T04:30:29+00:00

I have a colour class set up where it takes red/blue/green values and uses

  • 0

I have a colour class set up where it takes red/blue/green values and uses them to create a hex string which is then used to return a colour. It can be initialized by providing a red, blue and green colour like this

 Colour *col = [[Colour alloc] initWithRed:200 Green:100 Blue:05];

The problem with this method is, if I pass in a value like 05, the 0 is stripped off, and just a 5 is passed in, so the blue will set to just be 5 rather than 05. My method to return a valid hex string is this

   -(NSString *)getHexString
    {
        NSString *hexString = [NSString stringWithFormat:@"#%x%x%x", iRed_i,iGreen_i,iBlue_i];
        hexString = [hexString uppercaseString];
        return hexString;
    }

It is expecting 2 values for each number to return the correct colour code, but the 0 is being stripped off meaning it returns an incorrect colour.

The other method I use to create a colour is to initialise the colour object with a hex string like this:

Colour *colour = [[Colour alloc] initWithHex:@"#782402"];

I then use scanner to separate the 3 values like so

if ([sHex_p hasPrefix:@"#"]) sHex_p = [sHex_p substringFromIndex:1];
            unsigned int outVal;
            NSString *sRed  = [sHex_p substringToIndex:2];
            NSScanner* scanner = [NSScanner scannerWithString:sRed];

            [scanner scanHexInt:&outVal];
            [self setRed:outVal];

            NSString *sGreen  = [[sHex_p substringFromIndex:2] substringToIndex:2];
            scanner = [NSScanner scannerWithString:sGreen];
            [scanner scanHexInt:&outVal];
            [self setGreen:outVal];

            NSString *sBlue = [sHex_p substringFromIndex:4];
            scanner = [NSScanner scannerWithString:sBlue];

            [scanner scanHexInt:&outVal];

            [self setBlue:outVal];
        }

But again same problem, in the hex string I provided the last 2 values are 02, but when converting from int to string, that 0 will be stripped out, so just a 2 will be passed, again ending up with an incorrect colour.

I really am unsure as what the best way to solve this is. Would be really grateful if someone could be point in the right direction.

I have been testing it using this site here

http://www.colorpicker.com/

Quick example I am trying to create this hex string “#DB4200”. It requires red = 219 green = 66, blue = 00

But as the blue is just set to 0 when converts from string to int, ends up returning an incorrect colour, hex string ends up as “#DB420”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:30:30+00:00Added an answer on June 14, 2026 at 4:30 am

    Well, this actually involves three different situations. First, 05 is just 5,
    in both hexadecimal and decimal bases. The problem is not with your input in
    this case, but probably with the output. However, remember this is a decimal
    literal, so if you want #101010 to mean 16 out of 255 of each color, use
    hexadecimal constants like 0x10.

    Second: Regarding output. The format string you’re using does not enforce 2
    digits in each color, and that’s why your 05 becomes just 5. You can force
    the output of an integer (in hexadecimal form) to two digits padded with zeros
    by using %02x instead of %x.

    Third, is actually first. Looks like your method is working, but the output is
    wrong. Fixing the format string should solve this. Anyway, here’s an
    alternative version using sscanf.

    - (void)interpretHexString(NSString *str)
    {
        int r,g,b;
        const char *s = [str cStringUsingEncoding:NSUTF8StringEncoding];
    
        if (sscanf(s, "#%02x%02x%02x", &r, &g, &b) != 3) {
            /* problem with input */
        } else {
            /* set ivars */
            [self setRed:r];
            [self setGreen:g];
            [self setBlue:b];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class of inputs containing hex values for colour, without the #
Let's say I have an class called Star which has an attribute color .
Possible Duplicate: Difference between Color.red and Color.RED I have seen that the Java class
I have a set of stylesheets with different colour options. I have created a
I have a page where the background colour can be set dynamically. In some
I have an array that contains the RGB colour values for each pixel in
I have a method GetColors which takes a GetColorIdsRQ as a parameter and returns
Background: I have a custom class, which represents a Data Base Table, each property
I am developing one application in which I have set lots of text in
I have a set of div's with options inside them, I have already written

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.