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];
Related
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 handle decimal numbers in solidity?
If you want to find the percentage of some amount and do some calculation on that number, how to do that?
Suppose I perform : 15 % of 45 and need to divide that value with 7 how to get the answer.
Please help. I have done research, but getting answer like it is not possible to do that calculation. Please help.
You have a few options. To just multiply by a percentage (but truncate to an integer result), 45 * 15 / 100 = 6 works well. (45 * 15%)
If you want to keep some more digits around, you can just scale everything up by, e.g., some exponent of 10. 4500 * 15 / 100 = 675 (i.e. 6.75 * 100).
Currently my function goes like this:
#define kKILOMETER 1000
#define kSeconds_To_Milliseconds 1000
#define kHOUR 60
#define kMINUTE 60
#define kLongDistance_3KM 3
- (long)calculateTimeToMilliSecondsWithDistance:(double)distance andSpeed:(float)theSpeed
{
// Distance arrive in meters
double kmDistance = distance/kKILOMETER;
long time =
(long)(((kmDistance - kLongDistance_3KM)/[self.ref integerForKey:#"MAX_SPEED"])*kHOUR*kMINUTE*kSeconds_To_Milliseconds);
NSLog(#"calculateTimeToMilliSeconds -> sleep to (sec): %li, maxSpeed: %li",
time/1000, (long)[self.ref integerForKey:#"MAX_SPEED"]);
[self startSendSleepTime:time];
return time;
}
My "MAX_SPEED" value refer to 200 km/h.
What I get back is wrong value.
What I want to to calculate the time using my distance minus 3 kilometers with speed of 200 km/h but somehow it mess up.
Also it would be even better to get it in seconds, for that I should remove the kSeconds_To_Milliseconds, right?
I don't know what mess up for me.
EDIT 1:
For example:
2014-03-19 18:12:39.624 Cellular Radar[3338:60b] Distance to car: 4713, Trap Radius: 900
2014-03-19 18:12:39.673 Cellular Radar[3338:60b] calculateTimeToMilliSeconds -> sleep to (sec): 2055, maxSpeed: 3
2014-03-19 18:12:39.684 Cellular Radar[3338:60b] Sleep for (2055.000000 seconds | 34.250000 minutes | 0.570833 hours)
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.
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