Hexa Decimal conversion - iphone

How to convert integer into hexadecimal value programmatically in iphone?

Here's one way:
int myInteger = 12345;
NSString* myString = [NSString stringWithFormat:#"%x", myInteger];
myString will contain what you've asked for, so it may help to elaborate
on how you need to use the result.

Related

Objective-C character encoding - Change char to int, and back

Simple task: I need to convert two characters to two numbers, add them together and change that back to an character.
What I have got: (works perfect in Java - where encoding is handled for you, I guess):
int myChar1 = (int)([myText1 characterAtIndex:i]);
int myChar2 = (int)([myText2 characterAtIndex:keyCurrent]);
int newChar = (myChar1 + myChar2);
//NSLog(#"Int's %d, %d, %d", textChar, keyChar, newChar);
char newC = ((char) newChar);
NSString *tmp1 = [NSString stringWithFormat:#"%c", newC];
NSString *tmp2 = [NSString stringWithFormat:#"%#", newString];
newString = [NSString stringWithFormat:#"%#%#", tmp2, tmp1]; //Adding these char's in a string
The algorithm is perfect, but now I can't figure out how to implement encoding properties. I would like to do everything in UTF-8 but have no idea how to get a char's UTF-8 value, for instance. And if I've got it, how to change that value back to an char.
The NSLog in the code outputs the correct values. But when I try to do the opposite with the algorithm (I.e. - the values) then it goes wrong. It gets the wrong character value for weird/odd characters.
NSString works with unichar characters that are 2 bytes long (16 bits). Char is one byte long so you can only store code point from U+0000 to U+00FF (i.e. Basic Latin and Latin-1 Supplement).
You should do you math on unichar values then use +[NSString stringWithCharacters:length:] to create the string representation.
But there is still an issue with that solution. You code may generate code points between U+D800 and U+DFFF that aren't valid Unicode characters. The standard reserves them to encode code points from U+10000 to U+10FFFF in UTF-16 by pairs of 16-bit code units. In such a case, your string would be ill-formed and could neither be displayed nor converted in UTF8.
Also, the temporary variable tmp2 is useless and you should not create a new newString as you concatenate the string but rather use a NSMutableString.
I am assuming that your strings are NSStrings consisting of numerals which represent a number. If that is the case, you could try the following:
Include the following headers:
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
Then use the following code:
// convert NSString to UTF8 string
const char * utf8String1 = [myText1 UTF8String];
const char * utf8String2 = [myText2 UTF8String];
// convert UTF8 string into long integers
long num1 = strtol(utf8String1, NULL 0);
long num2 = strtol(utf8String2, NULL 0);
// perform calculations
long calc = num1 - num2;
// convert calculated value back into NSString
NSString * calcText = [[NSString alloc] initWithFormat:#"%li" calc];
// convert calculated value back into UTF8 string
char calcUTF8[64];
snprintf(calcUTF8, 64, "%li", calc);
// log results
NSLog(#"calcText: %#", calcText);
NSLog(#"calcUTF8: %s", calcUTF8);
Not sure if this is what you meant, but from what I understood, you wanted to create a NSString with the UTF-8 string encoding from a char?
If that's what you want, maybe you can use the initWithCString:encoding: method in NSString.

Get integer from UIWebView

I have a webpage that outputs an integer that I need to parse. I know how to get a string from a website in an UIWebView, but I need to get it as an integer. Converting a string to an integer using [myString integerValue] won't work here. How can I do this?
Edit: Here is my error
The reason behind the warning is that your variable is not an integer, but a pointer to an integer.
NSInteger *test = [onlineUsers integerValue];
^
Get rid of the asterisk

is there a way to get the character count in a string? in obj c

hii every one
is there a way to get the character count in a string in obj c?
like how does the SMS app determine how big of a bubble the text view sends and receives? thanks a lot
You can use something like this:
NSString *str = #"Hello World!";
NSUInteger len = str.length;
If it is NSString then use str.length
If it is c string then use strlen(cString)
If it is a NSString then
[string length];
Not sure if you are talking about multi-bytes characters
length
Returns the number of Unicode
characters in the receiver.
- (NSUInteger)length
Return Value
The number of Unicode characters in
the receiver. Discussion
The number returned includes the
individual characters of composed
character sequences, so you cannot use
this method to determine if a string
will be visible when printed or how
long it will appear.
NSLog(#"Length of your string is %d",[yourString length]);

Adding to a NSString

How can I create a string that can have integers added and removed to it with a simple + or -1?
You can't. What you can do is add or subtract from a number and get the string representation when you need it:
int a = 30;
a++;
a--; // etc.
NSString *numberString = [NSString stringWithFormat:#"%d", a];
If you want to append or remove characters representing integers from the end of strings, look into the NSString class.
If you're looking for operator overloading, Objective C doesn't have it.

Objective-C parse hex string to integer

I would like to know how to parse a hex string, representing a number, in Objective-C. I am willing to use both an objective, or a C-based method, either is fine.
example:
#01FFFFAB
should parse into the integer:
33554347
Any help would be appreciated!
Joshua Weinberg's answer is mostly correct, however the 0x prefix is optional when scanning hexadecimal integers. If you have a string in the format #01FFFFAB, you can still use NSScanner, but you can skip the first character.
unsigned result = 0;
NSScanner *scanner = [NSScanner scannerWithString:#"#01FFFFAB"];
[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&result];
you can use NSScanner for this
unsigned int outVal;
NSScanner* scanner = [NSScanner scannerWithString:#"0x01FFFFAB"];
[scanner scanHexInt:&outVal];
outVal will contain the int you're looking for. The 0x is optional.
strtol() is your friend.
It converts a string to a long, and you can pass the base of the number in. Strip that # sign off first though, or pass to strtol a pointer to the first numerical character.
You can use the below line for conversion. Its just one line code:
NSString *hexString = #"01FFFFAB";
length = (UInt64)strtoull([hexString UTF8String], NULL, 16);
NSLog(#"The required Length is %d", length);
Happy Coding!!!
Swift 4 standard library introduced new initializer for parsing all integer types. It takes string to parse with radix (i.e. base) and returns optional integer:
let number = Int("01FFFFAB", radix: 16)!
According to apple:
An NSScanner object interprets and converts the characters of an
NSString object into number and string values.
so, if u have NSData obj u can do next
NSString *dataDescription = data.description;
NSString *dataAsString = [dataDescription substringWithRange:NSMakeRange(1, [dataDescription length]-2)];
unsigned intData = 0;
NSScanner *scanner = [NSScanner scannerWithString:dataAsString];
[scanner scanHexInt:&intData];
For Swift 3:
var hex = "#01FFFFAB"
hex.remove(at: hex.startIndex)
var rgbValue:UInt32 = 0
Scanner(string: hex).scanHexInt32(&rgbValue)
// rgbValue == 33554347