I have a NSString which i have to convert into NSData. After converting into NSData, I need to transpose the NSData in objective-c. How do i transpose NSData in objective -c ?
Thanks
I have a NSString which i have to convert into NSData. After converting into NSData, I need to transpose the NSData in objective-c. How do i transpose NSData in objective -c ?
Thanks
It seems you wish to reverse the order of the bytes, here is an outline algorithm:
NSData
's length
to get the number of bytesByte
to hold the bytes. Use one of the malloc()
family.getBytes:length:
to get a copy of the bytesByte *
variable, say frontPtr
, to point at the first byte; set another, say rearPtr
to point at the last byte.frontPtr
, decrement rearPtr
, and keep iterating while rearPtr > frontPtr
.NSData
from the working buffer using NSData
's + dataWithBytesNoCopy:length:freeWhenDone:
passing YES
as the last argument - this will take ownership of the malloc
'ed buffer so there is no need for you to free it.The algorithm simply moves two pointers from the ends of the buffer towards the middle exchanging bytes as it goes. The termination condition make it work for even and odd lengths (in the latter case the middle byte doesn't need to be swapped).
If one the other hand you didn't wish to reverse the order of the bytes, but instead reverse the order of bits in each byte Google "C bit reverse" and follow the same general structure as the above algorithm but do bit reversing in the loop.
If after coding the above you have a problem ask a new question, include your code, and explain your issue. Someone will undoubtedly help you.
HTH