I am trying to generate a numerical string by padding the number with zeroes to the left.
0 would become 00000
1 would become 00001
10 would become 00010
I want to create five character NSString by padding the number with zeroes.
I read this Create NSString by repeating another string a given number of times but the output is an NSMutableString.
How can I implement this algorithm with the output as an NSString?
Best regards.
You can accomplish this by calling
[NSString stringWithFormat:#"%05d", [theNumber intValue]];
where theNumber is the NSString containing the number you want to format.
For further reading, you may want to look at Apple's string formatting guide or the Wikipedia entry for printf.
One quick & simple way to do it:
unsigned int num = 10; // example value
NSString *immutable = [NSString stringWithFormat:#"%.5u", num];
If you actually really want to use the long-winded approach from the example you read, you can send a “copy” message to a mutable string to get an immutable copy. This holds for all mutable types.
Related
I have a string with random names with an invisible decimal value as prefix . The decimal = the names length. I need to retrieve the names. Obviously they are of different length. I want the names in an array so my idea is to use stringByReplacingOccurrencesOfString:withString. I implement the word "trunk" at the beginning and end of names. Though I am having trouble accessing the index corresponding at the end of the name (decimal value), here is my code :
trimmed1 = [[trimmed1 stringByReplacingOccurrencesOfString:sp withString:#"trunk"]mutableCopy];
NSString *trunk = #"trunk%d";// add the ghost decimal at the end of prefix in order to get its value;
NSRange range =[trimmed1 rangeOfString:trunk];
int ghost= [trunk characterAtIndex:5];
NSMutableString *mu = [NSMutableString stringWithString:trimmed1];
[mu insertString : #"trunk" atIndex :range.location+range.length+ ghost];
I get the error [__NSCFString insertString:atIndex:]: Range or index out of bounds.
You are misunderstanding what %d means.
In a format used for creating a string it means "insert the value of an integer argument formatted as a string".
When matching one string against another it means "match the characters %d", I.e. it is not special in anyway.
You are getting an error as your string does not contain the characters "trunk%d". If you check the return value of rangeOfString: you will find it is returning a failure indication - read the documentation for how to test for that value.
For the simple task of matching an arbitrary decimal number trying looking at the NSString method rangeOfCharactersFromSet:.
You can also solve this problem with the classes NSScanner and NSRegularExpression.
HTH
I need to convert values like 1393443048683555715 to HEX. But, first of all, i cann't display it as decimal using NSLog(), for example.
Ok, it works:
NSLog(#"%qu", 1393443048683555706);
But what about converting to HEX. What type i have to use to store this big value?
NSLog([NSString stringWithFormat: #"%x", 1393443048683555706]);
// result eb854b7a. It's incorrect result!
but i forgot to say that this big number represented as string #"1393443048683555706" (not int)
You can use %qi and %qu format specifiers with NSLog to display 64-bit integers. Your constant appears to fit in 64-bit signed number, with the limits of:
[−9223372036854775808 to 9223372036854775807]
The "x" format specifier is for 32-bit numbers; you need to use either "qx" or "qX" (depending on whether you want the letter values to be uppercase or not). These are the formatters for unsigned long long values, see:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1
Next, you should not pass a string as you have done above directly to NSLog - this can cause a crash.
NSLog(string); // bad!!
NSLog(#"%#", string); // good
So if your value comes as a string, you'll want to do this:
NSString *longNumber = #"1393443048683555706";
NSLog(#"%qx", [longNumber longLongValue]);
If the string value can't be coerced to a number, longLongValue will return 0. I'll leave it to you do handle the error (and bounds) checking - see NSString for details.
If you want to save the hex value as a string, do this:
NSString *hexRepresentation = [NSString stringWithFormat:#"%qx", [longNumber longLongValue]];
Again, best to take care for error handling.
I know it is possible to use the methods compare: and isEqualToString:, and I suppose isEqualToString is the most efficient method If you know it´s an string. But my question is, is there another way to do it more efficiently? Like comparing char by char or something like that.
By reading the documentation:
The comparison uses the canonical representation of strings, which for a particular string is the length of the string plus the Unicode characters that make up the string. When this method compares two strings, if the individual Unicodes are the same, then the strings are equal, regardless of the backing store. “Literal” when applied to string comparison means that various Unicode decomposition rules are not applied and Unicode characters are individually compared. So, for instance, “Ö” represented as the composed character sequence “O” and umlaut would not compare equal to “Ö” represented as one Unicode character.
and:
When you know both objects are strings, this method is a faster way to check equality than isEqual:.
it seems that it's the best method available, to compare strings and that it does exactly what you need, that is: first it checks for length (if 2 strings have different length, is not necessary to check each char contained), then if the length it's the same it compares each char. Simple and efficient!
isEqualToString: is faster if you know both objects are strings, as the documentation states.
You could try converting both string to C strings and then use strcmp. Doubt it'll actually be any quicker though.
char *str1 = [myNSString1 UTF8String];
char *str2 = [myNSString2 UTF8String];
BOOL isEqual = strcmp(str1,str2);
I've got a dictionary initialized like so...
keyDictionary = [[NSDictionary dictionaryWithObjects:values forKeys:keys]retain];
where keys is an NSArray of the alphabet and other characters and values is an NSArray of unsigned chars, which are the USB hex keycodes for those characters.
The USB key codes are hex values that range from 0x04 to 0xE7. I'm trying to create a map between these two depending on what key is pressed on the keyboard.
The values array is created like so...
NSArray *values = [NSArray arrayWithObjects:
[NSNumber numberWithUnsignedChar:0x04]/*A*/,
[NSNumber numberWithUnsignedChar:0x05]/*B*/, /*ETC*/];
So ideally when I run this code...
where character == #"I"
- (uint8) getUSBCode:(NSString *)character
{
NSNumber *val = [keyDictionary objectForKey:character];
return (uint8)[val unsignedCharValue];
}
I would expect to get back 0x0C, but I'm getting 12 back as an int (which after I thought about it, makes sense). I need the hex value preserved. I do NOT need a string value. I need a straight conversion to the hex value or a better way to store
uint8 is just a typedef unsigned char.
EDIT I was not clear when I posted this earlier. Here's what I need.
I need the hex value of these codes because they are being sent over the internal company network. In addition, the pressed key's value is being converted from big endian (or little, it's escaping me right now which one it is) to the other, then being transmitted over an internal network. I understand that these values are stored in binary, but I need to transmit them in hex.
Also, I stated I was getting 12 back from the function. I was reading 12 from the debugger, not actually getting the value. That might be why I was getting confused.
12 (in base 10) is 0x0c.
If you want to print it out in hex, use the %x format specifier e.g.
NSLog(#"Hex value of char is 0x%02x", (unsigned int) c);
If you want to see it in hex in the debugger (assuming Xcode 3.2.x) right click on the variable and select hexadecimal as the format.
You know that an int is stored in binary (i.e. the 'hex' value is always and never preserved), so I'm interpreting your question as pertaining to printing to the screen.
You should be able to use a format specifier for that -- something like %0x.
The value that's returned from your -getUSBCode: method isn't two decimal digits, it's one eight-bit byte. Both "12" and "0x0C" are strings that represent that byte's value, so saying you want "0x0C" but don't want a string is a contradiction.
I can't imagine a "clean" or efficient method of doing this.
I would like to transform a string of numbers like #"1234" into an array of shorts for a calculator pet project*.
I think of getting substrings and then the intValue of the substring but it sounds cumbersome and overkill. Would there be a more elegant way of doing it?
Thanks in advance!
* I know that there are more efficient ways to do maths and plenty of C libraries do this but it's for my own education :-).
If I have this straight, you want to turn an NSString into a short array? This function assumes each character in the NSString is a separate short. Oh, and don't forget to free that array when you're done with it!
//Stephen Melvin <jinksys#gmail.com>
short *NSStringToShortArray(NSString *digits){
int count = [digits length];
short *shortArray = malloc(sizeof(short)*count);
for(int i = 0; i<count; i++){
shortArray[i] = (short)[digits characterAtIndex:i] - '0';
}
return shortArray;
}
A good calculator interface should take one string of characters as a single number then after the operation is selected it clears the field to accept the next number.
For example.
You type in 2 then hit the * button, the field clears and you type in 2 again and press = and get 4.
If you want the interface to accept an equation or a script then you should read up on parsing numeric equations.