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);
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 recently found that UIActionSheet actually caches its available delegate selectors before it presents.
Why not determine whether the delegate responds to a certain selector when it really needs to know? Is [delegate respondsToSelector:#selector()] really slow to perform? Should we use this kind of design pattern in our own code? Any ideas would be appreciated!
The flags struct in UIActionSheet.h is as follows:
struct {
unsigned int numberOfRows:7;
unsigned int delegateAlertSheetButtonClicked:1;
unsigned int delegateDidPresentAlertSheet:1;
unsigned int delegateDidDismissAlertSheet:1;
unsigned int hideButtonBar:1;
unsigned int alertStyle:3;
unsigned int dontDimBackground:1;
unsigned int dismissSuspended:1;
unsigned int dontBlockInteraction:1;
unsigned int sheetWasPoppedUp:1;
unsigned int sheetWasShown:1;
unsigned int animating:1;
unsigned int hideWhenDoneAnimating:1;
unsigned int layoutWhenDoneAnimating:1;
unsigned int titleMaxLineCount:2;
unsigned int bodyTextMaxLineCount:3;
unsigned int runsModal:1;
unsigned int runningModal:1;
unsigned int addedTextView:1;
unsigned int addedTableShadows:1;
unsigned int showOverSBAlerts:1;
unsigned int showMinTableContent:1;
unsigned int bodyTextTruncated:1;
unsigned int orientation:3;
unsigned int popupFromPoint:1;
unsigned int inPopover:1;
unsigned int delegateBodyTextAlignment:1;
unsigned int delegateClickedButtonAtIndex:1;
unsigned int delegateClickedButtonAtIndex2:1;
unsigned int delegateCancel:1;
unsigned int delegateCancel2:1;
unsigned int delegateWillPresent:1;
unsigned int delegateWillPresent2:1;
unsigned int delegateDidPresent:1;
unsigned int delegateDidPresent2:1;
unsigned int delegateWillDismiss:1;
unsigned int delegateWillDismiss2:1;
unsigned int delegateDidDismiss:1;
unsigned int delegateDidDismiss2:1;
unsigned int dontCallDismissDelegate:1;
unsigned int useAutomaticKB:1;
unsigned int twoColumnsLayoutMode:7; // one column || even width (leaves empty space) || first button wider || last button wider
unsigned int threeColumnsLayoutMode:7; // one column || even width (leaves empty space) || first button wider || last button wider
unsigned int shouldHandleFirstKeyUpEvent:1; // when presenting with hardware KB we have to handle the first key up event we get so we don't end up repeating the last key
unsigned int cancelWhenDoneAnimating:1;
unsigned int useThreePartButtons:1; // phone numbers layout
unsigned int useTwoPartButtons:1; // doc interaction layout
unsigned int displaySelectedButtonGlyph:1;
int indexOfSelectedButton:7; // default -1 (no checkmark) otherwise will display a checkbox (this for the airtunes action sheet)
unsigned int useCustomSelectedButtonGlyph:1;
} _modalViewFlags;
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 ;
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.