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.

could you explain why?

HI,
Please look at following code example:
string convert2string(byte [] bys)
{
string string1 = Encoding.ASCII.GetString(bys, 0, 30).TrimEnd(null);

return string1;
}

byte [] allzero = new byte [40];
string dbstr = convert2string(byte);

After I get dbstr(which is empty), I write it to SQL database. When I read
this from database again, and serilize to XML message, I see
����.....��� which means dbstr actually was not empty,
it filled with all "0" inside. Can anybody explain why? How can I get rid of
it? Thank you.

David
Nov 15 '05 #1
3 2274
"David Wang" <wg*****@yahoo.com> wrote in message
news:uO***************@TK2MSFTNGP09.phx.gbl...
string convert2string(byte [] bys)
{
string string1 = Encoding.ASCII.GetString(bys, 0, 30).TrimEnd(null);
return string1;
}

byte [] allzero = new byte [40];
string dbstr = convert2string(byte); TYPO: I think you mean convert2string(allzero)
After I get dbstr(which is empty), It is NOT empty... Just because it has non-printable characters
doesn't make it empty.
If you do:
Console.WriteLine("Length is: " + dbstr.Length);

You'll find it has 30 characters (I assume all are ASCII code 0).
byte [] allzero = new byte [40];

creates an array of value-types. So you have 40 bytes, each with a
default value of 0.

I suspect you'll want to add a check in convert2string to look for
all-zero case and return "" instead.

HTH,
Mike

Nov 15 '05 #2
David Wang <wg*****@yahoo.com> wrote:
Please look at following code example:
string convert2string(byte [] bys)
{
string string1 = Encoding.ASCII.GetString(bys, 0, 30).TrimEnd(null);

return string1;
}

byte [] allzero = new byte [40];
string dbstr = convert2string(byte);

After I get dbstr(which is empty), I write it to SQL database. When I read
this from database again, and serilize to XML message, I see
����.....��� which means dbstr actually was not empty,
it filled with all "0" inside. Can anybody explain why? How can I get rid of
it? Thank you.


Instead of calling TrimEnd (null) which will do nothing, call
TrimEnd ('\0')

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
David Wang <wg*****@yahoo.com> wrote:
Please look at following code example:
string convert2string(byte [] bys)
{
string string1 = Encoding.ASCII.GetString(bys, 0, 30).TrimEnd(null);

return string1;
}

byte [] allzero = new byte [40];
string dbstr = convert2string(byte);

After I get dbstr(which is empty), I write it to SQL database. When I read
this from database again, and serilize to XML message, I see
����.....��� which means dbstr actually was not empty,
it filled with all "0" inside. Can anybody explain why? How can I get rid of
it? Thank you.


Instead of calling TrimEnd (null) which will do nothing, call
TrimEnd ('\0')

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

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

Similar topics

4
by: John Dean | last post by:
Hi I have been reading quite a lot of Python source code recently and I have come across a particular construct which I don't understand. I would be grateful if somebody could explain the reason...
4
by: Chris | last post by:
Hello Could anyone explain why the following: #footer ul { float : left; margin : 2px 0px 7px 28px; padding : 0px; width : 360px;
13
by: C++fan | last post by:
The following code is for list operation. But I can not understand. Could anyone explain the code for me? /* * List definitions. */ #define LIST_HEAD(name, type) struct name { type...
21
by: Gactimus | last post by:
Can anyone explain what the lines with the '*' by them do? ----------- #ifndef _COUNTER_H #define _COUNTER_H #include <iostream> using namespace std; class Counter
10
by: Jeff Boes | last post by:
I'm hoping there's someone here with experience in building the Visual Explain tool from Red Hat. I downloaded it and the J2 SDK, but when I attempt to follow the build instructions, I get messages...
1
by: Andrew | last post by:
Hello, friends, I am implementing web app security using asp.net 1.1, and I found the following source code from Yahoo! Mail login page: <form method="post"...
2
by: David | last post by:
We've developed a seismic-data processing app in VB.NET 1.1 which runs on XP Pro using SQL2K. Like all seismic data apps, it deals with HUGE amounts of binary data (we use NTFS sparse "flat...
1
by: active | last post by:
I tried to use the help on 'controls' to find out what it is used for but there is so many different uses for the word as to make the help useless. So I tried the following. It appears the...
18
by: kardon33 | last post by:
I have this bit of code and im not used to C and it would help if some one could explain it in lamence terms. typedef struct { uchar IP; // IP address ushort Port; // UDP port BYTE-SWAPPED...
4
by: Chuthu | last post by:
Could anyone explain me the following concepts in detail? 1. JAXP 2. BGRAPH 3. JBOSS work flow engine
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...

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.