Solved

byte[] 50 characters to null terminated string

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

792 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