I'm trying to find the new line and returns that are inside an nsdata object that I'm parsing . Here's some code:
uint8_t *arr = [receivedData bytes];
NSUInteger begin1 = 0;
NSUInteger end1 = len;
uint8_t *arr1 = (Byte *)malloc(sizeof(Byte)*((end1-begin1+1)));
int j = 0;
for (int i = begin1; i < end1; i++){
arr1[j] = arr[i];
j++;
if (arr[i] == 10) NSLog(#"---new line code---"); //edit: working - data was a problem
}
I just need to know when I hit a new line or return.
Thank You.
That certainly looks correct. Are you certain that the data you're parsing has newlines? Could it be a /r instead of a /n? If you can, try using the debugger and step through to see what the values actually are, and compare with what you expect them to be, to make sure they are correct.
Related
I have a problem with understand some basics. I want create pointer array (a1) copy it to NSData (c1) than again create pointer array (b1) from NSData and check if content of a1 and b1 is the same.
But I face with two erors:
First is "incorrect checksum for freed object" when I want create NSData dataWithBytes:length:
Second bytes in a1 and b1 aren't the same.
Could someone tell me why? For record I use xcode5 with arc.
- (void) testCopyBuffer {
int const bufferSize =4096;
int* a1;
a1 = (int*)malloc(bufferSize);
for (int i=0; i<bufferSize; i++) {
a1[i] = i;
}
NSData *c1 = [NSData dataWithBytes:a1 length:bufferSize];
int* b1;
b1 = malloc(bufferSize);
[c1 getBytes:b1 length:bufferSize];
for (int i=0; i<bufferSize; i++) {
XCTAssertTrue(a1[i]==b1[i], "Powinny być takie same");
}
}
You don't allocate the right amount of memory.
For bufferSize number of ints, you need to allocate
a1 = malloc(bufferSize * sizeof(int));
And later consequently
NSData *c1 = [NSData dataWithBytes:a1 length:(bufferSize * sizeof(int))];
etc. In your case,
for (int i=0; i<bufferSize; i++) {
a1[i] = i;
}
writes beyond the allocated memory, which can lead to all kinds of undefined
behaviour.
I was getting this error when running my code
2013-02-23 10:52:54.063 Calculator[31319:11303] * Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString characterAtIndex:]: Range or index out of bounds'
* First throw call stack:
(0x1c90012 0x10cde7e 0x1c8fdeb 0x1c56c0d 0x2d4b 0x10e1705 0x18920 0x188b8 0xd9671 0xd9bcf 0xd8d38 0x4833f 0x48552 0x263aa 0x17cf8 0x1bebdf9 0x1bebad0 0x1c05bf5 0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x1565c 0x22c2 0x21f5 0x1)
My application usually takes the string entered in the textbox and converts to ascii values and then I wrote a small algorithm for it.Below is the piece of code how I am converting the string to ascii
for (int i=0; i<[first length]; i++) {
unichar ch = [first characterAtIndex:i];
firsttotal = firsttotal +ch;
}
for (int j=0; j<[second length]; j++) {
unichar chi = [first characterAtIndex:j];
secondtotal = secondtotal +chi;
}
Unless I'm reading your code wrong, it should be this:
for (int i=0; i<[first length]; i++) {
unichar ch = [first characterAtIndex:i];
firsttotal = firsttotal +ch;
}
for (int j=0; j<[second length]; j++) {
unichar chi = [second characterAtIndex:j]; // <-- THIS LINE
secondtotal = secondtotal +chi;
}
You're trying to take characters from first that may be beyond the end of the string.
Your second loop is pulling characters out of the first string. This is probably not what you intend. Because you iterate for each character in the second string, you will get this error with your code as posted above any time the second string is longer than the first.
Try changing the line
unichar chi = [first characterAtIndex:j];
to
unichar chi = [second characterAtIndex:j];
and this problem will disappear.
hi i am getting the errer invalid operends to binary expression nsstring and id even i use typecasting this is the code in which i have problem . kindly correct this code.
for (int j = 0 ; j<newarray.count ; j++){
if(j<newarray.count){
message = (NSString *) message + [newarray objectAtIndex:j]+ "," ;
}
}
You can not use + operator with objects, for your specific case you may replace the whole cycle with:
NSString* message = [newarray componentsJoinedByString:#","];
And FIY Objective-C does not support operator overloading at all.
I'm not really sure if this was your intention, but if you were trying to append new information to the string separated by commas you could go with something like this:
for (int j = 0 ; j<newarray.count ; j++){
[message stringByAppendingString:[NSString stringWithFormat:#"%#,",[newarray objectAtIndex:j]]];
}
Additionally, your condition if(j<newarray.count) would always evaluate true in this loop, and is therefore unnecessary.
You seem to be assuming NSString acts like std::string in C++. Try this:
NSMutableString *message = ...;
for (unsigned j = 0; j < newarray.count; j++)
{
if (j > 0)
[message appendString:#", "];
[message appendString:[newarray objectAtIndex:j]];
}
You should cast "[newarray objectAtIndex:j]", whose default return type is "id"
Check this
Also IIRC NSString doesn't have + operand overloaded. So maybe you should try [NSString stringWithFormat:].
I need to add space after every 4 characters in a string.. For example if the string is aaaaaaaa, i need to format it as aaaa aaaa. I tried the following code, but it doesn't work for me.
NSMutableString *currentFormattedString = [[NSMutableString alloc] initWithString:formattedString];
int count = [formattedString length];
for (int i = 0; i<count; i++) {
if ( i %4 == 0) {
[currentFormattedString insertString:#" " atIndex:i];
}
}
Can anyone help me with this?
You haven't said what isn't working with your code, so it's hard to know exactly what to answer. As a tip - in future questions don't just say "it isn't working", but state WHAT isn't working and HOW it isn't working. However...
NSMutableString *currentFormattedString = [[NSMutableString alloc] initWithString:formattedString];
int count = [formattedString length];
for (int i = 0; i<count; i++) {
if ( i %4 == 0) {
[currentFormattedString insertString:#" " atIndex:i];
}
}
You are inserting a space, but you are not then accounting for this in your index value. So, suppose your formattedString is aaaaaaaaaaaaaaaa
The first time through your loop, you will get to the 4th position and insert a space at i=4
aaaa aaaaaaaaaaaa
Now the next time you get to insert a space, i will be 8. But the 8th position in your currentFormattedString isn't where you think it will be
aaaa aaa aaaaaaaaa
Next time it will be another 4 characters along which still isn't where you think
aaaa aaa aa aaaaaaa
And so on
You have to take into account the inserted space which will affect the offset value.
NSString *text = [[NSString alloc] initWithString:#"aaaaaaaa"];
NSString *result = [[NSString alloc] init];
double count = text.length/4;
if (count>1) {
for (int i = 0; i<count; i++) {
result = [NSString stringWithFormat:#"%#%# ",result,[text substringWithRange:NSMakeRange(i*4, 4)]];
}
result = [NSString stringWithFormat:#"%#%# ",result,[text substringWithRange:NSMakeRange(((int)count)*4, text.length-((int)count)*4)]];
}
else result = text;
I found the following which formats a string to a telephone number format, but it looks like you could easily change it to support other formats
Telephone number string formatting
Nick Bull answered on the reasons why your method broke already.
IMHO the appropriate solution would be to use a while loop and do the loop increments yourself.
NSInteger i = 4; // first #" " should be inserted after the 4th (index = 3) char
while (i < count) {
[currentFormattedString insertString:#" " atIndex:i];
count ++; // you did insert #" " so the length of the string increased
i += 5; // you now must skip 5 (" 1234") characters
}
I have three CGLayers who's data I'd like to compare.
void *a = CGBitmapContextGetData(CGLayerGetContext(layerA));
void *b = CGBitmapContextGetData(CGLayerGetContext(layerB));
void *c = CGBitmapContextGetData(CGLayerGetContext(layerC));
I'd like to get a result like ((a OR b) AND c) where only bits that are on in layerA or layerB and also on in layerC end up in the result. These layers are kCGImageAlphaOnly so they are only 8 bits "deep", and I've only drawn into them with 1.0 alpha. I also don't need to know where the overlap lies, I just need to know whether there are any bits on in the result.
I'm really missing QuickDraw today, it had plenty of bit-oriented operations that were very speedy. Any thoughts on how to accomplish something like this?
Here's a naive implementation, assuming all three are the same size:
unsigned char *a = CGBitmapContextGetData(CGLayerGetContext(layerA));
unsigned char *b = CGBitmapContextGetData(CGLayerGetContext(layerB));
CGContextRef context = CGLayerGetContext(layerC);
unsigned char *c = CGBitmapContextGetData(context);
size_t bytesPerRow = CGBitmapContextGetBytesPerRow(context);
size_t height = CGBitmapContextGetHeight(context);
size_t len = bytesPerRow * height;
BOOL bitsFound = NO;
for (int i = 0; i < len; i++) {
if ((a[i] | b[i]) & c[i]) { bitsFound = YES; break; }
}
Since you're hankering for QuickDraw, I assume you could have written that yourself, and you know that will probably be slow.
If you can guarantee the bitmap sizes, you could use int instead of char and operate on four bytes at a time.
For more serious optimization, you should check out the Accelerate framework.
What about the CGBlendModes? kCGBlendModeDestinationOver acts as OR for A and B, and then you can use kCGBlendModeDestinationIn to AND that result with C.