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 ;
Related
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;
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);
I trying to do conversions between Binary, Octal, Decimal and Hexadecimal in Objective-C.
I had problems converting Octal to Decimal.
I have tried the following:
NSString *decString = [NSString stringWithFormat:#"%d", 077];
It works fine, returning 63 as expected, but my Octal value is a NSString. How can I tell the computer that it is a Octal;
I know there is a method called "scanHexInt:" which I used to convert Hexadecimal to decimal, but it seems there is no scanOctInt...
Any help would be appreciated!
The cleanest solution is probably:
long result = strtol(input.UTF8String, NULL, 8);
or
long long result = strtoll(input.UTF8String, NULL, 8);
Define a category on NSString (put this on top of any of your source code modules or into a new .m/.h file pair, #interface goes into .h, #implementation into .m):
#interface NSString (NSStringWithOctal)
-(int)octalIntValue;
#end
#implementation NSString (NSStringWithOctal)
-(int)octalIntValue
{
int iResult = 0, iBase = 1;
char c;
for(int i=(int)[self length]-1; i>=0; i--)
{
c = [self characterAtIndex:i];
if((c<'0')||(c>'7')) return 0;
iResult += (c - '0') * iBase;
iBase *= 8;
}
return iResult;
}
#end
Use it like that:
NSString *s = #"77";
int i = [s octalIntValue];
NSLog(#"%d", i);
The method returns an integer representing the octal value in the string. It returns 0, if the string is not an octal number. Leading zeroes are allowed, but not necessary.
Alternatively, if you want to drop down to C, you can use sscanf
int oct;
sscanf( [yourString UTF8String], "%o", &oct );
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.
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.