Solved

byte[] 50 characters to null terminated string

Posted on 2006-11-29
4
1,291 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Page navigation in windows phone 8.1 application not working. 5 24
Video Player 11 23
Hey!!!! 1 20
How to make a Gridview column hidden (visibility false) in the HTML code? 2 16
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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