Solved

byte[] 50 characters to null terminated string

Posted on 2006-11-29
4
1,290 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
ID: 18036982
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
ID: 18037139
If there are only 3 characters, there are 47 null characters.

D
0
 
LVL 6

Accepted Solution

by:
e1v earned 500 total points
ID: 18037152
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Round up to 100% in .NET 10 84
Iterate a dictionnary to change values 4 53
Access properties in nested observable collections 8 23
Icons and Colors for Terms 3 24
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

16 Experts available now in Live!

Get 1:1 Help Now