1

I got a string in little endian that I would like to convert in big endian.

This "647b" should become "7b64". How can I do this in iOS (C++ code welcome)?

PS: I am deriving the string from a NSData object.

mm24
  • 9,280
  • 12
  • 75
  • 170
  • Have you had a look at https://developer.apple.com/reference/corefoundation/1667080-byte_order_utilities – sbarow Sep 30 '16 at 10:45
  • Do you want to change memory location like concept of `endians` or just want to change string value like that ? – Ketan Parmar Sep 30 '16 at 10:50
  • What exactly is "647b"? Is it a string with four characters giving the hexadecimal representation of two bytes? Or is it the hexadecimal representation of some string internals? What are you trying to achieve anyway? Please better describe it. Most likely, you need to change the endiness in the NSData instance before any strings are involved. – Codo Sep 30 '16 at 10:56
  • http://stackoverflow.com/questions/8552864/bit-conversion-tool-in-objective-c Try to use this solution. @Codo you are right about my post. – Viktorianec Sep 30 '16 at 11:02

1 Answers1

0

You didn't say how you were converting your data into a NSString, so I have to make some assumptions here.

NSStrings (and CFStringRefs, which are toll free bridged) have encodings. Many iOS devs don't need to keep in mind that their strings are UTF8Encoded.

[If you look at the list of string encodings from Apple (CoreFoundation & Foundation), you'll see some of them do specify little-endian and big-endian.

And probably the best way to do what you are trying to do is something like this:

// load it as UTF16 big endian
NSString *str = [NSString alloc] initWithData:yourDataObject encoding:NSUTF16BigEndianStringEncoding];

That line of code I found in this related question.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215