Some notes on software development... RSS 2.0
# Wednesday, 02 April 2008

If you have a char array that contains the string you want but is padded out with null terminators e.g. '\0' you can use the String.TrimEnd method to get the exact string you need.

A 12 element char array contains the following chars: M y S t r i n g \0 \0 \0 \0

To convert to a string you can use the String constructor to pass in a Char[] type. Unfortunately a .NET string can happily contain null characters so your resulting string would be MyString\0\0\0\0
Using the TrimEnd('\0') will return the correctly terminated string.


Note: Outputting a string containing null characters to other classes like form controls may automatically terminate the strings correctly at the first null character but behind the scenes the string still contains the null characters.

MSDN String.TrimEnd Method
Strings In .NET and C# (Jon Skeet)

Wednesday, 02 April 2008 09:46:35 (GMT Standard Time, UTC+00:00)  #    -

Sign In
Total Posts: 116
This Year: 0
This Month: 0
This Week: 0
Comments: 36
About the author/Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2019
Hadrian Phillips

All Content © 2019, Hadrian Phillips
DasBlog theme 'Business' created by Christoph De Baene (delarou)