could you explain why?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Wang

    #1

    could you explain why?

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

    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


  • Michael Mayer

    #2
    Re: could you explain why?

    "David Wang" <wgwangb@yahoo. com> wrote in message
    news:uOgANe%23V DHA.532@TK2MSFT NGP09.phx.gbl.. .
    [color=blue]
    > string convert2string( byte [] bys)
    > {
    > string string1 = Encoding.ASCII. GetString(bys, 0,[/color]
    30).TrimEnd(nul l);[color=blue]
    >
    > return string1;
    > }
    >
    > byte [] allzero = new byte [40];
    > string dbstr = convert2string( byte);[/color]
    TYPO: I think you mean convert2string( allzero)[color=blue]
    >
    > After I get dbstr(which is empty),[/color]
    It is NOT empty... Just because it has non-printable characters
    doesn't make it empty.
    If you do:
    Console.WriteLi ne("Length is: " + dbstr.Length);

    You'll find it has 30 characters (I assume all are ASCII code 0).
    [color=blue]
    > byte [] allzero = new byte [40];[/color]
    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



    Comment

    Working...