Objective-C - ulong to uint32

4.1k Views Asked by At

How can i convert an ulong to uint32? A simple cast is not working. What works for me is converting the long to a string and then again parsing the string to an int.

However this seems really wrong. Is there any better way?

1

There are 1 best solutions below

0
On

Like Paul R said, not sure what you mean by "simple cast". Both of the following are working fine for me (and, note, I'm testing with a ULONGLONG).

  1. Direct cast:

    unsigned long long val1 = (unsigned long long)75;
    UInt32 val2 = (UInt32)val1;
    NSLog( @"%qu, %lu", num1, num2 );
    // OUTPUTS: 75, 75

  2. Using NSNumber:

    NSNumber* val1 = [NSNumber numberwithUnsignedLongLong:75];
    NSNumber* val2 = [NSNumber numberwithUnsignedInteger:[val1 usignedIntegerValue]];
    NSLog( @"%qu, %u", [num1 unsignedLongLongValue], [num2 unsignedIntegerValue]];
    // OUTPUTS: 75, 75