change the ascii code, problem.

Discussion in 'Mac Programming' started by MNiazmand, Jun 4, 2011.

  1. macrumors newbie

    Joined:
    Jun 4, 2011
    #1
    i've found myself in trouble. could you please do me a favor? how can i make myself assure about my program, that if the user presses the 'a' key, it's ascii code change to the one that programer wants and then show it in the NSTextField. thanks for your attention.

    example:

    [​IMG]
     
  2. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #2
    Man, there seems to be a sudden influx of "simple" encryption threads all of a sudden.

    This appears to be a character substitution cypher.

    the "ll" -> "ee" suggests a one to one mapping.

    However the difference between l and e, and e and b are not the same, so it suggests something more sophisticated than a simple C = P + a mod 26
     
  3. thread starter macrumors newbie

    Joined:
    Jun 4, 2011
    #3
    Oops!

    My problem is :

    1 - how to mapping keyboard
    2 - change ascii code when key-pressed { on key pressed in NSTextField }
    3 - real-time changing. {run time}
     
  4. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #4
    Making the assumptions that 'hello' becomes 'HELLO' for the purposes on the cypher, I did some quick math using the 'E'->'B' and 'L'->'E' assuming
    C = aP + b mod 26

    I ended up with
    C = 19 * P + 3 mod 26


    Which will make 'E'->'B' and 'L'->'E', but not 'H'->'A' and 'O'->'Y'

    So I would need more information about the way this is programmed, or at least a good deal more data, say capital letters in the input.

    But to be fair I didn't even really understand your question, I just assumed you wanted to reverse engineer this cypher/program.
     
  5. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #5
    Then I assume you have some kind of 1:1 mapping already written, say in a lookup table.

    NSTextField has a delegate method that looks promising -controlTextDidChange

    Implement that to either:
    1) Copy the contents of the textfield into a NSMutableString, and then walk through each character and lookup then replace the character. Display this string in the other textField.

    2) Get the last character of the current string in the textField, and do the lookup and then append it to the string in the other textField.

    Either option would be pretty trivial, but option 2 is more efficient.
     
  6. thread starter macrumors newbie

    Joined:
    Jun 4, 2011
    #6

    thx mr jared_kipe

    but : i want work with ascii code . because my language not english and
    I'm switching keys pressed characters in the English language with my language ASCII .

    How did this act ?
    I'm grateful for the example bring.

    thx for help.

    btw my language is persian .;)
     
  7. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #7
    ASCII is an ancient and very limited code with only 128 characters. It is not even enough to write proper English, for example words like naïve, names like Zoë, proper “quote” characters, they all cannot be written in ASCII. And forget about writing Persian with ASCII characters (that's Arabic with a few extensions, if I remember right).

    Go to System Preferences, "Language and Text", "Input Sources". At the top of the list, turn on "Keyboard and Character Viewer". That gets you the "Character Viewer" in the menubar. Check it out to get some idea about the range of different characters that are available. You should also enable a Persian keyboard, so you can type text like ضصثقف غعهخح شسیبل اتنم ظطذدز رو.

    Then go to www.unicode.org to learn about Unicode, which is what any reasonable person (and all of the Cocoa code) uses to represent text, written in _any_ language. Any text field in Cocoa handles entering Persian text automatically.
     
  8. thread starter macrumors newbie

    Joined:
    Jun 4, 2011
    #8
    thx for help .

    but i don't add a new keyboard .

    i want writing with especial font of my language.
    my font is not standard keyboard layout ,
    So
    I have to switch characters entered
    Fits with the Persian characters .

    Now what should be ???????:(:(:(
     
  9. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #9
    All of the above is true. HOWEVER NSStrings are UTF, thus you can and should be able to pull the above description off going from ASCII characters into persian.

    NSString's -characterAtIndex: returns a unichar which is an unsigned short. So will probably not return what you expect for multi byte characters like π and ö, but there is NO reason you can't implement lookups/replaces from US-keyboard entries like 'a' -> 'å' (whatever persian character you want)

    Just use NSMutableString's -appendString: @"å" or -replaceCharactersInRange: lastCharacterRange withString: @"å"
    , for 1) and 2) respectively.
     
  10. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #10
    Ok...

    Both the proposed ideas (previously referred to as 1) and 2)) will do exactly what you are looking to do.

    Is your screenshot a mockup or something then?
    Code:
    ....
    IBOutlet NSTextField *inputField;
    IBOutlet NSTextField *outputField;
    ....
    
    - (void)controlTextDidChange: (NSNotification *)aNotification {
         //psudo code
         stringIn = inputField string
         stringOut = new NSMutableString
         for each character in string {
              substitute =  replacement for character
              stringOut append substitute
         }
    
         outputField set to stringOut
    }
    
    EDIT: Also, this implementation of 1) would work pretty well for a file or some other string/data input too. You just need to make a function/method to do the replacement for character lookup.
     
  11. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #11
    Well, I managed to enter arabic characters into Safari without problems, and I can enter arabic characters into any text field in any Cocoa application without problems, just by switching the keyboard, no programming needed. All the characters are available in the Arial, Courier, Times New Roman, Microsoft Sans Serif, Tahoma, DejaVu, or in specialzed fonts like Al Bayan, Baghdad, Geeza Pro, KufiStandard, Nadeem.

    If you insist on doing this by hand, use the manuals from www.unicode.org or the "Character Viewer" that you can turn on in the menubar to find out which codes to use. And it is Unicode, not ASCII.
     
  12. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #12
    I read it as he has a font that makes "a" look like some persian character, so he just replace "a" with "b" because the fonts glyph of "b" is what he wants to show up when he types "a"
     
  13. thread starter macrumors newbie

    Joined:
    Jun 4, 2011
    #13
    Jared's
    Thanks for your help.
    could i have your mailing address, please ?
    it would be very nice to talk with you more about my problem.

    Do not you think that the control key and press
    ASCII code switching is the best solution.?

    by the way its my mailing address :
    niazmand@msn.com || my.niazmand@aol.com
     
  14. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #14
    I'll email you later today probably.

    Well since you never mentioned the control key before I am once again in the dark as to what you are trying to accomplish.

    Out of curiosity do you happen to speak french better than english?

    I think maybe you've made some assumptions as to how the problem should be solved. Maybe write a detailed explanation of what the problem is, and let us think about some solutions.
     
  15. thread starter macrumors newbie

    Joined:
    Jun 4, 2011
    #15
    i'm waiting for you .
     

Share This Page