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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:39:16+00:00 2026-05-29T04:39:16+00:00

I have a byte array that’s been initialized with 0xFF in each byte: for

  • 0

I have a byte array that’s been initialized with 0xFF in each byte:

for (int i = 0; i < buffer.Length; i++)
{
    buffer[i] = 0xFF;
}

Once this byte array has been filled with valid data, I need to extract an ASCII string that’s stored at offset 192 and may be up to 32 characters in length. I’m doing this like so:

ASCIIEncoding enc = new ASCIIEncoding();
stringToRead = enc.GetString(buffer, 192, 32);

This works but I need to strip off the trailing bytes that contain 0xFF to avoid the string looking something like “John Smith??????????????????????”. Is there a function in .NET that provides this ability? Something like the String.TrimEnd() function perhaps or am I looking at a regex to do this?

  • 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-29T04:39:18+00:00Added an answer on May 29, 2026 at 4:39 am

    I would suggest just finding out how long the string will really be:

    int firstFF = Array.IndexOf(buffer, (byte) 0xff, 192);
    if (firstFF == -1)
    {
        firstFF = buffer.Length;
    }
    stringToRead = Encoding.ASCII(buffer, 192, firstFF - 192);
    

    I would not try to give Encoding.ASCII bytes which aren’t valid ASCII-encoded text. I don’t know offhand what it would do with them – I suspect it would convert them to ? to show the error (as suggested by your existing output), but then you wouldn’t be able to tell the difference between that and real question marks. For example:

    byte[] data = { 0x41, 0x42, 0x43, 0xff, 0xff };
    string text = Encoding.ASCII.GetString(data);
    Console.WriteLine(text.Contains((char) 0xff)); // False
    Console.WriteLine(text.TrimEnd((char) 0xff).Length); // Still 5...
    

    Now you could create an encoding which used some non-ASCII replacement character… but that’s a lot of hassle when you can just find where the binary data stops being valid.

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

Sidebar

Related Questions

I have a control that has a byte array in it. Every now and
I have a byte array property that must be a certain length. I am
This is sort of an odd request. I have a byte array that I
I have a byte array that has hex values and I initially put those
I have a program in C++ that has a BYTE array that stores some
I have a program that has a byte array that varies in size, but
I have a byte array that represents a complete TCP/IP packet. For clarification, the
I have a byte array that may or may not have null bytes at
I have byte array as input. I would like to convert that array to
I have an SHA-1 byte array that I would like to use in 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.