Solved

byte[] 50 characters to null terminated string

Posted on 2006-11-29
4
1,288 Views
Last Modified: 2012-06-21
Hi,

C# .NET 2.0

I have a 50 character byte array, containing a null terminated set of chars. To convert it to a string, I decode it using standard UTF8Encoders ...

System.Text.UTF8Encoding decode = new System.Text.UTF8Encoding();
decode.GetString(myChars);

However because it is a 50 byte array I get back
SomeText\0\0\0\0\0\0\0\0\0\0\0\0\0 ... \0
The \0 being the NULL character. It does not return a string of length 8

Is there a simple way just to get the null terminated string out of the byte array without all the extra padding? I would have assumed the GetString would have does that, but when looking at the text output in binary mode, there is all the extra padding..

Cheers,
Derek
0
Comment
Question by:java_programmer
  • 2
4 Comments
 
LVL 6

Expert Comment

by:e1v
Comment Utility
Can't you just remove it?

string s= decode.GetString(myChars);
s = s.Remove(s.IndexOf('\0'));
0
 
LVL 2

Author Comment

by:java_programmer
Comment Utility
If there are only 3 characters, there are 47 null characters.

D
0
 
LVL 6

Accepted Solution

by:
e1v earned 500 total points
Comment Utility
s.Remove(s.IndexOf('\0')) will remove every null character to the right of the first null character, so that should work
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now