473,320 Members | 2,107 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,320 software developers and data experts.

Getting Rid of the Nulls

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


Nov 16 '05 #1
5 3469
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" <so*****@hotmail.com> skrev i meddelandet
news:uJ**************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #2
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" <so*****@hotmail.com> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #3
Drew <so*****@hotmail.com> wrote:
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".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
> 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

Nov 16 '05 #5
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" <je****@ctcdeveloper.com> wrote in message
news:Ov**************@TK2MSFTNGP11.phx.gbl...
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" <so*****@hotmail.com> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
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



Nov 16 '05 #6

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

Similar topics

3
by: aaj | last post by:
Hi I am probably going to regret asking this because I'm sure you are going to tell me my design is bad 8-) ah well we all have to learn.... anyway I often use Nulls as a marker to see if...
6
by: mike | last post by:
I'm doing what I thought was a simple GROUP BY summary of fairly simple data and the my numbers aren't working out Some results are showing up <NULL> when I know the data is in the database ...
0
by: Rhino | last post by:
I am working with SQL Functions in DB2 for Windows/Linux/UNIX (V8.2.1) and am having a problem setting input parameters for SQL Functions to null in the Development Center. My simple function,...
1
by: PST | last post by:
Here's a problem I'm trying to deal with: I'm working on a Frontpage 2000 website for a boat handicapping system, built in Access 97. What I'm trying to accomplish is: The user enters a...
37
by: MLH | last post by:
For example: Nz(,0) returns "300" if the value in field is 300 (currency data type) and "0" if the value is zero or null. I get strings in the query output - they are all left aligned and I...
5
by: Drew | last post by:
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...
1
by: Jeff | last post by:
Hello all, I have an SQL Insert statement I'm trying to run via ExecuteNonQuery. It works fine so long as there are no nulls values. Here is the statement...(its a shortened version of what i...
14
by: rodchar | last post by:
hey all, i have a winapp that imports a text file into a typed dataset using the data adapter. some of the values are null. is there a way to remove the null values and put an empty string for...
6
by: Cliff72 | last post by:
I need to fill in the nulls in the batch field the value from the record immediately preceding the null one ie replace the nulls with the preceding value until I hit a record with a value in...
5
by: ph3ng | last post by:
Hi I was executing sp_help on a server (ran against master database) and i get the following message: Associated statement is not prepared(HY007) Anyone got any idea what is wrong?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.