fairly new iPhone developer here. Building an app to send RS232 commands to a device expecting them over a TCP/IP socket connection. I've got the comms part down, and can send ASCII commands fine. It's the hex code commands I'm having trouble with.
So lets say I have the following hex data to send (in this format):
\x1C\x02d\x00\x00\x00\xFF\x7F
How do I convert this into an NSData object, which my send method expects?
Obviously this does not work for this hex data (but does for standard ascii commands):
NSString *commandascii;
NSData *commandToSend;
commandascii = @"\x1C\x02d\x00\x00\x00\xFF\x7F";
commandToSend = [commandascii dataUsingEncoding:NSStringEncoding];
For a start, some of the \x hex codes are escape characters, and I get an "input conversion stopped..." warning when compiling in XCode. And NSStringEncoding obviously isn't right for this hex string either.
So the first problem is how to store this hex string I guess, then how to convert to NSData.
Any ideas?
This is an old topic, but I'd like to add some remarks.
• Scanning a string with
[NSString characterAtIndex]
is not very efficient. Get the C string in UTF8, then scan it using a*char++
is much faster.• It's better to allocate
NSMutableData
with capacity, to avoid time consuming block resizing. I think NSData is even better ( see next point )• Instead of create NSData using malloc, then
[NSData dataWithBytes]
and finally free, use malloc, and[NSData dataWithBytesNoCopy:length:freeWhenDone:]
It also avoids memory operation ( reallocate, copy, free ). The freeWhenDone boolean tells the NSData to take ownership of the memory block, and free it when it will be released.
• Here is the function I have to convert hex strings to bytes blocks. There is not much error checking on input string, but the allocation is tested.
The formatting of the input string ( like remove 0x, spaces and punctuation marks ) is better out of the conversion function. Why would we lose some time doing extra processing if we are sure the input is OK.
If you can hard code the hex data:
If your code must interpret the hex string (assuming the hex string is in a variable called
inputData
andlengthOfInputData
is the length ofinputData
):Hex data is just bytes in memory, you think of it as a string because that's how you see it but they could represent anything. Try: (typed in the browser, may contain errors)
etc...
I know this is a very old thread, but there is an encoding scheme in Objective C that can easily convert your string of hex codes into ASCII characters.
1) remove the
\x
from the string and with out keeping spaces in the string just convert the string toNSData
using :If I want to hard-code the bytes, I do something like this:
If you're obtaining these backslash-escaped bytes at run time, try the
strunvis
function.First, it's Xcode, with a lowercase c.
Second,
NSStringEncoding
is a type, not an encoding identifier. That code shouldn't compile at all.More to the point, backslash-escaping is not an encoding; in fact, it's largely independent of encoding. The backslash and 'x' are characters, not bytes, which means that they must be encoded to (and decoded from) bytes, which is the job of an encoding.