How can I make ios UDID shorter but still unique enlough - iphone

I want to make the UDID of a iOS device to less than or equal to 15 characters.
I am porting a Symbian based client-server software to iOS. The server side uses IMEI (15 digits)to id a client phone. Now on iOS, UDID is too long(40 hex digits). As I want to minimize the change of server program or DB, I need to store the UDID in a varchar(15).
So it's there any way to make the UDID shorter but still unique enlough. It could be much better if I can also get the UDID from the shorted string.

You could convert the hex digits into 20 Latin-1 characters (≤0xff) or 10 Unicode BMP characters (≤0xffff).
If that varchar(15) can accept Unicode BMP characters, then we are done.
Otherwise, you could chop the last 5 characters from that 20 Latin-1 characters. The UDID is in fact a SHA-1 hash of some device-unique values, which can be considered quite random and the digits are uniformly distributed. Therefore, with 15 Latin-1 characters the reduced UDID should be able to represent 25615 ~ 1036 devices, which is much more than enough.
In fact, even if you just take 15 hex characters from the UDID it could already represent ~ 1018 devices.
Note that the last 2 methods are lossy, i.e. there is no way you could get the complete UDID from the 15 characters.

I just wrote this gist -> https://gist.github.com/3996097
What do you think about a CFUUID + sha1 + substring + random uppercase ?
//Get a random hash (Generated from CFUUID+sha1)
NSString *hash = [NSString sha1:[NSString getUUID]];
//Shorten the sha1
NSString *short_random_id = [hash substringFromIndex:[hash length]-10];
//Random uppercase / lowercase the id
NSMutableString *random_id_final = [NSMutableString string];
for (NSUInteger i = 0; i < [short_random_id length]; i++)
{
NSString *substring = [short_random_id substringWithRange:NSMakeRange(i, 1)];
[random_id_final appendString:(rand() % 2) ? [substring lowercaseString] : [substring uppercaseString]];
}

Related

Convert iPhone unique identifier to integer

I have made a web service that gets an integer as a unique identifier for a Phone. Later, i realised that an iPhone UID is an NSstring in that format: 5DE31FA12-XXXXX-XXXXX-XXXXXXX. So i would like to convert this value to an integer.
Here is the way i get the UID in a NSString format.
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
NSString *theUID = (NSString *)string;
How can i convert NSString *theUID into an integer value, so as to send it to the webservice?
EDIT: As CocoaFu correctly mentioned , it is impossible to fit that value in an int. Could I somehow convert it to an NSString with a format like "ddddddddddddddddddddddddddd" (where d = 0-9) so as to hash it and then send it? It is impossible to change the webservice now.
The largest integer is 8 bytes or 64 bits. The UUID you show has 26 hex characters which is 104 bits. It just won't fit in an integer. Use the string.
CFUUIDCreate returns a string with 32 hex characters, that is 128 bits or 16 bytes. Ignore the '-' characters, they are just for human readability. Here is an example from
CFUUIDCreate: BDFC3FE6-3A5E-48C0-B18D-E42B8E275428
UDID won't fit in an int. The format you have listed is what the simulator gives you. Device UDID is 40-characters long alpha-numeric.

NSString representation of fractions using unicode

I am trying to "nicely" display fractions in my iPhone application. Previously I have been using a tedious switch statement leading to hardcoded unicode characters for the vulgar fractions, but I have learnt about the unicode fraction slash character which, if I am understanding it correctly, should mean that I can create a string as follows:
[NSString stringWithFormat:#"%i\u2044%i",numerator,denominator];
And the "renderer" will automatically print it with a smaller, superscriped numerator and subscripted denominator. However, the above code just gives me the standard 1/2 appearance. I am using drawAtPoint to put the string on the screen. I have experimented with decomposedStringUsingCanonicalMapping and precomposedStringUsingCanonicalMapping but to be honest the documentation lost me.
Should this be working or does NSString drawing not cope with this?
I happened to only want simple fractions for recipes to be converted to Unicode vulgar fractions.
Here is how you can do it:
CGFloat quantityValue = 0.25f;
NSString *quantity = nil;
if (quantityValue == 0.25f) {
// 1/4
const unichar quarter = 0xbc;
quantity = [NSString stringWithCharacters:&quarter length:1];
} else if (quantityValue == 0.33f) {
// 1/3
const unichar third = 0x2153;
quantity = [NSString stringWithCharacters:&third length:1];
} else if (quantityValue == 0.5f) {
// 1/2
const unichar half = 0xbd;
quantity = [NSString stringWithCharacters:&half length:1];
} else if (quantityValue == 0.66f) {
// 2/3
const unichar twoThirds = 0x2154;
quantity = [NSString stringWithCharacters:&twoThirds length:1];
} else if (quantityValue == 0.75f) {
// 3/4
const unichar threeQuarters = 0xbe;
quantity = [NSString stringWithCharacters:&threeQuarters length:1];
}
NSLog(#"%#", quantity);
I'm not aware of any way for a unicode character to have the properties you describe. AFAIK the only thing that distinguishes U+2044 from a regular slash is it's a bit more angled and has little-to-no space on either side, therefore making it nestle up a lot closer to the surrounding numbers.
Here's a page on using the Fraction Slash in HTML, and as you can see it demonstrates that you simply get something like "1⁄10" if you try and use it on your own. It compensates for this by using the <sup> and <sub> tags in HTML on the surrounding numbers to get an appropriate display.
In order for you to get this to work in NSString you're going to have to figure out some way to apply superscripting and subscripting to the surrounding numbers yourself.
There are some included Unicode chars that do give actual fraction appearances, but they're limited to a 1/2, a 1/3 and a 1/4 I think.
If you want this for arbitrary fractions, seems to me like you need a custom view that draws the appropriate look; either through using positioned subviews or drawRect:.
I know this was a long time ago, but if it helps, there are superscript Unicode characters for all decimal numbers you can use to display arbitrary fractions in most fonts; see answers on a similar question here - https://stackoverflow.com/a/30860163/4522315
Edit:
As per comments, this solution depends on the font you're using. Amsi Pro (left) and other commercial fonts tend to include all the required symbols for superscripts, but the system font (right) does not.

iPhone - Truncate String to Last Four

All,
I can't seem to find the answer I'm looking for using search so I'll ask it.
I need to pull the last four digits of a credit card number out and set it into another string. It needs to account for the variances in credit card number lengths (i.e. 16 numbers or 15 numbers, etc)
i.e. if the number was "1234567890123456" I would want to set a new string as "3456".
Thanks
Assuming the credit card number is stored as a string:
NSString *lastFour = [fullNumber substringFromIndex:[fullNumber length] - 4];
Assuming it's an unsigned integer of any wide-enough type:
NSString *lastFour = [NSString stringWithFormat: #"%04u", (unsigned int)(fullNumber % 10000)];
NSString *newString = [oldString substringFromIndex:[oldString length] - 4];

objective-c code to right pad a NSString?

Can someone give a code example of how to right pad an NSString in objective-c please?
For example want these strings:
Testing 123 Long String
Hello World
Short
if right padded to a column width of say 12: and then a sting "XXX" is added to the end of each, it would give:
Testing 123 xxx
Hello World xxx
Short xxx
That is a 2nd column would like up.
Adam is on the right track, but not quite there. You do want to use +stringWithFormat:, but not quite as he suggested. If you want to pad "someString" to (say) a minimum of 12 characters, you'd use a width specifier as part of the format. Since you want the result to be left-justified, you need to precede the width specifier with a minus:
NSString *padded = [NSString stringWithFormat:#"%-12#", someString];
Or, if you wanted the result to be exactly 12 characters, you can use both minimum and maximum width specifiers:
NSString *padded = [NSString stringWithFormat:#"%-12.12#", someString];
2nd column of what would line up?
Given that you are on iOS, using HTML or a table view would be far more straightforward than trying to line up characters with spaces. Beyond being programmatically more elegant, it will look better, be more resilient to input data changes over time, and render a user experience more in line with expectations for the platform.
If you really want to use spaces, then you are going to have to limit your UI to a monospace font (ugly for readability purposes outside of specific contexts, like source code) and international characters are going to be a bit of a pain.
From there, it would be a matter of getting the string length (keeping in mind that "length" does not necessarily mean "# of characters" in some languages), doing a bit of math, using substringWithRange: and appending spaces to the result.
Unfortunately, Objective-C does not allow format specifiers for %#. A work-around for padding is the following:
NSString *padded = [NSString stringWithFormat:#"%#%*s", someString, 12-someString.length, ""];
which will pad the string to the right with spaces up to a field length of 12 characters.
%-# does not work, but %-s works
NSString *x = [NSString stringWithFormat:#"%-3#", #"a" ];
NSString *y = [NSString stringWithFormat:#"%-3#", #"abcd" ];
NSString *z = [NSString stringWithFormat:#"%-3# %#", #"a", #"bc" ];
NSString *zz = [NSString stringWithFormat:#"%-3s %#", "a", #"bc" ];
NSLog(#"[%#][%#][%#][%#].......", x,y,z,zz);
output:
[a][abcd][a bc][a bc].......
Try below. its working for me.
NSString *someString = #"1234";
NSString *padded = [someString stringByPaddingToLength: 16 withString: #"x" startingAtIndex:0];
NSLog(#"%#", someString);
NSLog(#"%#", padded);
First up, you're doing this a bad way. Please use separate labels for your two columns and then you will also be able to use proportional fonts. The way you're going about it you should be looking for an iPhone curses library.
If you really have to do it this way just use stringWithFormat:, like:
NSString *secondColumnString = #"xxx";
NSString *spacedOutString = [NSString stringWithFormat:#"testingColOne %#", secondColumnString];
NSString *spacedOutString = [NSString stringWithFormat:#"testingAgain %#", secondColumnString];

iPhone SDK : NSString NSNumber IEEE-754

Can someone help me ? I have a NSString with #"12.34" and I want to convert it into a NSString with the same float number but in single precision 32bits binary floating-point format IEEE-754 : like #"\x41\x45\x70\xa4" (with hexa characters) or #"AEp¤"...
I'm sure it's something easy but after many hours of reading the doc without finding a solution...
Thank you !
As Yuji mentioned, it's not a good idea to encode an arbitrary byte sequence into an NSString(although it can contain null bytes), as encoding transformations can(and probably WILL) destroy your byte sequence. If you want access to the raw bytes of a float, you may want to consider storing them as an NSData object(though I suggest you think through your reasons for wanting this first). To do this:
NSString *string = #"10.23";
float myFloat = [string floatValue];
NSData *myData = [[NSData alloc] initWithBytes:&myFloat length:sizeof(myFloat)];
If you want to get the raw bytes of a float, you could cast it, like so:
NSString *str = #"12.34";
float flt = [str floatValue];
unsigned char *bytes = (unsigned char *)&flt;
printf("Bytes: %x %x %x %x\n", bytes[0], bytes[1], bytes[2], bytes[3]);
However the order in which these bytes are stored in the array depends on the machine. (See http://en.wikipedia.org/wiki/Endianness). For example, on my Intel iMac it prints: "Bytes: a4 70 45 41".
To make a new NSString from an array of bytes you can use initWithBytes:length:encoding: