user1501354
user1501354

Reputation: 151

Convert NSString to unsigned in Objective C

I want to convert an NSString to an unsigned int.

Why? Because I want to do parallel payment in PayPal.

Below I have given my coding in which I want to convert the NSString to an unsigned int.

My query is:

    //optional, set shippingEnabled to TRUE if you want to display shipping
    //options to the user, default: TRUE
    [PayPal getPayPalInst].shippingEnabled = TRUE;
    
    //optional, set dynamicAmountUpdateEnabled to TRUE if you want to compute
    //shipping and tax based on the user's address choice, default: FALSE
    [PayPal getPayPalInst].dynamicAmountUpdateEnabled = TRUE;
    
    //optional, choose who pays the fee, default: FEEPAYER_EACHRECEIVER
    [PayPal getPayPalInst].feePayer = FEEPAYER_EACHRECEIVER;
    
    //for a payment with multiple recipients, use a PayPalAdvancedPayment object
    PayPalAdvancedPayment *payment = [[PayPalAdvancedPayment alloc] init];
    payment.paymentCurrency = @"USD";
    
    // A payment note applied to all recipients.
    payment.memo = @"A Note applied to all recipients";
    
    //receiverPaymentDetails is a list of PPReceiverPaymentDetails objects
    payment.receiverPaymentDetails = [NSMutableArray array];
    
    NSArray *emailArray = [NSArray arrayWithObjects:@"[email protected]",@"[email protected]", nil];
    
    for (int i = 1; i <= 2; i++) {
        PayPalReceiverPaymentDetails *details = [[PayPalReceiverPaymentDetails alloc] init];
        
        // Customize the payment notes for one of the three recipient.
        if (i == 2) {
            details.description = [NSString stringWithFormat:@"Component %d", i];
        }
        
        details.recipient = [NSString stringWithFormat:@"%@",[emailArray objectAtIndex:i-1]];
        
        unsigned order;
        
        if (i==1) {
            order = [[feeArray objectAtIndex:0] unsignedIntValue];
        }
        if (i==2) {
             order = [[amountArray objectAtIndex:0] unsignedIntValue];
        }
        
        //subtotal of all items for this recipient, without tax and shipping
        details.subTotal = [NSDecimalNumber decimalNumberWithMantissa:order exponent:-4 isNegative:FALSE];
        
        //invoiceData is a PayPalInvoiceData object which contains tax, shipping, and a list of PayPalInvoiceItem objects
        details.invoiceData = [[PayPalInvoiceData alloc] init];
        
        //invoiceItems is a list of PayPalInvoiceItem objects
        //NOTE: sum of totalPrice for all items must equal details.subTotal
        //NOTE: example only shows a single item, but you can have more than one
        details.invoiceData.invoiceItems = [NSMutableArray array];
        PayPalInvoiceItem *item = [[PayPalInvoiceItem alloc] init];
        item.totalPrice = details.subTotal;
        [details.invoiceData.invoiceItems addObject:item];
        
        [payment.receiverPaymentDetails addObject:details];
    }
    
    [[PayPal getPayPalInst] advancedCheckoutWithPayment:payment];

Can anybody tell me how to do this conversion?

Upvotes: 4

Views: 6315

Answers (3)

Michael
Michael

Reputation: 519

In my case I had to implement something like this (adapt to your needs):

NSString *myStringObject = [NSString stringWithFormat:@"%u", someObject.hash];
NSUInteger hashValue = (NSUInteger)myStringObject.longLongValue;

I originally used myStringObject.intValue, as suggested below, but then my integers were getting "clipped" because they were too big when assumed signed (but not too big unsigned due to the extra bit). Using myStringObject.longLongValue fixed the problem for me.

I should note that my strings were originally created using NSUIntegers (unsigned ints) so I didn't have to worry about parsing an integer that was too big.

Upvotes: 3

trojanfoe
trojanfoe

Reputation: 122458

There is no conversion to unsigned integer, however there is a conversion to signed integer (NSInteger):

NSInteger order = [[freeArray objectAtIndex:0] integerValue];
NSLog(@"order=%ld", order);

or int:

int order = [[freeArray objectAtIndex:0] intValue];
NSLog(@"order=%d", order);

Upvotes: 1

trumpetlicks
trumpetlicks

Reputation: 7085

Assuming your array has a string in it:

unsigned int order = (unsigned int)[[feeArray objectAtIndex:0] intValue];

There really is no NSString method to convert directly to unsigned int values. However if you can guarantee that none of your values are signed ints, then the above should work for you.

Upvotes: 2

Related Questions