简体   繁体   中英

Python pack / unpack converts to Objective C

I have one Python script and I wants to convert in objective C.

from struct import *
data = [10000,10000,10000,10]
d = [int(i) for i in data]
print d
list = unpack('BBBBBBBBBBBBBBBB',pack('>IIII', d[0], d[1], d[2], d[3]))
print list

Output

[10000, 10000, 10000, 10]
(0, 0, 39, 16, 0, 0, 39, 16, 0, 0, 39, 16, 0, 0, 0, 10)

I have did first int array conversion in objective C, But stuck at pack and unpack

Objective C for first part

NSArray *timouts = [NSArray arrayWithObjects:[NSString stringWithFormat:@"10000"],[NSString stringWithFormat:@"10000"],[NSString stringWithFormat:@"10000"],[NSString stringWithFormat:@"10"],nil];

    NSMutableArray *ary = [NSMutableArray arrayWithCapacity:4];
    NSInteger coutner = 0;
    for (NSString *string in timouts)
    {
        int outVal;
        NSScanner* scanner = [NSScanner scannerWithString:string];
        [scanner scanInt:&outVal];

        ary[coutner] = [NSString stringWithFormat:@"%d",outVal];
        coutner++;
    }

I have try to do so, But not much aware with Python scripting. Not got the way how pack and unpack works.

First of all I want to say that you should try to learn Objective-C on a better level. It is not too hard (esp. coming from Python, because both languages are dynamically typed). However, I will give you some advises to your Q.

Let's have a closer view to your code:

A.

You have:

NSArray *timouts = [NSArray arrayWithObjects:[NSString stringWithFormat:@"10000"],[NSString stringWithFormat:@"10000"],[NSString stringWithFormat:@"10000"],[NSString stringWithFormat:@"10"],nil];

I really do not see any benefit from converting all numbers to strings. Simply store numbers:

NSArray *timeOuts = @[@10000, @10000, @10000, @10];

@[] means "array literal", @x means NSNumber instance object.

B.

You can print out the list itself simply with NSLog() :

NSLog( @"%@", timeOuts );

C.

You have to read instances of NSNumber , because you stored such instances:

NSMutableArray * bytes = [NSMutableArray arrayWithCapacity:4];
for (NSNumber *value in timeOuts) // Take out numbers
{
…
}

D.

Now to the hardest part: unpacking

Because you stored instances of NSNumber into the array, it is pretty easy to get the integer value:

NSMutableArray *bytes = [NSMutableArray arrayWithCapacity:4];
for (NSNumber *value in timeOuts) // Take out numbers
{
   int intValue = [value intValue];
   …
}

E.

You can "pack them" into a string with -stringWithFormat: . However, if I understand the log in your Q correct, you want to print out the single bytes of a value, not the whole value.

NSMutableArray *bytes = [NSMutableArray arrayWithCapacity:4];
for (NSNumber *value in timeOuts) // Take out numbers
{
   int intValue = [value intValue];
   for( int x = 0; x < 4; x++ )
   {
     int byte = intValue & 0xFF000000; // Mask out bit 0-23
     [bytes addObject:@(byte)];  // Store byte
     intValue <<= 8;             // Shift up bit 0-23 to 8-31 for the next iteration

   }
}
NSLog( @"%@", bytes );

z.

So we end up with this:

NSArray *timeOuts = @[@10000, @10000, @10000, @10];
NSLog( @"%@", timeOuts );

NSMutableArray *bytes = [NSMutableArray arrayWithCapacity:4];
for (NSNumber *value in timeOuts) // Take out numbers
{
   int intValue = [value intValue];
   for( int x = 0; x < 4; x++ )
   {
     int byte = intValue & 0xFF; // Mask out bit 8-31
     [bytes addObject:@(byte)];  // Store byte
     intValue >>= 8;             // Shift down bit 8-31 to 0-23 for the next iteration

   }
}
NSLog( @"%@", bytes );

If you really need to store the values as strings, let me know. I will edit my answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM