473,396 Members | 1,809 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,396 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 3473
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.