Getting Rid of the Nulls

  • Thread starter Thread starter Drew
  • Start date Start date
D

Drew

I have been using System.Text.Encoding.Unicode.GetString(byte[],0,72)
to converter a byte array from the registry to a string.

This works, but I end up with a bunch of null characters after
the "good" string data that I need to parse. This is evidenced
by the fact that string.length returns 36 when the actual string
data I need to work with is usually much less.

I tried using String.Trim(null) but this doesn't achieve the desired result.

The only thing I can think of is to convert the string to a char array
and loop through until I hit a null character.

Is there a better way to get rid of the nulls?

If I could just get the actual length of the string that would help.

Thanks,

Drew
 
Drew,

I believe the GetString methods asxsumed your byte array to contain the
data needed to build the correct string, and if not then you will get the
extra
padding. The lenght comes from the fact that you tell it 72 bytes and each
unicode character is two bytes each -> 72/2 = 36 characters.

HTH,

//Andreas
 
Drew:

It looks as though Andreas has your root problem fixed. If you still need
to perform the trim, you can use
string newString = oldString.Trim('\0') (note the single quotes, not double
quotes)

I'm not sure why String.Trim(null); doesn't work. According to the help, it
should be exactly what you are after.
 
Drew said:
I have been using System.Text.Encoding.Unicode.GetString(byte[],0,72)
to converter a byte array from the registry to a string.

This works, but I end up with a bunch of null characters after
the "good" string data that I need to parse. This is evidenced
by the fact that string.length returns 36 when the actual string
data I need to work with is usually much less.

If the actual string is less than 36 characters, why are you decoding
72 bytes? Where did the number 72 come from?
I tried using String.Trim(null) but this doesn't achieve the desired result.

No, it wouldn't. A null reference isn't the same as a null character,
\0. String.Trim('\0') should be fine though.
The only thing I can think of is to convert the string to a char array
and loop through until I hit a null character.

Is there a better way to get rid of the nulls?

If I could just get the actual length of the string that would help.

Registry strings are always null-suffixed - so you can just decode two
bytes fewer in order to avoid having a null at the end of the string.
However, it doesn't sound like this was your real problem, if the data
is "usually much less".
 
If the actual string is less than 36 characters, why are you decoding
72 bytes? Where did the number 72 come from?

Well, it's a little complicated, but this byte array contains several values.

I determined through trial and error that the maximum length of the
string I need is 72 bytes. Which is located in the first 72 bytes.

However, the actual string is a variable (Owner's Name) so it could
be any length between 0 and 72 bytes.

Are you saying that there is a way to determine the exact length
of the actual string without the nulls before I use getString ?

No, it wouldn't. A null reference isn't the same as a null character,
\0. String.Trim('\0') should be fine though.

Oh, I will try that next. Thanks.

Drew
 
In C#, null is the default reference type value, and refers to a null
reference. It is not 0.

String.Trim takes a char[]. If you pass null, you're passing no characters.

-mike
MVP

J.Marsch said:
Drew:

It looks as though Andreas has your root problem fixed. If you still need
to perform the trim, you can use
string newString = oldString.Trim('\0') (note the single quotes, not
double
quotes)

I'm not sure why String.Trim(null); doesn't work. According to the help,
it
should be exactly what you are after.


Drew said:
I have been using System.Text.Encoding.Unicode.GetString(byte[],0,72)
to converter a byte array from the registry to a string.

This works, but I end up with a bunch of null characters after
the "good" string data that I need to parse. This is evidenced
by the fact that string.length returns 36 when the actual string
data I need to work with is usually much less.

I tried using String.Trim(null) but this doesn't achieve the desired result.

The only thing I can think of is to convert the string to a char array
and loop through until I hit a null character.

Is there a better way to get rid of the nulls?

If I could just get the actual length of the string that would help.

Thanks,

Drew
 
In C#, null is the default reference type value, and refers to a null
reference. It is not 0.
String.Trim takes a char[]. If you pass null, you're passing no
characters.

I understand that part, But check this line from the help:
'If trimChars is a null reference (Nothing in Visual Basic), white space
characters are removed instead."

That's why we expected String.Trim(null) to remove whitespace, not because
we confused \0 with null.


Michael Giagnocavo said:
-mike
MVP

J.Marsch said:
Drew:

It looks as though Andreas has your root problem fixed. If you still need
to perform the trim, you can use
string newString = oldString.Trim('\0') (note the single quotes, not
double
quotes)

I'm not sure why String.Trim(null); doesn't work. According to the help,
it
should be exactly what you are after.


Drew said:
I have been using System.Text.Encoding.Unicode.GetString(byte[],0,72)
to converter a byte array from the registry to a string.

This works, but I end up with a bunch of null characters after
the "good" string data that I need to parse. This is evidenced
by the fact that string.length returns 36 when the actual string
data I need to work with is usually much less.

I tried using String.Trim(null) but this doesn't achieve the desired result.

The only thing I can think of is to convert the string to a char array
and loop through until I hit a null character.

Is there a better way to get rid of the nulls?

If I could just get the actual length of the string that would help.

Thanks,

Drew
 
Jon:

I think that this is also a documentation error:
The help says this:
"If trimChars is a null reference (Nothing in Visual Basic), white space
characters are removed instead."


We are not confusing '\0' with null -- those are certainly different things.
However, What I get from the help is that if I pass null, _all_ whitespace
characters should be removed: '\0', a space, '\t', \n', '\l' and so on.
Whereas if I pass '\0', I expect it to look for the null char only.


Jon Skeet said:
Drew said:
I have been using System.Text.Encoding.Unicode.GetString(byte[],0,72)
to converter a byte array from the registry to a string.

This works, but I end up with a bunch of null characters after
the "good" string data that I need to parse. This is evidenced
by the fact that string.length returns 36 when the actual string
data I need to work with is usually much less.

If the actual string is less than 36 characters, why are you decoding
72 bytes? Where did the number 72 come from?
I tried using String.Trim(null) but this doesn't achieve the desired
result.

No, it wouldn't. A null reference isn't the same as a null character,
\0. String.Trim('\0') should be fine though.
The only thing I can think of is to convert the string to a char array
and loop through until I hit a null character.

Is there a better way to get rid of the nulls?

If I could just get the actual length of the string that would help.

Registry strings are always null-suffixed - so you can just decode two
bytes fewer in order to avoid having a null at the end of the string.
However, it doesn't sound like this was your real problem, if the data
is "usually much less".
 
J.Marsch said:
Jon:

I think that this is also a documentation error:
The help says this:
"If trimChars is a null reference (Nothing in Visual Basic), white space
characters are removed instead."

We are not confusing '\0' with null -- those are certainly different things.
However, What I get from the help is that if I pass null, _all_ whitespace
characters should be removed: '\0', a space, '\t', \n', '\l' and so on.
Whereas if I pass '\0', I expect it to look for the null char only.

I don't believe '\0' is considered a whitespace character. Certainly
Char.IsWhiteSpace('\0') returns false. That's why it wasn't working.
 
Now that makes sense. I made the mistake of assuming that '\0' is
considered to be whitespace. That would certainly clear up the issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top