0x02, 0x03 Hex-chars In Objective-c - String - Stack Overflow

Just browsing Stack Overflow? Help us improve your experience. Sign up for research
    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs 0x02, 0x03 hex-chars in objective-c Ask Question Asked 12 years, 4 months ago Modified 12 years, 4 months ago Viewed 4k times 0

I'm currently working on a server-client project and i'm writing a little tcp iPhone app client. The problem is, that I've to send the server a "packet" with the following formae:

0x02 + a string + 0x09 + an int + 0x03

The problem is, that I don't know how to include the 0x02 (as hex). At the end, I should send everything as NSData.

Here is what I've got so far.

unsigned char tag_start = 0x02; unsigned char tag_end = 0x03; unsigned char tag_split = 0x09; NSString * stringToSend = [NSString stringWithFormat: @"%c%@%c%@%c%@%c%@%c", tag_start, oobject, tag_split, oid, tag_split, oaction, tag_split, ovalue, tag_end]; NSData * dataToSend = [[NSData alloc] initWithData:[stringToSend dataUsingEncoding:NSASCIIStringEncoding]]; Share Improve this question Follow edited Jul 16, 2012 at 14:26 Cajunluke's user avatar Cajunluke 3,11329 silver badges28 bronze badges asked Jul 16, 2012 at 13:20 Lukas's user avatar LukasLukas 1,3567 gold badges24 silver badges50 bronze badges 3
  • Wrong is, that the, STX 0x02 will change to NSString 0x32 – Thomas Commented Jul 16, 2012 at 14:15
  • To convert an NSInteger (or an unsigned char) to a hex string, you can use the %x format specifier for NSString. Something like this: [NSString stringWithFormat@"0x%x", tag_start"] would produce 0x2 (which is the same value as 0x02). – Andrew Riebe Commented Jul 16, 2012 at 14:20
  • Thank's for this tipp, but it doesn't work. The chars in the result aren't STX, ETX, CSM. – Lukas Commented Jul 19, 2012 at 6:34
Add a comment |

3 Answers 3

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 0

try this way:

NSString *_command = @"\x02...\x09...\x03";

it should be working.

Share Improve this answer Follow answered Jul 16, 2012 at 14:40 holex's user avatar holexholex 24k7 gold badges63 silver badges77 bronze badges Add a comment | 0

I used, in a similar problem, the NSMutableData and worked then with appendBytes (to append STX, ETX, CSM) or appendData (NSString).

Share Improve this answer Follow answered Jul 16, 2012 at 14:18 Thomas's user avatar ThomasThomas 3301 silver badge11 bronze badges 1
  • Thanks for the reply. I've tried this already, but got problems to add the STX,... How have you managed to add them? – Lukas Commented Jul 17, 2012 at 10:48
Add a comment | 0

Heres how it works:

NSMutableData * mutableData = [[NSMutableData alloc] init]; // Appends STX [mutableData appendData: [NSData dataWithBytes: "\x02" length:1]]; // Appends ETX [mutableData appendData: [NSData dataWithBytes: "\x09" length:1]]; // Appends CSM [mutableData appendData: [NSData dataWithBytes: "\x03" length:1]];

My problem was that the server didn't respond to my messages, but the messages were correct.

Share Improve this answer Follow answered Jul 29, 2012 at 17:27 Lukas's user avatar LukasLukas 1,3567 gold badges24 silver badges50 bronze badges Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • Your docs are your infrastructure
  • Featured on Meta
  • More network sites to see advertising test [updated with phase 2]
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...
1 How to display hexadecimal value in Objective C with leading 0x? 2 datatype hexadecimal 0 Strange hex formatting in NSString 3 Hex NSString to char[] 3 Not of hexadecimal value 0xffff? 0 Objective-C double hex representation 0 How to convert NSString with hex values to Char - Objective C 0 Hexadecimal to Binary in Objective C 0 NOT operation to NSString with Hexadecimal value Objective-C 1 Conversion from NSString to Hex

Hot Network Questions

  • What is 擦边 as in the case of the athlete champion turned dancer?
  • Standard SMD chip resistor with higher power in the same package
  • How do I add a trusted check constraint quickly
  • What's a good way to append a nonce to ciphertext in Python for AES GCM in Python?
  • What powers do police have to force people onto trains?
  • Lebesgue equivalence class with no continuous representative
  • Schrödinger's cat ++
  • Can one publication contribute to two separate grants?
  • Preventing resulting shapefile being added to ArcGIS Pro map by ArcPy
  • In the USCF, is it legal to roll a 20 sided die to decide the first move?
  • Special stairs of infinites and infinitesimals
  • Did renaissance actors learn their parts by heart?
  • Why is it safe to soak an electric motor in isopropyl alcohol but not distilled water?
  • How do different observers decide if they are looking at the same thing?
  • Fastest quasi-random function in 6502?
  • Configure Linux to regularly sync cached data to disk
  • Static vs dynamic certificate pinning
  • Solving Large size problems in industry: Tips and Tricks
  • Is Holy Terra Earth?
  • Can you make 5 x 3 “magic” rectangles?
  • What mechanism could cause a person not to cast a reflection?
  • How can I fix this leaking connection in the heating system?
  • What is the polymorph reached by letting the chocolate cool down?
  • Rational divisors on Calabi–Yau threefolds
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-objectivec

Từ khóa » C 0x02