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 8675733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:01:07+00:00 2026-06-12T20:01:07+00:00

My question title isn’t clear, sorry. I tried ;) I have a binary string

  • 0

My question title isn’t clear, sorry. I tried 😉

  • I have a binary string that contains nul bytes.
  • I know the length of the string.
  • I have a function (from a library) encodes strings into another format.
  • I need to encode the binary string using the library function.
  • The library function doesn’t accept a length & assumes the string is nul terminated.
  • I know how to encode the nul bytes by hand (represent them as "\\000")

Take the pseudo code:

/* I have a string, and the length in bytes of that string */
char * data   = value->bytes;
long   length = value->length;

/* I need to use a function, but it doesn't handle embedded nuls */
char * encoded = lib_func_encode(data);
lib_free(encoded); // library function requires a free() routine be called

That doesn’t work, because the library function only encodes the first chunk of data up to the first nul byte. I could write a fairly horrible loop that keeps calling strlen() on data and counting until length bytes have been encoded, appending a "\\0000" string every time I need to encode some more bytes, realloc’ing some buffer to accomodate each extra chunk of encoded data. It feels like this is going to be a lot of code to get around a fairly simple problem. Coming from a language with concepts like split() (on the nul bytes) map() (to encode each chunk) and join() (using "\\000") I’m instinctively thinking about the cleanest solution that way… but nothing really exists in standard C to do things like that.

In a fictional functional language it might look like:

join("\\000",
     map(lambda(chunk){encode(chunk)}, split('\0', data)))

How would a seasoned C programmer write a routine like I need to write?

  • 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-12T20:01:08+00:00Added an answer on June 12, 2026 at 8:01 pm

    You can do it in multiple steps:

    1. Find all embedded nul bytes
    2. From this and the length you can calculate the length of a new string
    3. Allocate a new string of the calculated length
    4. Iterate over the binary string copying all non-nul characters, when reaching a nul character insert the special character sequence
    5. Terminate new string
    6. Call library function with new string
    7. Free the new string
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if the question isn't that clear in the title but I had a
The title to this question isn't written very well. Sorry 'bout that. I'm curious
The title of this question isn't so clear, but the code and question is
I know the question title isn't amazing, but I can't think of a better
I know the question title isn't the best. Let me explain. I do a
First of all: Sorry for my bad English! I know the title isn't the
I'm here again with another question/problem. I know that topic's title may be like
Okay the question title may not have made sense... mostly because I don't know
Sorry if the title isn't really related to my question well enough, I wasn't
Question is in the title. I'd just love to understand why this isn't supported?

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.