Converting SHA1 hash Hex string to decimals using objective C on iPhone - ios

I am a beginner to developing apps on the iPhone and I am trying to convert a 40 character SHA1 hash in hex to decimals. i have been looking around and the largest integer type in unsigned long long but it's not enough. I tried looking at NSDecimalNumber but it doesn't have Hex function.

Well I guess there is no way of doing so. I would have to change it to a string instead.

Related

How to convert hexadecimal NSData into decimal NSData?

How to convert long hexadecimal number371d2a3f041fb4ab210fe7567b3a041f9072089790f9b34139fb2df7301ebcad
into long decimal number 24928736809215721890296636138640601514085104766215587836409400141363575438509 in iOS?
What you are looking for is a type that can store an arbitrary-sized integer. There is no native type for this in Foundation, but a Google search for "bigint in objective-c" should give you some ideas.

How to cast hex string as hex integer?

I'm using BLE plugin, which requires to present the values as list of hex number(e.g. [0x20,0x11,0x06,0x6D]).
I was able to get a list of hex strings using int.toRadixString (['0x20','0x11','0x06','0x6D']), but didn't find a way to convert those strings into hex integers.
I'm looking for something like int demicalToHex(int demical) or similar function.
Thanks in advance.
There's no such thing as a hex number. What it needs is a list of integers (List<int>).
In the same way that you have [0x20,0x11,0x06,0x6D] you could equally write that as [32, 17, 6, 109]. Sometimes, it's convenient to think about integers in their decimal representation or their hex representation (or octal, or binary...), but as far as the compiler is concerned, they are just integers.
Just use an array of integers (not strings).

Swift - Convert string to double without scientific notation(exponents)

I have a string which is in exponents. For example my string is
"-4.17182640083098e-31"
I have an array of values like this. I want to use this to draw chart.
The graph accepts double values only to plot it. When I convert it to double using Double(str) and using it in graph is not working because the graph library not accepting exponents.
So I converted this to decimal value the result is
-0.0000000000000000000000000000011803449813502
So I again converted this decimal to double it gives the same -4.17182640083098e-31 with exponents notation.
So can't use this is graph. So please suggest a way to convert this to double without the exponents/scientific notations. Thanks in advance.

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 (e.g. 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.

Translating memory contents into a string via ASCII encoding

I have to translate some memory contents into a string, using ASCII encoding. For example:
0x6A636162
But I am not sure how to break that up, to be translated into ASCII. I think it has something to do with how many bits are in a char/digit, but I am not sure how to go about doing so (and of course, I would like to know more of the reasoning behind it, not just "how to do it").
ASCII uses 7 bits to encode a character (http://en.wikipedia.org/wiki/ASCII). However, it's common to encode characters using 8 bits instead (note that technically this isn't ASCII). Thus, you'd need to split your data into 8-bit chunks and match that to an ASCII table.
If you're using a specific programming language, it may have a way to translate an ASCII code to a character. For instance, Ruby has the .chr method, Python has the chr() built-in function, and in C you can printf("%c", number).
Note that each nibble (4 bits) can be represented as one hexadecimal digit, so for the sample string you show, each 8-bit "chunk" would be:
0x6A
0x63
0x61
0X62
the string reads "jcab" :)

Resources