简体   繁体   中英

Convert Large NSString in Hexadecimal to Decimal NSString iOS

As the title of the question states I'm looking to take the following string in hexadecimal base:

b9c84ee012f4faa7a1e2115d5ca15893db816a2c4df45bb8ceda76aa90c1e096456663f2cc5e6748662470648dd663ebc80e151d4d940c98a0aa5401aca64663c13264b8123bcee4db98f53e8c5d0391a7078ae72e7520da1926aa31d18b2c68c8e88a65a5c221219ace37ae25feb54b7bd4a096b53b66edba053f4e42e64b63

And convert it to its decimal equivalent string:

130460875511427281888098224554274438589599458108116621315331564625526207150503189508290869993616666570545720782519885681004493227707439823316135825978491918446215631462717116534949960283082518139523879868865346440610923729433468564872249430429294675444577680464924109881111890440473667357213574597524163283811

I've looked to use this code, found at this link :

unsigned result = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];

[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&result];
NSLog(@" %u",result);

However, I keep getting the following result: 4294967295 . Any ideas on how I can solve this problem?

This sounds like a homework/quiz question, and SO isn't to get code written, so here are some hints in hope they help.

Your number is BIG, far larger than any standard integer size, so you are not going to be able to do this with long long or even NSDecimal .

Now you could go and source an "infinite" precision arithmetic package, but really what you need to do isn't that hard (but if you are going to be doing more than this then such using a package would make sense).

Now think back to your school days, how were you taught to do base conversion? The standard method is long division and reminders.

Example: start with BAD in hex and convert to decimal:

BAD ÷ A = 12A remainder 9
12A ÷ A =  1D remainder 8
 1D ÷ A =   2 remainder 9
  2 ÷ A =   0 remainder 2

now read the remainder back, last first, to give 2989 decimal.

Long division is a digit at a time process, starting with the most significant digit, and carrying the remainder as you move to the next digit. Sounds like a loop.

Your initial number is a string, the most significant digit is first. Sounds like a loop.

Processing characters one at a time from an NSString is, well, painful. So first convert your NSString to a standard C string. If you copy this into a C-array you can then overwrite it each time you "divide". You'll probably find the standard C functions strlen() and strcpy() helpful.

Of course you have characters in your string, not integer values. Include ctype.h in your code and use the digittoint() function to convert each character in your number to its numeric equivalent.

The standard library doesn't have the inverse of digittoint() , so to convert an integer back to its character equivalent you need to write your own code, think indexing into a suitable constant string...

Write a C function, something like int divide(char *hexstring) which does one long division of hexstring , writing the result into hexstring and returning the remainder. ( If you wish to write more general code, useful for testing, write something like int divide(char *buf, int base, int divisor) - so you can convert hex to decimal and then back again to check you get the back to where you started. )

Now you can loop calling your divide and accumulating the remainders (as characters) into another string.

How big should your result string be? Well a number written in decimal typically has more digits than when written in hex (eg 2989 v. BAD above). If you're being general then hex uses the fewest digits and binary uses the most. A single hex digit equates to 4 binary digits, so a working buffer 4 times the input size will always be long enough. Don't forget to allow for the terminating NUL in C strings in your buffer .

And as hinted above, for testing make your code general, convert your hex string to a decimal one, then convert that back to a hex one and check the result is the same as the input.

If this sounds complicated don't despair, it only takes around 30 lines of well spaced code.

If you get stuck coding it ask a new question showing your code, explain what goes wrong, and somebody will undoubtedly help you out.

HTH

Your result is the maximum of unsinged int 32 bit, the type you are using. As far as I can see, in the NSScanner documentation long long is the biggest supported type.

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