How get int from hex with Objective-C? - objective-c

I have char with hex value '\xa1', it's 161, and how I can get 161 in int value?
This doesn't work for me:
char a = '\xa1';
int b = a;
And I have a uint8_t buffer[4], it reads bytes from NSInputStream, with hex value like this, how I can get array with int values from this array?

A char is signed (where the high bit designates the number as negative). You apparently want an unsigned char, so either:
unsigned char a = '\xa1';
int b = a;
Or
char a = '\xa1';
int b = (unsigned char) a;

Related

objective c convert int to unsigned char hex

I've to convert
int x = 4 in to unsigned char y = 0x04
int x = 0 in to unsigned char y = 0x00
int x = -4 in to unsigned char y = 0xFC
int x = -8 in to unsigned char y = 0xF8
and so on, till -20 to 0xEC.
Does anyone know a smart way to do ?
There is no conversion as such; simply cast from int to unsigned char:
int x = -4;
unsigned char y = (unsigned char)x;
There is also no hex as such; that's just a formatting option to printf() or whatever:
printf("y=0x%02x", (unsigned)y);

How convert char* to float xcode

I need convert 'field' in float type. How can do?
char *field = (char *) sqlite3_column_text(statment, 1);
Assuming that the floating point value is stored in a string column, you can use the sqlite3_column_double function of SQLite:
float field = (float)sqlite3_column_double(statement, 1);
SQLite will do an automatic conversion for you, but using a column of a floating-point type would be a better choice.
This can be done this way as well if you are converting numeric value.
char c = 0x010;
char* ch = &c;
float f = ((float)(*ch));
float* fl = &f;
printf("\n%f\n",*fl);

Using scanf in Objective-C will not assign to an integer?

I know that i need to assign int a and int b to something but i replaced it with the scanf functions example ( int a = scanf("%d", a); and int b = scanf("%d", b); but that did not work so i kept it as original like shown.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
int a ;
int b ;
int c = a * b;
printf("Welcome to the multiplication calculator");
printf("\n");
printf("what would you like to choose for first value?");
scanf("%d", &a);
printf("\n");
printf("What would you like to input for the second value?");
scanf("%d", &b);
printf(" Here is your product");
printf("\n");
NSLog(#"a * b =%i", c);
return 0;
}
int c = a * b;
Should be right before your NSLog.
When you say int c = a * b at the beginning here is what happens:
Since int a is not initialized, it is given a random garbage value, same with b. Then you immediately assign c the product of the two garbage values.
While you'r initializing c as c=a+b at that time the values of a and b are zeros so its storing c to zero. After that by using scanf your reading values to a and b . Now if you print c value the value is zero thats what it is showing.
If you want proper result you have to put the statement c=a+b after reading a and b.

Converting int, double to char array in C or Objective C

I want to convert int to char array and double to char array. Any way of doing this in C or Objective C.
If you want to treat the number as an array of char, you can take the address and cast the pointer:
int i;
double d;
char * ic = (char *) &i;
char * dc = (char *) &d;
then ic and dc are pointers to char. They aren't zero-terminated, so you can't use them as strings, but they can be used as arrays.
For another interpretation of what "converting" means, use
union double_or_bytes { double d ; char bytes[8] ; } converter;
converter.d = <the double you have> ;
<do what you wanted to do with> converter.bytes ;

how to convert byte value into int in objective-c

Please tell me how to convert bytes to NSInteger/int in objective-c in iPhone programming?
What do you mean by "Bytes"?
If you want convert single byte representing integer value to int (or NSInteger) type, just use "=":
Byte b = 123;
NSInteger x;
x = b;
as Byte (the same as unsigned char - 1 byte unsigned integer) and NSInteger (the same as int - 4 bytes signed integer) are both of simple integer types and can be converted automatically. Your should read more about "c data types" and "conversion rules".
for example http://www.exforsys.com/tutorials/c-language/c-programming-language-data-types.html
If you want to convert several bytes storing some value to int, then convertion depends on structure of these data: how many bytes per value, signed or unsigned.
If by byte, you mean an unsigned 8 bit value, the following will do.
uint8_t foo = 3; // or unsigned char foo...
NSInteger bar = (NSInteger) foo;
or even
NSInteger bar = foo;
My guess:
unsigned char data[] = { 0x00, 0x02, 0x45, 0x28 };
NSInteger intData = *((NSInteger *)data);
NSLog(#"data:%d", intData); // data:675611136
NSLog(#"data:%08x", intData); // data:28450200
So, beware of byte-order.
NSInteger x = 3;
unsigned char y = x;
int z = x + y;
Use the "=" operator.