Convert a String into an array with Keycodes - objective-c

today i started with a simple Project that should convert a string into an array with keycodes to simulate keystrokes.
My Problem is that i cant use a char as an id for a dictionary to convert that char into the keycode.
//Declare a Dictionary
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
//Add the Basic data for keycodes (i know that there are some missing)
[dictionary setObject:#"0x00" forKey:#"a"]; //a
[dictionary setObject:#"0x0B" forKey:#"b"]; // b
[dictionary setObject:#"0x08" forKey:#"c"]; //c
[dictionary setObject:#"0x02" forKey:#"d"]; // d
[dictionary setObject:#"0x0E" forKey:#"e"]; //e
[dictionary setObject:#"0x03" forKey:#"f"]; //f
[dictionary setObject:#"0x05" forKey:#"g"]; //g
[dictionary setObject:#"0x04" forKey:#"h"]; //h
[dictionary setObject:#"0x22" forKey:#"i"]; //i
[dictionary setObject:#"0x26" forKey:#"j"]; //j
[dictionary setObject:#"0x28" forKey:#"k"]; //k
[dictionary setObject:#"0x25" forKey:#"l"];//l
[dictionary setObject:#"0x2D" forKey:#"m"];//m
[dictionary setObject:#"0x2E" forKey:#"n"];//n
[dictionary setObject:#"0x1F" forKey:#"o"];//o
[dictionary setObject:#"0x23" forKey:#"p"];//p
[dictionary setObject:#"0x0C" forKey:#"q"];//q
[dictionary setObject:#"0x0F" forKey:#"r"];//r
[dictionary setObject:#"0x01" forKey:#"s"];//s
[dictionary setObject:#"0x11" forKey:#"t"];//t
[dictionary setObject:#"0x20" forKey:#"u"];//u
[dictionary setObject:#"0x09" forKey:#"v"];//v
[dictionary setObject:#"0x0D" forKey:#"w"];//w
[dictionary setObject:#"0x07" forKey:#"x"];//x
[dictionary setObject:#"0x10" forKey:#"y"];//y
[dictionary setObject:#"0x06" forKey:#"z"];//z
NSString *workwith = [_mess stringValue]; //Get String from Interface
long stringl = [workwith length]; //Get lenght of the String
int a = 0;
char text[stringl-1]; //make a array with the lenght of the string
while (a < stringl) { //fill this array with chars from the string
text[a] = [workwith characterAtIndex:a];
NSLog(#"%c",text[a]);
a++;
}
char fmat[stringl-1]; //make a second char to fill it with keycodes
int dnehmen = 0;
while (dnehmen <= stringl-1) {
fmat[dnehmen] = [dictionary objectForKey:#"%c",text[dnehmen]]; //stuck at this point
}
At the last line i try to use a char from the first array as an id for the dictionary to get the keycode for the second array.
I get an error that i cant use a char as an id and so i need a was to get the keycode from a char.
(I read some Posts on this site but i didn`t really understood what they´ve done)
Thanks for your answers
Tim

The error you report is down to the line:
fmat[dnehmen] = [dictionary objectForKey:#"%c",text[dnehmen]];
as #"%c",text[dnehmen] does not return an object but a char value due to the use of the rather obscure C comma operator - a comma is usually a separator but is also an operator (like + etc.) which just returns its second argument. So you get an error referring to characters...
Given that you've used NSString values as keys what you need is a string containing your character and you probably meant to type:
fmat[dnehmen] = [dictionary objectForKey:[NSString stringWithFormat:#"%c",text[dnehmen]]];
While that will fix your first problem you now have another, your dictionary returns an NSString containing four characters and your code suggests you'd like the byte (char) represented by those four characters when interpreted by a compiler as a hexadecimal literal...
At this point you might be tempted to try to convert the string into a byte value by parsing it as a hexadecimal number, but you shouldn't. Instead look at putting the value you need into the NSDictionary in the first place. Both the objects any keys used in an NSDictionary can be any [1] type of object. You might want to consider using NSNumber objects for both.
Hopefully that will set you off on the right track in fixing your design.
[1] Well almost any to be precise, any which have equality and hash methods - which in practice is everything that descends from NSObject so don't worry about this pedantic detail now!

Related

Conversion from NSString to Hex

i am new in this field and i was working on a conversion of NSString to Hex and have been stuck into it. My String lets suppose is 1,FF,F8 now how can i convert that into hex numbers like 0x01,0x0FF and 0x0F8
First step would be to split the string containing "1,FF,F8" into three strings containing the separate hex values, "1", "FF", "F8".
NSString *hexString = #"1,FF,F8";
NSArray *hexValues = [hexString componentsSeparatedByString:#","];
As for the conversion from NSString to hex, I'm not quite sure what exactly you want.
If you just want to add on a "0x0" to the beginning of the hex values, you can just do:
NSMutableArray *formattedHexValues = [NSMutableArray array];
for(NSString *hexValue in hexValues) {
[formattedHexValues addObject:[NSString stringWithFormat:#"0x0%#", hexValue]];
}
If you want to actually get the integer value of the hex string, do this:
for(NSString *hexString in formattedHexValues) {
unsigned int value;
[[NSScanner scannerWithString:hexString] scanHexInt:&value];
NSLog(#"The value is %d", value);
}
Typed this out in the browser so there might be a syntax mistake or two, but it generally should work fine.

discussing functionality of id's stringValue function?

I got a NSMutableArray object with int values
and I can get a certain value via :
int *v0=[[[arrayObj objectAtIndex:0] intValue];
there is no problem.
But
I got a NSMutableArray object with NSString values
and I cannot get a certain value via :
NSString *v0=[[[arrayObj objectAtIndex:0] stringValue];
//raises error
I want to learn and understand exactly what stringValue for... and why this error occurs ?
NSString *v0=[arrayObj objectAtIndex:0];
works as expected.I asusme its some kind of pointer with null terminated so it can leech value.
Im not sure this line is also unicode/encoded string safe code.
in conclusion:
want to know the purpose of stringValue with some lines o code snippets
I got a NSMutableArray object with int values
That's not possible, Cocoa arrays always contain objects. You probably have an array of NSNumber objects that wrap the integers, like:
NSArray *arrayOfNumbers = #[#1, #2, #3];
NSNumber objects have an intValue method, so this works:
int value = [arrayOfNumbers[0] intValue];
On the other hand when you have an array of strings ...
NSArray *arrayOfStrings = #[#"1", #"2", #"3"];
... you want to access individual elements directly, without converting the string object to something else:
NSString *element = arrayOfStrings[0];
NSString objects do not understand the stringValue method:
[arrayOfStrings[0] stringValue]; // crash: does not recognize selector
Back at the beginning, our NSNumber objects from the first array do understand stringValue. You can use it to convert the number to a string:
NSString *intString = [arrayOfNumbers[0] stringValue];
To make the confusion perfect, NSString also understand the intValue message:
int value = [arrayOfStrings[0] intValue];
Here intValue means to try to convert the string to a plain C int value.
The error you will be getting (but failing to post with your question) will be Unknown selector sent to instance and this is because NSString doesn't have a stringValue method.
The approach you suggest is correct:
NSString *v0 = [arrayObj objectAtIndex:0];
EDIT (prompted by #Answerbot's answer):
The reason you are confused is that [NSString intValue] is used to convert the string value to an integer, as long as the string represents an integer (i.e. #"123"). However you don't need this for string as the object is already a string. It's therefore not provided.

How to append NSString wiht number?

I'm new in Cocoa.
I have NSString - (e.g) MUSIC . I want to add some new NSString in Array,
And want to check something like this
if MUSIC already contained in Array, add Music_1 , after Music_2 and so on.
So I need to be able read that integer from NSString, and append it +1 .
Thanks
Use
NSString *newString = [myString stringByAppendingString:[NSString stringWithFormat:#"_%i", myInteger]];
if myString is "music", newString will be "music_1" or whatever myInteger is.
EDIT: I seem to have gotten the opposite meaning from the other answer provided. Can you maybe clarify what it is you are asking exactly?
Check it out:
NSMutableArray *array = [NSMutableArray arrayWithObjects:#"123", #"qqq", nil];
NSString *myString = #"MUSIC";
NSInteger counter = 0;
if ([array containsObject:myString]){
NSString *newString = [NSString stringWithFormat:#"%#_%d", myString, ++counter];
[array addObject:newString];
}
else
[array addObject:myString];
For checking duplicate element in Array you can use -containsObject: method.
[myArray containsObject:myobject];
If you have very big array keep an NSMutableSet alongside the array.Check the set for the existence of the item before adding to the array. If it's already in the set, don't add it. If not, add it to both.
If you want unique objects and don't care about insertion order, then don't use the array at all, just use the Set. NSMutableSet is a more efficient container.
For reading integer from NSString you can use intValue method.
[myString intValue];
For appending string with number you can use - (NSString *)stringByAppendingString:(NSString *)aString or - (NSString *)stringByAppendingFormat:(NSString *)format ... method.
Here's how you convert a string to an int
NSString *myStringContainingInt = #"5";
int myInt = [myStringContainingInt intValue];
myInt += 1;
// So on...

Converting NSString to key value pair

I have a response from server which is NSString and looks like this
resp=handshake&clientid=47D3B27C048031D1&success=true&version=1.0
I want to convert it to key value pair , something like dictionary or in an array .
I couldn't find any useful built-in function for decoding the NSString to NSdictionary and replacing the & with space didn't solve my problem , can anyone give me any idea or is there any function for this problem ?
This should work (off the top of my head):
NSMutableDictionary *pairs = [NSMutableDictionary dictionary];
for (NSString *pairString in [str componentsSeparatedByString:#"&"]) {
NSArray *pair = [pairString componentsSeparatedByString:#"="];
if ([pair count] != 2)
continue;
[pairs setObject:[pair objectAtIndex:1] forKey:[pair objectAtIndex:0]];
}
or you could use an NSScanner, though for something as short as a query string the extra couple of arrays won't make a performance difference.

iPhone - Splitting an NSString into char NSStrings

I've got a 2d NSArray of {{"foo","food only only"}, {"bar","babies are rad"} ... } and I need to end up with 2 NSArrays: one of characters and one of the corresponding words. So #"f", #"o",#"b",#"a",#"r" and #"food",#"only",#"babies",#"are",#"rad" would be my two NSArray's of NSStrings.
So first, how do I get #"f",#"o",#"o" from #"foo"
And second how can I only keep the uniques? I'm guessing NSDictionary and only add if key is not there giving me #"f":#"food" #"o":#"only" then use getObjects:andKeys: to get two C arrays which I'll convert to NSArrays..
Based on the below answer I went with the following. I didn't actually use the NSMutableDict, I just added my letters to it to get the uniqueness check before creating my 2 output arrays:
unichar ch = [[arr objectAtIndex:0] characterAtIndex:i];
NSString *s = [NSString stringWithCharacters: &ch length: 1];
if (![dict objectForKey:s]) {
}
getCharacters will get you started with an array of characters: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/getCharacters:
You could cycle through and check for uniques after
If you want the individual characters of a string, try -characterAtIndex:. That will get you them as the unichar primitive type, which you can then wrap in NSString like so:
unichar ch = ...;
NSString *chString = [NSString stringWithCharacters: &ch length: 1];
To keep uniques, you can store objects in an NSMutableSet, though it will not preserve the order in which objects are added to it.