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

  • Home
  • SEARCH
  • 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 7527753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:11:02+00:00 2026-05-30T04:11:02+00:00

I have a byte array as input. This should be the binary representation of

  • 0

I have a byte array as input. This should be the binary representation of standard conforming UTF8 HTML. It is, but most of the time only. Sometimes it also contains embedded nulls (\x0 character or NUL). This is not under my control. I need to transform this byte array into a string.

Tried so far:

  • Obviously using a StreamReader or TextReader does not work as it stops on hitting the first NUL
  • Encoding.UTF8.GetString does not work either – also stops on the first NUL

What worked but is rather unelegant:

   mynewarray = myoldarray.Where( x => x!=0).ToArray();
   var output = Encoding.UTF8.GetString(mynewarray);

Is there a more elegant way to do this excepted in creating a new byte array skipping the NUL chars and then use one of the solutions above? The byte array can be pretty big, more than 2-4 Mb… The MSDN tells that Strings might actually contain embedded NUL but does not tell what are the best approaches to handle such strings.

  • 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-05-30T04:11:04+00:00Added an answer on May 30, 2026 at 4:11 am

    Your string is already right. It will contain the NUL characters. But when you use a string with the included NUL chars you will get all kind of problems.

    Encoding.UTF8.GetString does not stop at \0 as you see in my example.

    See what happens when I output such a string:

      var text = new byte[]{65, 65, 0, 65};
      var s = Encoding.UTF8.GetString(text);
      Console.WriteLine("len is: " + s.Length + " chars");
      Console.WriteLine("text: '" + s + "'");      
      Console.WriteLine("this line doesn't appear because NUL was sent to console");
    

    output is:

    len is: 4 chars
    text: 'AA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have byte array as input. I would like to convert that array to
I have a byte array filled from a file uploaded. But, in another part
Suppose i have input array byte A[50]; i have put three diffrent data types
Most of the time when we read the file stream into a byte array,
I have this uncompressed byte array: 0E 7C BD 03 6E 65 67 6C
I have an input byte array which I would like to feed to the
I have a byte array in memory, read from a file. I would like
I have a byte[] array, the contents of which represent a TIFF file (as
I have a byte array that represents a complete TCP/IP packet. For clarification, the
I have a byte array highlighted below, how do I insert it into a

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.