iosobjective-cswiftuint

UInt8 from Swift to Objective-C


I need a clarification. I'm following a tutorial on Hash sha256 in Swift and my app is in Objective-C. I have trouble translating this UInt8 in Objective-C. What is the equivalent in Objective-C?

let rsa2048Asn1Header:[UInt8] = [
        0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
        0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00
    ]

I tried this but I'm not sure it's right I'd like to have confirmation

UInt8 rsa2048AsnlHeader[] = {
         0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
         0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00};

Solution

  • Yes what you posted above would work if you're just looking to use a primitive array in Objective-C.

    Another option would be to use NSArray, shown below:

    NSArray<NSNumber *> *rsa2048AsnlHeader = @[
        @0x30, @0x82, @0x01, @0x22, @0x30, @0x0d, @0x06, @0x09, @0x2a, @0x86, @0x48, @0x86,
        @0xf7, @0x0d, @0x01, @0x01, @0x01, @0x05, @0x00, @0x03, @0x82, @0x01, @0x0f, @0x00
    ];
    

    To get the equivalent UInt8 value from the NSNumber elements, use the unsignedCharValue property of the NSNumber class (which is equivalent to uint8Value in Swift). For example:

    for (NSNumber *value in rsa2048AsnlHeader) {
        UInt8 uint8Value = value.unsignedCharValue;
        // ...
    }
    

    Source