Objective C CFSwapInt32 - objective-c

I want to create a siple app which swaps the bytes of 2 and 4 byte hex codes.
So it should do: from 1234 to 3421 swap. I google and found out that I have to use byteorder and CFSwapInt32 and CFSwapInt16.
Here is what I already got:
NSString *byteOrder = [NSString stringWithFormat:#"%d",CFSwapInt32(12345678)];
NSLog(byteOrder);
But instead of the correct swapped bytes I get: 1315027968 as the number of the NSLog.
Can someone help me or tell me what I did wrong? :) I just want to swap bytes so they are in reversed order
1234 -->3412
12 34 -->34 12
12345678 -->78563412
12 34 56 78 --> 78 56 34 12
Thank you

Try
NSString *byteOrder = [NSString stringWithFormat:#"%x",CFSwapInt32(0x12345678)];
%x will output a value as hexadecimal.
Starting a number with 0x will interpret it as a hexadecimal value.

Your original number is 12345678 which, in hex, is 0x00BC614E
The output you get in the log is 1315027968 which, in hex, is 0x4E61BC00
So everything is working correctly.
You can try doing the same in hex if you prefer:
NSString *byteOrder = [NSString stringWithFormat:#"%x",CFSwapInt32(0x00BC614E)];
NSLog(byteOrder);
should log 0x4E61BC00 while
NSString *byteOrder = [NSString stringWithFormat:#"%x",CFSwapInt32(0x12345678)];
NSLog(byteOrder);
should log 0x78563412

Related

Why are values over 100 become negative?

I'm working on a deserializer in C++. I get a stream of data and I am transferring this to a char array. At the moment, everything is working perfectly except when the value is over 100, the value I get returned becomes a negative value. For example 241 becomes -15 but values below what it seems to be 100 stay the same.
Here is the code I am running
char streamBuffer[1024]; //where the stream data is held
/**code in between that transfer data stream to streamBuffer[]**/
char printBuffer[10];
for(int i = 0; i < 10 ; i++){
sprintf(printBuffer, "streamData = %ld",streamBuffer[i]);
PrintData(printBuffer); //prints the value
}
For example, my stream data could look like 1,3,5,10,241,etc.
When I get to the printData function, it gives me the correct value for any value below 100 it seems.
I've used
sprintf(printBuffer, "streamData = %lld",streamBuffer[i]);
or
sprintf(printBuffer, "streamData = %d",streamBuffer[i]);
but I am still not getting the right values.
So my output will look like this.
StreamData: 1
StreamData: 3
StreamData: 5
StreamData: 10
StreamData: -15
Solution was to change char streamBuffer[1024] to unsigned char streamBuffer[1024] and change %ld to %u in the sprintf statement. Now values over 100 remain the same.

How to make objective c console line vertical output?

Here's my understanding fetching the input:
int num = 0;
NSLog (#"Input 5 numbers");
scanf("%d", &num);
NSLog (#"\n You inputted: %d !", num);
Sample Input: 54321
Output: 54321
But the output should be:
5
4
3
2
1
I see no one-line-code solution here. It can be achieved with for or while loop. e.g. you may divide num by 10 or convert it to string and fetch each character.
For an example of converting an NSString to a char array see here.

How to convert hex to binary?

For Objective-C:
Hi everyone, I'm trying to convert a hex input into binary. For example, someone enters in :
A55
I want that to convert to
101001010101
I've tried looking through past posts and none seem to be working for me. Any help would be greatly appreciated.
Use a lookup table: there are only 16 possible characters in a HEX representation, each corresponding to a four-character binary code group. Go through the HEX character-by-character, obtain a lookup, and put it in the resultant NSString.
Here is a copy of the lookup table for you.
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
There are multiple options as to how to do lookups. The simplest way would be making a 128-element array, and placing NSStrings at the elements corresponding to codes of the characters (i.e. at positions '0', '1', ..., 'E', 'F', with single quotes; these are very important).
I believe there is a built-in function for this. If not, you should at least be able to go hex->dec then dec->bin
You can write the conversion from scratch if you know the number of characters, bin to hex is common enough algorithmically.
A mathematical look at the algorithms
SO Answers in C/C++ Another
Base 10 to base n in Objective C
C Hex->Bin
Build a lookup table (an array where you can supply a value between 0 and 15 to get the binary for that hex digit):
char *hex_to_bin[] = {
"0000", "0001", "0010", "0011",
/* ... */
"1100", "1101", "1110", "1111"
};
There should be 16 elements in that table. The conversion process for multiple digits is to handle one digit at a time, appending the results onto the end of your result storage.
Use getchar() to read a char:
int c = getchar();
if (c < 0) { puts("Error: Invalid input or premature closure."); }
Use strchr() to determine which array index to retrieve:
char *digits = "00112233445566778899AaBbCcDdEeFf";
size_t digit = (strchr(digits, c) - digits) / 2;
Look up the corresponding binary values for digit:
printf("%s", hex_to_bin[digit]); // You'll want to use strcat here.

Is there any short hand on formatting the String?

I am using objective c, and I got some variables like this:
1100
920
845
1439
But I want to change it to :
11:00
09:20
08:45
14:39
The problem is how can I fill the zero when I got three number only? I know the logic can be simple to do it I detect the string length, but is there any way to do it more effectually? Thank you.
unsigned int time = 1100;
NSString *strTime = [NSString stringWithFormat:#"%02u:%02u", time / 100, time % 100];

NSString get 100 characters from start

How to get a substring from NSSTring from index 0 to 99. i.e. first 100 characters
[myString substringToIndex:100]
You need to be sure that index 100 is valid, i.e. length of string is at least 100. Otherwise an exception will be thrown.
[str substringToIndex: 100]