10

I'm trying to write a method which takes in a hex value such as 0xD2691E for the purpose of returning a UIColor object.

I found this macro which I want to convert into a method, but I don't know how to specify the data type other than void *.

#define UIColorFromRGB(rgbValue) [UIColor \
       colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
       green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
       blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

  //Then use any Hex value

 self.view.backgroundColor = UIColorFromRGB(0xD2691E);  
Community
  • 1
  • 1
openfrog
  • 40,201
  • 65
  • 225
  • 373
  • Related http://stackoverflow.com/questions/1243201/is-macro-better-than-uicolor-for-setting-rgb-color – halex Apr 10 '13 at 12:05

4 Answers4

12

What is the data type of a hex value like 0xD2691E?

According to C standard, the type of an hexadecimal constant is the first of this list in which its value can be represented:

C11 (n1570), § 6.4.4.1 Integer constants

int
unsigned int
long int
unsigned long int
long long int
unsigned long long int

Since D2691E (b16) is equal to 13789470 (b10), the type of your constant depends on your implementation.

C standard only guarantees that INT_MAX >= +32767, whereas LONG_MAX >= +2147483647.

C11 (n1570), 5.2.4.2.1 Sizes of integer types

  • INT_MAX +32767
  • LONG_MAX +2147483647

Therefore, (unsigned) long int could be a suitable choice.

md5
  • 23,373
  • 3
  • 44
  • 93
  • iOS uses the ILP32 architecture, so both int and long are 32-bit here (https://developer.apple.com/library/ios/#documentation/Xcode/Conceptual/iPhoneOSABIReference/Articles/ARMv6FunctionCallingConventions.html#//apple_ref/doc/uid/TP40009021-SW1) – Martin R Apr 10 '13 at 12:24
1

from what i remembered they are something like int or unsigned int.

Maurice Rodriguez
  • 663
  • 2
  • 7
  • 17
1

Please try to use this one.....

    unsigned long long 
    unsigned long int
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

In this method, they perform bitwise AND operation, so it must be unsigned of int OR long

Mani
  • 17,549
  • 13
  • 79
  • 100