How to Convert Nsdata to Unsigned or signed Byte array [duplicate] - ios

This question already has answers here:
How to convert NSData to byte array in iPhone?
(6 answers)
Closed 9 years ago.
in my app i have to upload byte array(Audio ) to server .
BUt now We are getting Nsdata .How to convert this Nsdata to Byte array to post the Audio file to server
Can any one help me?

your can use bytes method of NSData;
NSUInteger len = [yourData length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [yourData bytes], len);
If your byte is null terminated you can use
NSString *content = [NSString stringWithUTF8String:[yourData bytes]];
and if not
NSString *content = [[NSString alloc] initWithBytes:[yourData bytes]
length:[yourData length] encoding: NSUTF8StringEncoding];

Related

Send hexString Data through UDP

I'm developing an iOS app. I've come across a problem.
I am trying to send a hexString data through UDP to an wifi camera, which will response while getting correct data. My code is shown below. However I can't get any response from my wifi camera. (I'm using
https://github.com/robbiehanson/CocoaAsyncSocket)
NSString *sendMsg = #"6745000005000000000000000000000000000000000000001400000067450000140000000A";
NSData *bytes = [sendMsg dataUsingEncoding:NSUTF16BigEndianStringEncoding];
NSString *host = #"255.255.255.255";
[self.udpSocket sendData:bytes toHost:host port:ListenPort withTimeout:-1 tag:1];
Beside, I've try send my data through PacketSender (an app can send UDP data), which has a correct response.
enter image description here
Problem has been solved. The problem is while converting NSString to NSData. It's hex string which need to convert to NSData. Below is my code which works.
- (NSData *)dataFromHexString:(NSString *)hexString {
NSAssert((hexString.length > 0) && (hexString.length % 2 == 0), #"hexString.length mod 2 != 0");
NSMutableData *data = [[NSMutableData alloc] init];
for (NSUInteger i=0; i<hexString.length; i+=2) {
NSRange tempRange = NSMakeRange(i, 2);
NSString *tempStr = [hexString substringWithRange:tempRange];
NSScanner *scanner = [NSScanner scannerWithString:tempStr];
unsigned int tempIntValue;
[scanner scanHexInt:&tempIntValue];
[data appendBytes:&tempIntValue length:1];
}
return data;}

iOS how to convert a .aac audio file to binary data?

thanks for your help,my problem is : i need to convert a aac audio file to byte data then i can transmit this data to the server by using tcp.but i can not convert aac to byte. i use:
NSString *docDir = NSTemporaryDirectory();
NSString* _tempRecorderPath;
_tempRecorderPath = [docDir stringByAppendingPathComponent:#"ringtones_tmp.aac"];
NSData *testringdata=[NSData dataWithContentsOfFile:_tempRecorderPath];
Byte *nbyteData=(Byte*)[ringData bytes];
but the result is there is only '\xff' in nbyteData!
i do not know why ? And how to fix it!!
try this:-
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
This code will dynamically allocate the array to the correct size (you must free(byteData) when you're done) and copy the bytes into it.
Have a look at this link . This link explains exactly what you want
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);

Trouble converting NSData to int

I have an NSData object that contains just <64> which is supposed to represent the int 100
How can I convert this NSData to an int?
I can convert it to it's Chr equivalent d using
NSString *string = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
but I need the Dec equivalent of 100
Thanks
<64> means that the NSData object contains a single byte with the value 0x64 = 100,
so the following should work;
const uint8_t *bytes = [data bytes]; // pointer to the bytes in data
int value = bytes[0]; // first byte
int *b = (int *)data.bytes;
printf("%d",*b); //prints 100
Below logic converts NSData to integer perefctly. Length of bytes does not matter. It just works.
NSData *data;
NSString *stringData = [data description];
stringData = [stringData substringWithRange:NSMakeRange(1, [stringData length]-2)];
unsigned dataAsInt = 0;
NSScanner *scanner = [NSScanner scannerWithString: stringData];
[scanner scanHexInt:& dataAsInt];

Converting a series of bits into NSData object

How do I convert a series of 32 bits (representing 4 bytes) stored in an NSString, into an NSData object of 4 bytes in objective-c?
For example, how can I convert the following string:
NSString *bitSeries = #"00000000000000000000000111101100";
into NSData object with length precisely 4?
You can use strtoul() with base 2 to convert the string to an unsigned integer:
NSString *bitSeries = #"00000000000000000000000111101100";
uint32_t value = strtoul([bitSeries UTF8String], NULL, 2);
and then create an NSData object:
NSData *data = [NSData dataWithBytes:&value length:sizeof(value)];
NSLog(#"%#", data);
// Output: <ec010000>
Or, if you prefer big-endian byte order:
value = OSSwapHostToBigInt32(value);
NSData *data = [NSData dataWithBytes:&value length:sizeof(value)];
NSLog(#"%#", data);
// Output: <000001ec>

NSString cString is Deprecated. What is the alternative?

I've got another newbie question.
I've written a piece of code that converts a NSString to NSMutableData in order to simulate a webService result.
It turns out however that cString is deprecated. Can you help me replace it?
Here's my code.
NSString *testXMLDataString =
#"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
etc....
"</SOAP-ENV:Envelope>";
//Replace webData Received from the web service with the test Data
NSMutableData *testXMLData = [NSMutableData dataWithBytes:[testXMLDataString cString] length:[testXMLDataString length]];
[webData setData:testXMLData];
Get the raw bytes from the string.
Get the length of those bytes in the UTF8 encoding.
Create the NSData object using the +dataWithBytes:length: method.
const char *rawBytes = [testXMLDataString UTF8String];
const NSUInteger length = [testXMLDataString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSAssert(length > 0, #"Couldn't convert to UTF-8");
NSMutableData *testXMLData = [NSMutableData dataWithBytes:rawBytes length:length];
[webData setData:testXMLData];

Resources