Curnelious
Curnelious

Reputation: 1

NSString to bytes with NSData?

I need to convert NSString to bytes-to send over tcp.

 NSString *dataS= @"ran"; // is in byte  114 97 110 

but if i use the NSData to convert it(i need it as an NSData at the end because i am adding multiple NSData's) with :

NSData *requestData = [dataS dataUsingEncoding:NSUTF8StringEncoding];

i get : <72616e> when logging the solution . and its not true. I am probably missing something here .

using the :[ToSend characterAtIndex:k] to convert it, gives integer- i want to use NSData for the conversion. How would i do that ?

Upvotes: 0

Views: 122

Answers (2)

Tammo Freese
Tammo Freese

Reputation: 10754

You convert the string the correct way. requestData contains three bytes. In the output, these bytes are shown as hex: 72 in hex is 114 in decimal, 61 in hex is 97 in decimal, 6e in hex is 110 in decimal.

To pass the bytes to lower level methods, you can get the length of the data by calling [requestData length], and a pointer to the bytes by calling [requestData bytes].

Upvotes: 3

Antonio MG
Antonio MG

Reputation: 20410

You are doing it right, the logging is showing it like that because it's the way the logger formates it for the output, that's all.

114 97 110 in bytes is <72616e> in Hex.

Upvotes: 0

Related Questions