PDA

View Full Version : Xcode/Obj-C methodology question?


myer0244
04-06-2009, 10:38 AM
Hello folks,
I am somewhat new to writing in xcode/obj-c.

I am trying to re-write one of my programs I had written with C# onto the mac platform. I have figured most of it out now, but there is one little piece that I cannot seem to overcome that I thought you all might be able to help me out with.

What I am trying to do is send a string of Hex data to a serial port usb dongle(rs232) I have figured a way to connect to the RS232 port and am able to send data to it already. My problem is converting the data to what the device on the other end of the rs232 cable can read.

Here is a snippet of my C# code:
private byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}

This converts a string of Hex data with or without spaces into an array of bytes in C#. The data is then sent out the designated communications port which the device can read.

My question is:
Is there a method to do this using Xcode/Obj-C? I have dug through the internet for about a week now and have not been able to find anything. Only converting bytes to hex which is not what i need. Anybody have any ideas?

Any help is appreciated!

hayne
04-06-2009, 01:11 PM
Is there a method to do this using Xcode/Obj-C?

I think you are asking the wrong question.
I think you meant to ask if there is a way to do such string manipulations in Cocoa. Cocoa is the set of object libraries that is commonly used for OS X apps.
Objective-C is just a language - it doesn't come with libraries of useful routines. Look up the docs on Cocoa.
And Xcode is just the IDE (GUI) used for editing & compiling.
Maybe start here: http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/index.html

myer0244
04-06-2009, 01:43 PM
I think you are asking the wrong question.
I think you meant to ask if there is a way to do such string manipulations in Cocoa. Cocoa is the set of object libraries that is commonly used for OS X apps.
Objective-C is just a language - it doesn't come with libraries of useful routines. Look up the docs on Cocoa.
And Xcode is just the IDE (GUI) used for editing & compiling.
Maybe start here: http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/index.html

Sorry, let me rephrase:
Is there any way to convert a Hex string into a Byte array within the mac development world?
I have looked through all of the documentation before on the development website with now luck(even in the Cocoa section).

Mikey-San
04-06-2009, 03:36 PM
Here is a snippet of my C# code:

The Cocoa string type is NSString. You can use a C string if you like, but they're harder to deal with and if you aren't careful, you will have stability and security problems. Stick with NSString. It does the hard work for you. (And if you need to manipulate an NSString and then get a raw C string out of it, there are pre-existing methods for that.)

You can also mix standard C and Obj-C without any effort. Obj-C is a strict superset of C, so anything you can do in C can be done in Obj-C.