0

I want to be able to get a string (for example the letter @"A"), and I want to get what is the byte value (01000001) and change it to another byte value like (01000010) = B. Than convert it in a string again and replace the letter A for the letter B, in objective-c (xcode)

I'm I clear ? (maybe not...) Thanks for any help.

Vincent

3
  • A char is indistinguishable from a byte so what is your goal? Commented May 29, 2013 at 1:59
  • check this link.... stackoverflow.com/questions/655792/… Commented May 29, 2013 at 5:27
  • My goal is to get the byte value (ex: 00100101) of a string like the letter @"A" and be able to change this byte value (UFC-8 format) Commented May 29, 2013 at 19:48

3 Answers 3

1

You can use characterAtIndex to get the character out of the NSString. (It will return a unichar.) It's a little tougher to go the other way, though.

It might be best to do getCharacters:range: to get the entire contents of the string, then initWithCharacters:length: to reconstruct the NSString.

Sign up to request clarification or add additional context in comments.

Comments

0

@"foo" is actually an NSString, take a look at the class documentation here:

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

Strings are immutable so you would need to use something like getCString to copy them to a char * buffer, change them and then create a new NSString using stringWithCString. Depending on the original encoding this may not do what you want.

Comments

0

I only have some background knowledge in Java, where I would tackle this problem as follows:

String has the methods length() and getCharAt(int index), which could be used in a for block to save each of the letters as a char, for example in an array. char s can be casted into an int:

char a = 'h';

int test = (int) a;

now all you have to do is to find or write a method changeIntegerToBinaryNumber() to get the binary number.

To get a letter from a binary number, the mentioned steps should just be reversed: find / write a method changeBinaryToInterger(), cast int to char, optionally save the char in an array which again can be build together to a String...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.