Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:52:15+00:00 2026-06-01T16:52:15+00:00

I want to concatenate null characters with regular characters like so: NSString *message =

  • 0

I want to concatenate null characters with regular characters like so:

NSString *message = @"ABC";
NSUInteger length = [message length];
char char4 = length;
char char3 = length >> 8;
char char2 = length >> 16;
char char1 = length >> 24;
message = [NSString stringWithFormat:@"%c%c%c%c%@",char4,char3,char2,char1,message];

The problem is that the string stays at length 4 (and looks like ¿ABC). How can I edit this code so that the null characters are also appended to the string?

In reply to Mark’s comment

What I am trying to do here is append the length of the string to the beginning of the string, not in numerical format, but in the format of ascii characters (almost like a base 256 number). I would use the format, @"%04c%@",length,message, the problem is that the resulting string would be 000¿ABC and zeroes have ascii value (48 in decimal to be exact) and so that defeats the purpose. The ASCII character that has decimal value 0 is the null character (\0) so I have to use that instead of 0. It is necessary that I have those leading null characters.

For the purposes of what I’m trying to accomplish, the following code works

NSUInteger length = [message length];
char char4 = length;
char char3 = length >> 8;
char char2 = length >> 16;
char char1 = length >> 24;

if (char4 == '\0')
    message = [NSString stringWithFormat:@"\0%@",message];
else
    message = [NSString stringWithFormat:@"%c%@",char4,message];

if (char3 == '\0')
    message = [NSString stringWithFormat:@"\0%@",message];
else
    message = [NSString stringWithFormat:@"%c%@",char3,message];

if (char2 == '\0')
    message = [NSString stringWithFormat:@"\0%@",message];
else
    message = [NSString stringWithFormat:@"%c%@",char2,message];

if (char1 == '\0')
    message = [NSString stringWithFormat:@"\0%@",message];
else
    message = [NSString stringWithFormat:@"%c%@",char1,message];

But, if anybody can contribute something shorter or more intuitive, that’d be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:52:16+00:00Added an answer on June 1, 2026 at 4:52 pm

    Don’t do this. It’s a bad idea. For example, NSString is conceptually built on 16-bit Unicode characters, but you’re trying to prepend bytes. Note that there’s nothing guaranteeing that NSString’s internal representation is UTF-16 or any other specific encoding. In any case, when the string is written out it has to be converted to whatever encoding and that is unlikely to preserve your length prefixing in a way you can predict.

    Use an NSData with the length in the first 4 (or maybe 8 would be better for a 64-bit length) bytes followed by the string in a particular encoding. I recommend UTF-8.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two char variables, and later on I want to concatenate
i want to concatenate 2 strings in c++, i can't use char*. I tried
I want to concatenate strings in Haskell and also integers from function like this:
I want to concatenate two const char* but when I do this I got
I want to concatenate a piece of text, for example The answer is with
I have an array of strings: @array I want to concatenate all strings beginning
want to know why String behaves like value type while using ==. String s1
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
If i want to concatenate 2 strings in C, do i have to allocate
SQL Server 2008 - I want to concatenate four columns into delimited values, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.