How to use hex counting in filenames for programmatic loading? - objective-c

I have several directories of 12 .caf files and am loading them programmatically:
NSString *soundToPlay = [NSString stringWithFormat:#"sounds/%d/%d_%d.caf", type, note, timbre];
If I want to, say, increment from 9 to 10 in one of those values, suddenly my string is an extra character long, which makes it harder to manipulate later with something like NSMakeRange. I'd like to keep all these %ds to a single character.
What I'd like to do is name my files using the digits 0-9 but then continue with A, B, C instead of 10, 11, 12. This would keep everything single-character. I'm hoping there's an easy way to do this kind of thing, still allowing stuff like increment, +/-, and modulo. If so, what is it?

X is the hexadecimal format specifier:
[NSString stringWithFormat:#"sounds/%X/%X_%X.caf", type, note, timbre]
Alternatively you could always use two digit numbers. That would allow to select from more than 16 cases:
[NSString stringWithFormat:#"sounds/%02d/%02d_%02d.caf", type, note, timbre]

Related

Method converts strings in Objective-C

I'm new to objective-c and I need help with this.
A phone number in this country has 10 digits like 6195946191 or 619JYDN191. It is hard to read a phone number formatted like that. One common format is 619-594-6191. Add the method phoneFormat to the NSString class. The method phoneFormat converts strings like
#"6195946191", #"619 594 6191", #"619 5946191" and #"619-594-6191" to #"619-594-6191".
That is all the methods below will return #"619-594-6191"
[#"6195946191" phoneFormat]
[#"619 594 6191" phoneFormat]
[#"619 5946191" phoneFormat]
[#"619-594-6191" phoneFormat]
Can anyone show me how to do this?
Do it in two stages. First remove all undesirable characters (everything that is not a digit? depends what input you are expecting). Personally, I would probably use NSScanner for this, though I can think of other ways.
Now you are guaranteed you have 10 digits in succession, so insert a hyphen after the 3rd and 6th characters and you're done.

Comparing NSString to NSTextView Range prior to Appending

Coding in Objective-C, I'm appending text to a NSTextView object named subCap in my code like so:
[[[_subCAP textStorage] mutableString]appendString:[NSString stringWithFormat:#"%#", subcapLine]];
subcapLine will have two timecode values such as: "01:00:00:00 01:00:01:00" separated by a single space, then a newline (\n) character, then a string like "ONC314_001_001" followed by two newline chars (\n\n).
The end result will create a list similar to:
01:00:00:00 01:00:01:00
ONC314_001_001
01:00:01:00 01:00:02:00
ONC314_001_002
01:00:02:00 01:00:03:00
ONC314_001_003
etc, etc, etc.
It's a sub caption file for placing text (the ONC314 lines) at appropriate times in a video file, as indicated by the timecodes.
However, I've determined that there is an odd set of circumstances where a timecode pair could be the same as the previous timecode pair, and if that happens, I want to skip appending that line.
So, my question is, given that the timecodes are always 11 chars apiece, separated by a space, can anybody think of a way I can easily grab the prior TC pair and compare it to my current pair in the subcapLine I'm preparing to append? The problem is the text of the sub caption could be random lengths. In my example they're the same, but that isn't always the case.
If I need to check prior to compiling my subcapLine, I can do that too, but I just thought it might be more slick to use a range of some sort to grab the prior pair of TCs from the last-written line in the NSTextView object and compare (again, using a range?) against the TCs in the line I'm about to append?
Thoughts and suggestions much appreciated.
Chris Conlee
When you add a timecode store the length of the text field string just before you add the timecode so you will have the offset to the timecode you are about to add.
Then before adding a new timecode you could simply use the previous offset you stored to extract the substring and do a string comparison and see if the timecodes are identical.
This should allow you to always have an offset to the previous timecode regardless of the length of the subtitles.

How can I get the actual value of the char instead of the int value

So at first i was trying to use character at index, then convert it to nsnumber, and then get the int value, but for 9 i got a value of 57. So I knew what was going wrong, I'm getting the int of the character itself.
So I read a little, and found atoi, but I get this error, that doesnt crash my app jsut pauses it.
My code is:
int current = atoi([startSquares characterAtIndex: i]);
Now startSquares is a big string full of numbers, and this above line is in a for loop where i goes from 0 to 99.
57 is ASCII for the digit '9'. Assuming that by the "value of the char" you mean "the numeric value of the digit the char represents", you can use the simple trick available in ASCII:
int digit = char - '0';
This trick works, because all digits are encoded in order starting with the digit zero (ASCII code 48). So when you subtract '0' (which is another way to write 48) from 57, you get 9, the value of the digit '9'.
This is a bad design, you should use an int array to hold your squares.
But if you absolutely insist on sticking with your approach, dasblinkenlight's way is the way to go. Just subtract the int value of char '0' from the char that you read.
If you have a character c '9' and you want the numeric value 9, you can use c - '0'. It isn't clear that this is what you want, though.
If you have an array of char that contains a series of numbers (of more than one digit), you need to advance a pointer through that array, and then you could call atoi with that pointer, when it points at a digit (see isdigit), or you could use sscanf, or, you could put it in an NSString and get the next number using intValue. But that would give you an NSInteger, not an NSNumber. I don't think you really want an NSNumber, since you can't directly take the square of one.

Trouble converting Dec to Hex (Int to Char)

i know this seems to be a stupid question, but i'm really getting trouble here.
I'm working in a project where i have some functions i can´t modify. That is, i got some C functions (not really my speciality) inside my Obj. C code that i can modify.
So here it is... to explain a little what i have.
I'm receiving a NSData like "\xce\x2d\x1e\x08\x08\xff\x7f" . I have to put each \hex code in a char array, like this:
cArray[1]=ce;
cArray[2]=2d;
cArray[3]=1e;
cArray[4]=08;
etc, etc... of course not LIKE THIS, but just so you understand. My initial move was to separe the NSData with subdataWithRange: and fill in an array with all "subdata". So the next move could be passing each position of that array to a char array, and that's where i got stuck.
I'm using something like (dont have my code right now)
for(int i=0 ; i<=64 ; i++) {
[[arrayOfSubData objectAtIndex:i] getBytes:&charArray[i]];
}
To fill the char array with the hex from my array of subData. That works almost perfectly. Almost.
Taking that example of cArray, my NSLog(#"pos%i: %x",i,charArray[i]) would show me:
pos1: ce
pos2: 2d
pos3: 1e
pos4: 8
And all the "left zeros" are supressed in that same way. My workaround for the moment (and i´m not sure if it is the best practice here) is to take my subDataArray and initWithFormat: a string with it. With that i can transform the string to an int with NSScanner scanHexInt:, but then i´m stucked again when converting back my decimal int to a hexadecimal CHAR. What would be the best approach to fill my char array that way?
Any help or some "tough love" will be greatly appreciated. Thanks
According to the normal rules of printf formatting (which NSLog follows also) you want the following:
NSLog(#"pos%i: %02x", i, charArray[i]);
The '0' says to left pad with 0s and is a flag. The '2' says to ensure that output for that field is at least two characters. So that'll ensure that at least two characters are output and pad to the left with '0's in order to fill space.

Store an NSString as a fixed length integer?

having a bit of trouble finding a solution to this.
I want to take a large ordered text file of words and create - in the same order - a text file of fixed length numeric values.
For example:
Input File Output File
AAA -> 00000001
AAH -> 00002718
AAZ -> 71827651
Initially it seemed a hash function would do the trick. However they are one way. Also perhaps they are a bit "heavyweight" for this. After all, I don't need any cryptography. Plus, it's a reference file. It will never change.
Any compression is a bonus not essential. That said, I don't want the file to get any bigger than it already is. Which is why I don't just want to write out the words as text but with fixed lengths.
So, bottom line; input is a NSString of variable length, output is an integer of fixed length. And, I must be able to take the integer and figure out the string.
Any help much appreciated!
Thanks!
xj
Well, this would be a bit of a brute force method, but here's my guess.
Start by making a custom function to convert one letter of text to an integer less than 100. (I'm not sure if such a function already exists, if so then great!) You might need to just go to stuff like "if ([input isEqual: #"a"]){ return 1;}
Then, run that function on each letter of text, and get the final integer by combining the previous results.
For example:
int myVal1 = [intConverter firstLetter];
int myVal2 = [intConverter secondLetter];
int myVal3 = [intConverter thirdLetter];
int finalValue =100^3 + 100^2*myVal1 + 100*myVal2 + myVal3;
Then, finalValue would be of the form 1(myVal1)(myVal2)(myVal3), which is what I think you're looking for.
To get back the original string, simply use the mod (%) and division functions to get the individual values back, then run the intConverter function backwards. (This would probably mean writing a new function that basically runs those if statements in reverse, but oh well.)
I hope this helps.