How to convert an integer to a hexadecimal string in C++

int number = 42;
stringstream ss;
ss << hex << number;
// now ss.str(), which is a string, contains "2a"

Getting the UDID of an iOS device

[[UIDevice currentDevice] uniqueIdentifier]

iroundf

A few minutes ago I wrote a rounding function for myself in C. Probably not the best implementation, but it works all right. If you’re interested, check it out (I’ve put some sample values in the comments):

int iroundf(float value)
{
    if(value > 0.0)
    {       
        int unsigned cutvalue = (unsigned int)value; // 3.8234234 => 3
        float remain = value - cutvalue; // 3.8234234-3 = 0.8234234
       
        if(remain >= 0.5) // 0.8234234 >= 0.5 => TRUE
            return cutvalue + 1; // iroundf(3.8234234) => 4
        else
            return cutvalue;
    }
   
    if(value < 0.0)
    {
        int cutvalue = (int)value; // -1.34 => -1 // -4.67 => -4
        float remain = value - cutvalue; // -1.34-(-1) = -0.34 // -4.64-(-4) = -0.64
       
        if(remain >= -0.5) // -0.31 >= -0.5 => TRUE // -0.61 >= -0.5 => FALSE
            return cutvalue; // iroundf(-1.34) => -1
        else
            return cutvalue - 1; // iroundf(-4.67) => -5
    }
   
    return 0;
}

update: I just came across the following very short and elegant solution (talk about forests and trees…)

int result = (int)(float_number + 0.5);

Getting filepath to the Documents folder on iPhone

To achieve the task written in the title of this post, you have to do the following:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsPath = [paths objectAtIndex:0];

Button resize causing text blur?

Today I noticed a very, very strange behaviour (bug?) in Xcode. I am creating buttons for my iPhone application with a custom image background and a text on top of it. The Interface Builder attributes window looks like this:

Usually I change the font-family and size for my text, but when I do this, the button dimensions suddenly grow pretty big. So I just head over to the Button Size tab and reset the dimension values to the originals, like so:

And here comes the crazyness: as I modify the width/height values, the text inside the button gets slightly blurred!

During my current development I realized that some button texts are not as sharp as others, but I had no idea why. Finally I managed to catch this “bug” by redoing the last few steps before the blur effect occured.

How to overcome this pesky issue? If I resize the button “by hand” in the view editor, the text keeps its sharpness. Odd.