943,793 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1064
  • C++ RSS
Aug 17th, 2008
0

check the length of a hex number?

Expand Post »
Hey

I'm writing a color class, and I want to have a constructor that can take in an unsigned int like 0x00FF00FF and interpret it as fully opaque green. I have code for that (I think it should work, could someone check it?):
C++ Syntax (Toggle Plain Text)
  1. rzb::color::color(unsigned int hex)
  2. {
  3. m_red = (hex & 0xff000000)/(0x00ffffff);
  4. m_green = (hex & 0x00ff0000)/(0x0000ffff);
  5. m_blue = (hex & 0x0000ff00)/(0x000000ff);
  6. m_alpha = (hex & 0x000000ff);
  7. }

The thing is, I'd like the user to also be able to give this input: 0x00FF00, and have that also be interpreted as opaque green (alpha value would be FF by default), but I can't figure out how to do that. Is there a way to check the length of a number passed as input (or some other algorithm to do what I want). I can't just compare the values, because 0xffffff (opaque white) comes out to 16,777,215, but 0x0000ffee (slightly transparent green) comes out to 65,518, a smaller value, even though it's the the larger form.
Similar Threads
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Aug 17th, 2008
0

Re: check the length of a hex number?

Wow, nvm - just figured it out.

Have alpha be first, not last.

But if someone could confirm that and/or check the above code I'd appreciate it.
Last edited by CoolGamer48; Aug 17th, 2008 at 11:10 pm.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Aug 18th, 2008
0

Re: check the length of a hex number?

Wow, nvm - just figured it out.

Have alpha be first, not last.

But if someone could confirm that and/or check the above code I'd appreciate it.
I don't think you want to be dividing by 0xff, 0xffff, and 0xffffff, but rather by 0x100, 0x10000, and 0x1000000. In particular, 0xff00 / 0xff = 0x100 = 256, which is not in the range of 0 to 255. You can also speed things and use the shift right (>>) operator rather than divide:

C++ Syntax (Toggle Plain Text)
  1. unsigned int red = (hex & 0x00ff0000) >> 16;
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Aug 18th, 2008
1

Re: check the length of a hex number?

Oh, you're right.

But actually, I dropped the system of using different members for red, green, blue, and alpha. I'm just using a single hex value as a member to save space.

Edit:
Just wondering, would this be any faster:
C++ Syntax (Toggle Plain Text)
  1. unsigned int red = (hex << 8) >> 16;
  2. //or is it:
  3. unsigned int red = (hex << 8) >> 24;
Last edited by CoolGamer48; Aug 18th, 2008 at 1:21 am.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Aug 18th, 2008
0

Re: check the length of a hex number?

Oh, you're right.

But actually, I dropped the system of using different members for red, green, blue, and alpha. I'm just using a single hex value as a member to save space.

Edit:
Just wondering, would this be any faster:
C++ Syntax (Toggle Plain Text)
  1. unsigned int red = (hex << 8) >> 16;
  2. //or is it:
  3. unsigned int red = (hex << 8) >> 24;
It would be the second one:
C++ Syntax (Toggle Plain Text)
  1. unsigned int red = (hex << 8) >> 24;

Which one is faster is a great question, one that I don't know the answer to but that hopefully others will.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Aug 18th, 2008
0

Re: check the length of a hex number?

Think about 64-bit portability (why not?): hex << 8) >> 24 does not work if sizeof(unsigned int) > 4.
Make user input argument safety in 100% portable manner, for example:
C++ Syntax (Toggle Plain Text)
  1. rzb::color::color(unsigned int hex)
  2. {
  3. m_alpha = (hex & 0xFF);
  4. m_blue = ((hex>>=8) & 0xFF);
  5. m_green = ((hex>>=8) & 0xFF);
  6. m_red = ((hex >>8) & 0xFF);
  7. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 18th, 2008
0

Re: check the length of a hex number?

Err... ok, ran into a prob again (like my first one). fyi ive gone back to the multiple member system (r,g,b,a as four separate variables). I still can't figure out how to let users pick whether or not to give the alpha bits. If they give (in a ARGB format) 0xee00ff00 (slightly transparent green), I'll read it fine, but if they give 0x00ff00, I'll read the alpha value as 0x00, when it should be 0xff. So my fix didn't really fix anything I guess. Any ideas on how to get this to work?
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Introducing myself and a n00b help question. ;)
Next Thread in C++ Forum Timeline: Homework problem (can't figure it out)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC