Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4060192
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:18:24+00:00 2026-05-20T15:18:24+00:00

I’m trying to covert this bit of C code to Cocoa and I’m struggling

  • 0

I’m trying to covert this bit of C code to Cocoa and I’m struggling to figure out how.

  char *deskey = "123 456 789 101 112 131 415 161";
  unsigned char key[16];
  memset(key, 0, sizeof(key));
  sscanf(deskey, "%o %o %o %o %o %o %o %o",
    (int*)&key[0], (int*)&key[1], (int*)&key[2],
    (int*)&key[3], (int*)&key[4], (int*)&key[5],
    (int*)&key[6], (int*)&key[7]);

I’ve tried using NSMutableArray and NSData but having no luck. I was able to scan the string and pull out the numbers, but I’m not sure how to store into NSData after that.

  NSMutableArray *enckey = [[[NSMutableArray alloc] init] autorelease];
  NSScanner *scanner = [NSScanner scannerWithString:self.deskey];
  int pos = 0;

  while ([scanner isAtEnd] == NO) {
    if ([scanner scanInt:&pos]) {
      [enckey addObject:[NSString stringWithFormat:@"%o", pos]];
    }
    else {
      NSLog(@"Your DES key appears to be invalid.");
      return;
    }
  }

Basically trying to convert ascii DES key to string to use for Triple DES encryption. Any help is greatly appreciated, thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:18:25+00:00Added an answer on May 20, 2026 at 3:18 pm

    @Keenan “I was hoping to avoid using sscanf and char* in place of the Cocoa classes”. Well you can do that, but what are you hoping to produce? If you want a byte array as the result then you need to stick to unsigned char[], and that begs the question why you’d do the parsing on NSString in the first place.

    Here is an Objective-C translation of your C code. Note that octal is seen as ancient history by Cocoa so its parsing classes only deal with decimal and hexadecimal, so you need to write your own or use a standard C function (strtol below).

    This example produces both a unsigned char[] and an NSMutableArray – pick one.

    // There are no checks in the code, like in the original...
    // BTW 789 is not an octal number...
    NSString *descKey = @"123 456 789 101 112 131 415 161";         //    char *deskey = "123 456 789 101 112 131 415 161";
    // pick one...
    NSMutableArray *keyObjC = [NSMutableArray new];                 //    unsigned char key[16];
    unsigned char keyC[16];
    //    memset(key, 0, sizeof(key));
    
    // As @JeremyP has pointed out the sscanf is wrong as %o produces a 4-byte value and you only want a 1-byte one.
    // In C you would therefore need key to be an array of ints and then assign each element to a byte (unsigned char),
    // or parse a different way.
    
    unsigned ix = 0;    // for keyC choice only
    NSArray *numbers = [descKey componentsSeparatedByString:@" "];  //    sscanf(deskey, "%o %o %o %o %o %o %o %o",
    for (NSString *aNumber in numbers)                              //           (int*)&key[0], (int*)&key[1], (int*)&key[2],
    {                                                               //           (int*)&key[3], (int*)&key[4], (int*)&key[5],
                                                                    //           (int*)&key[6], (int*)&key[7]);
        unsigned char next = (unsigned char)strtol([aNumber UTF8String], NULL, 8);
        keyC[ix++] = next;                                          // for keyC choice
        [keyObjC addObject:[NSNumber numberWithUnsignedChar:next]]; // keyObjC choice
    }
    

    If you want to approach the one line of your Python just compress the iteration to:

    for (NSString *aNumber in [descKey componentsSeparatedByString:@" "]) { [keyObjC addObject:[NSNumber numberWithUnsignedChar:(unsigned char)strtol([aNumber UTF8String], NULL, 8)]]; }
    

    but it is still longer of course!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.