-1

I'm currently working on a project where I need to work with unsigned char* and unwanted bytes in there. I'll describe the situation:

1/ I have an iOS app that loads an image (UIImage).

2/ I transform this UIImage into a unsigned char* (thanks to the ImageHelper given by PaulSoft here

3/ I send this unsigned char* to a library that works his magic on the image.

4/ The library sends me back an unsigned char* with complementary information at the beginning of the array.

And here I am. I want to remove the 13 firsts bytes of this array, and I have no idea how to do so. I've tried converting it to a NSString*, cropping it and get it back. Same thing with NSData*. I am definitely not a pro in Objective-C, so I'm open to every ideas you can have. Please feel free to ask me anything if I wasn't clear enough.

Best regards;

Chris.

Chris
  • 3
  • 2
  • 1
    This has nothing to do with Objective-C -- it's a basic C programming question. Learn C before you tackle Objective-C. (The answer is `newBufferPointer = oldBufferPointer + 13;`) – Hot Licks Apr 09 '15 at 12:52
  • The simplest way is to copy everything except the first 13 bytes to a new array (or just ignore them). – molbdnilo Apr 09 '15 at 12:53

1 Answers1

2

As @HotLicks said in his comment, the simplest thing to do is to simply create a new char pointer that points 13 bytes further into the buffer:

char* newPointer = oldPointer + 13;

It's likely that you need to keep the original pointer so you can release the block of memory once you're done with it. (If you created the buffer with malloc(), {or calloc(), or one of the related functions} or the library you're using did so, you need to call free() on the buffer to release it once you're done with it. The docs on the library you're using should tell you if you need to free the buffer or not. That's important.)

You could also use the C function memmove() to copy the end of the block up 13 bytes, but there's no real reason to do that. (You should not use the related function memcpy() because it may not work correctly when the source and destination of a move overlap.)

Edit:

@HotLicks, you should really post your comment as an answer so the OP can accept yours. You deserve the credit for it since you posted it first.

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Ok, I just feel so stupid right now... I have tried a lot (and i mean A LOT) of things i found either here or on the web, and the solution was as easy as this. Anyway, thank you HotLicks, and I'm going to check this answer as the good response (since I can't do so on a comment). – Chris Apr 09 '15 at 13:15