473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

converting char[] to string

I have a char[] that i convert to a string as follows
m_tHeader.m_sshortname is defined as char[8];

string result = new string(m_tHeader.m_sshortname);

The problem is that any '\0' chars in the array remain in the string and
it's not terminated properly.

Is there a nice way to trim these off (string.trim doesnt work) rather than
my very nasty code below

while((result.Length > 0)&&(result[result.Length - 1] == '\0'))

result = result.Remove(result.Length - 1,1);

thanks


Mar 21 '06 #1
2 19302
Hi claire,

Yes, the C# string does not use a null terminator. That is, a C# string may
contain null characters.

Depending on what you want to do with the contents of the string, there are
several things you can do.

First, you can always convert a C# string to a char* (pointer to an array of
char), in which the first null terminator becomes the end of the string. Of
course, this requires some unsafe code to do, but I mentioned it in case it
might come in handy at some point in the future.

Second, you can initialize the string by using IndexOf when initializing the
string:

string result = new string(Array.IndexOf(m_tHeader.m_sshortname, '0');

or if you have .Net 2.0:

string result = new string(Array.IndexOf<char>(m_tHeader.m_sshortname, '0');

This overload will truncate the string you get at the first null 0. Of
course, you may have to check to see for sure whether there *is* a null zero
in the string, to prevent the third argument from being -1:

int index = Array.IndexOf<char>(m_tHeader.m_sshortname, '0')
string result = new string(m_tHeader.m_sshortname, 0, (index < 0 ? 0 :
index))

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
"claire" <so*****@microsoft.com> wrote in message
news:u5**************@TK2MSFTNGP10.phx.gbl...
I have a char[] that i convert to a string as follows
m_tHeader.m_sshortname is defined as char[8];

string result = new string(m_tHeader.m_sshortname);

The problem is that any '\0' chars in the array remain in the string and
it's not terminated properly.

Is there a nice way to trim these off (string.trim doesnt work) rather
than my very nasty code below

while((result.Length > 0)&&(result[result.Length - 1] == '\0'))

result = result.Remove(result.Length - 1,1);

thanks

Mar 21 '06 #2
claire wrote:
I have a char[] that i convert to a string as follows
m_tHeader.m_sshortname is defined as char[8];

string result = new string(m_tHeader.m_sshortname);

The problem is that any '\0' chars in the array remain in the string and
it's not terminated properly.

Is there a nice way to trim these off (string.trim doesnt work)


Well, String.Trim does work - I suspect you're not using it properly.

Are you sure that the char array only has null characters at the end?
If so, just use:

string result = new string (m_tHeader.m_sshortname).TrimEnd('\0');

That should work fine (and does in my test code).

Jon

Mar 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
3
by: fakeprogress | last post by:
How would I go about converting this C code to C++? /* LIBRARY is an array of structures */ /* This function compares 'tcode' with */ /* existing codes in the array. */ /* It...
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
5
by: Hans Mull | last post by:
Hi! How can I convert a string to a const unsigned char*? (string::c_str() converts the string to a signed char) Thanks in advance, Hans
1
by: vcbytes | last post by:
I am having a problem with the following code: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { String^ texts = textBox1->Text; char *text =...
4
by: screamer81 | last post by:
Hello, I've a SDK functions description for a scanner and I try to convert unmanaged DLL C++ functions to c#. I converted all except one of them. This function is getting a struct parameter. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.