911: byte [] manipulation

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

    #1

    911: byte [] manipulation

    HI all,
    I am a newbie to C#....
    I have a byte[] of variable length. Starting from byte[15] the array, i
    have the following content in the array:

    [username\0passw ord\0info\01234]

    How can I extract those string from the byte[]?

    Hope someone can give me some advice.

    Thanks.

    Martin

  • Yunus Emre ALPÖZEN [MCAD.NET]

    #2
    Re: byte [] manipulation

    There are several ways to accomplish this. U can use StringBuilder to append
    bytes u read from the array.

    Or you can convert byte array to a string.
    System.Text.Enc oding.Default.G etString method...

    or too much solutions...

    --

    Thanks,
    Yunus Emre ALPÖZEN
    BSc, MCAD.NET

    "Martin" <Martin@discuss ions.microsoft. com> wrote in message
    news:485C4779-42DA-46E4-B2E6-A48408AB916B@mi crosoft.com...[color=blue]
    > HI all,
    > I am a newbie to C#....
    > I have a byte[] of variable length. Starting from byte[15] the array, i
    > have the following content in the array:
    >
    > [username\0passw ord\0info\01234]
    >
    > How can I extract those string from the byte[]?
    >
    > Hope someone can give me some advice.
    >
    > Thanks.
    >
    > Martin
    >[/color]


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: byte [] manipulation

      Hi,

      you can use one of the XXXEncoding ( ASCIIEncoding , UTF8Encoding )
      GetString method.


      Cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation




      "Martin" <Martin@discuss ions.microsoft. com> wrote in message
      news:485C4779-42DA-46E4-B2E6-A48408AB916B@mi crosoft.com...[color=blue]
      > HI all,
      > I am a newbie to C#....
      > I have a byte[] of variable length. Starting from byte[15] the array, i
      > have the following content in the array:
      >
      > [username\0passw ord\0info\01234]
      >
      > How can I extract those string from the byte[]?
      >
      > Hope someone can give me some advice.
      >
      > Thanks.
      >
      > Martin
      >[/color]


      Comment

      • Martin

        #4
        Re: byte [] manipulation

        But since the string comes in at the 15 bytes of the array and end by a
        \0...and the other string start right after the prior \0, how could i
        effectively pass the content between those into a stringbuilder?

        "Yunus Emre ALPÖZEN [MCAD.NET]" wrote:
        [color=blue]
        > There are several ways to accomplish this. U can use StringBuilder to append
        > bytes u read from the array.
        >
        > Or you can convert byte array to a string.
        > System.Text.Enc oding.Default.G etString method...
        >
        > or too much solutions...
        >
        > --
        >
        > Thanks,
        > Yunus Emre ALPÖZEN
        > BSc, MCAD.NET
        >
        > "Martin" <Martin@discuss ions.microsoft. com> wrote in message
        > news:485C4779-42DA-46E4-B2E6-A48408AB916B@mi crosoft.com...[color=green]
        > > HI all,
        > > I am a newbie to C#....
        > > I have a byte[] of variable length. Starting from byte[15] the array, i
        > > have the following content in the array:
        > >
        > > [username\0passw ord\0info\01234]
        > >
        > > How can I extract those string from the byte[]?
        > >
        > > Hope someone can give me some advice.
        > >
        > > Thanks.
        > >
        > > Martin
        > >[/color]
        >
        >
        >[/color]

        Comment

        • Yunus Emre ALPÖZEN [MCAD.NET]

          #5
          Re: byte [] manipulation

          using System;
          using System.Text;

          namespace SampleApplicati on
          {
          class Test
          {
          static byte[] CreateByteArray ()
          {
          return
          Encoding.Defaul t.GetBytes("012 345678901234use rname\0password \01234");
          }
          static void Main()
          {
          byte[] buffer = CreateByteArray ();
          ///
          /// Username has a length at 8
          ///
          Console.WriteLi ne(
          Encoding.Defaul t.GetString(buf fer,15,8));
          Console.WriteLi ne();

          ///
          /// Or You Don't know length
          ///
          StringBuilder sb = new StringBuilder() ;
          int index = 15;
          while(index<buf fer.Length)
          {
          if (buffer[index]=='\0' )
          {
          if (sb.Length>0)
          {
          Console.WriteLi ne(sb.ToString( ));
          sb.Remove(0,sb. Length);
          }
          }
          else
          sb.Append((char )buffer[index]);
          index++;
          }
          if (sb.Length>0)
          Console.WriteLi ne(sb.ToString( ));
          Console.Read();
          }
          }
          }

          --

          Thanks,
          Yunus Emre ALPÖZEN
          BSc, MCAD.NET

          "Martin" <Martin@discuss ions.microsoft. com> wrote in message
          news:7568AA12-9E75-4F2B-932F-F26B8C54271C@mi crosoft.com...[color=blue]
          > But since the string comes in at the 15 bytes of the array and end by a
          > \0...and the other string start right after the prior \0, how could i
          > effectively pass the content between those into a stringbuilder?
          >
          > "Yunus Emre ALPÖZEN [MCAD.NET]" wrote:
          >[color=green]
          >> There are several ways to accomplish this. U can use StringBuilder to
          >> append
          >> bytes u read from the array.
          >>
          >> Or you can convert byte array to a string.
          >> System.Text.Enc oding.Default.G etString method...
          >>
          >> or too much solutions...
          >>
          >> --
          >>
          >> Thanks,
          >> Yunus Emre ALPÖZEN
          >> BSc, MCAD.NET
          >>
          >> "Martin" <Martin@discuss ions.microsoft. com> wrote in message
          >> news:485C4779-42DA-46E4-B2E6-A48408AB916B@mi crosoft.com...[color=darkred]
          >> > HI all,
          >> > I am a newbie to C#....
          >> > I have a byte[] of variable length. Starting from byte[15] the array,
          >> > i
          >> > have the following content in the array:
          >> >
          >> > [username\0passw ord\0info\01234]
          >> >
          >> > How can I extract those string from the byte[]?
          >> >
          >> > Hope someone can give me some advice.
          >> >
          >> > Thanks.
          >> >
          >> > Martin
          >> >[/color]
          >>
          >>
          >>[/color][/color]


          Comment

          Working...