add a single byte to byte()

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

    add a single byte to byte()

    I am really do not know so much about byte/bit, etc.

    Question, if I defined a byte(), how can I add a single byte to it?

    what I want is that I have an array of bytes, I loop that array, look at the
    value of that individual byte, if that byte is what I want, then I insert it
    into the new byte(). How can I do it?

    --
    Kind regards

    Guoqi Zheng
    guoqi AT meetholland dot com




  • Hal Rosser

    #2
    Re: add a single byte to byte()

    redim the array - and -
    assign a byte to the new element

    "Guoqi Zheng" <no@sorry.nl> wrote in message
    news:%23F0557dl EHA.1936@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > I am really do not know so much about byte/bit, etc.
    >
    > Question, if I defined a byte(), how can I add a single byte to it?
    >
    > what I want is that I have an array of bytes, I loop that array, look at[/color]
    the[color=blue]
    > value of that individual byte, if that byte is what I want, then I insert[/color]
    it[color=blue]
    > into the new byte(). How can I do it?
    >
    > --
    > Kind regards
    >
    > Guoqi Zheng
    > guoqi AT meetholland dot com
    > Http://www.meetholland.com
    >
    >
    >[/color]


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.754 / Virus Database: 504 - Release Date: 9/6/2004


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: add a single byte to byte()

      * "Hal Rosser" <hmrosser@bells outh.net> scripsit:[color=blue]
      > redim the array - and -
      > assign a byte to the new element[/color]

      In this case, 'ReDim Preserve' is the way to go.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • David

        #4
        Re: add a single byte to byte()

        On 2004-09-08, Guoqi Zheng <no@sorry.nl> wrote:[color=blue]
        > I am really do not know so much about byte/bit, etc.
        >
        > Question, if I defined a byte(), how can I add a single byte to it?
        >
        > what I want is that I have an array of bytes, I loop that array, look at the
        > value of that individual byte, if that byte is what I want, then I insert it
        > into the new byte(). How can I do it?[/color]

        For resizable arrays, you really want to use
        System.Collecti ons.ArrayList. You can always convert back into an
        ordinary byte() after you've filled the ArrayList with the contents you
        want.

        If you only need to add a single byte one time, then ReDim Preserve will
        work fine, but it sounds like you're doing this in a loop, and
        performance will be terrible if you rely on ReDim here.


        Comment

        • Hal Rosser

          #5
          Re: add a single byte to byte()

          Agreed
          [color=blue]
          > In this case, 'ReDim Preserve' is the way to go.
          > />[/color]


          ---
          Outgoing mail is certified Virus Free.
          Checked by AVG anti-virus system (http://www.grisoft.com).
          Version: 6.0.754 / Virus Database: 504 - Release Date: 9/7/2004


          Comment

          • Cor Ligthert

            #6
            Re: add a single byte to byte()

            Guaoqi,

            Assume you have a text and want it to set as Asci in a byte array, than you
            get something as (roughly typed in this message so watch typos)

            \\\\
            dim myString as string = "Ik hoop dat dit helpt?"
            dim ByteArr(mystrin g.length) as byte
            for i as integer = 0 to mystring.length -1
            ByteArr(i) = Cbyte(asc(mystr string.substrin g(i,1)))
            next
            ///

            I hope this helps?

            Cor

            "Guoqi Zheng"
            ..[color=blue]
            > I am really do not know so much about byte/bit, etc.
            >
            > Question, if I defined a byte(), how can I add a single byte to it?
            >
            > what I want is that I have an array of bytes, I loop that array, look at[/color]
            the[color=blue]
            > value of that individual byte, if that byte is what I want, then I insert[/color]
            it[color=blue]
            > into the new byte(). How can I do it?
            >
            > --
            > Kind regards
            >
            > Guoqi Zheng
            > guoqi AT meetholland dot com
            > Http://www.meetholland.com
            >
            >
            >[/color]


            Comment

            • Guoqi Zheng

              #7
              Re: add a single byte to byte()

              Thanks, it sounds like exactly what I am looking for. An arraylist then back
              to byte()

              --
              Kind regards

              Guoqi Zheng
              guoqi AT meetholland dot com



              "David" <dfoster@woofix .local.dom> wrote in message
              news:slrncjv5fg .aof.dfoster@wo ofix.local.dom. ..[color=blue]
              > On 2004-09-08, Guoqi Zheng <no@sorry.nl> wrote:[color=green]
              > > I am really do not know so much about byte/bit, etc.
              > >
              > > Question, if I defined a byte(), how can I add a single byte to it?
              > >
              > > what I want is that I have an array of bytes, I loop that array, look at[/color][/color]
              the[color=blue][color=green]
              > > value of that individual byte, if that byte is what I want, then I[/color][/color]
              insert it[color=blue][color=green]
              > > into the new byte(). How can I do it?[/color]
              >
              > For resizable arrays, you really want to use
              > System.Collecti ons.ArrayList. You can always convert back into an
              > ordinary byte() after you've filled the ArrayList with the contents you
              > want.
              >
              > If you only need to add a single byte one time, then ReDim Preserve will
              > work fine, but it sounds like you're doing this in a loop, and
              > performance will be terrible if you rely on ReDim here.
              >
              >[/color]


              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: add a single byte to byte()

                David,
                Just be aware that an ArrayList with bytes in it is going to cause a lot of
                boxing & unboxing which may hurt performance.

                I would consider an array of bytes & ReDim Preserve, only over allocate the
                array, then keep track of how many actual elements are in the array. By over
                allocate I mean allocate 50 elements in the array although there is only 40
                actual elements in it.

                I will check if I have a good example. I have posted one or two examples
                previously.

                Hope this helps
                Jay


                "David" <dfoster@woofix .local.dom> wrote in message
                news:slrncjv5fg .aof.dfoster@wo ofix.local.dom. ..[color=blue]
                > On 2004-09-08, Guoqi Zheng <no@sorry.nl> wrote:[color=green]
                >> I am really do not know so much about byte/bit, etc.
                >>
                >> Question, if I defined a byte(), how can I add a single byte to it?
                >>
                >> what I want is that I have an array of bytes, I loop that array, look at
                >> the
                >> value of that individual byte, if that byte is what I want, then I insert
                >> it
                >> into the new byte(). How can I do it?[/color]
                >
                > For resizable arrays, you really want to use
                > System.Collecti ons.ArrayList. You can always convert back into an
                > ordinary byte() after you've filled the ArrayList with the contents you
                > want.
                >
                > If you only need to add a single byte one time, then ReDim Preserve will
                > work fine, but it sounds like you're doing this in a loop, and
                > performance will be terrible if you rely on ReDim here.
                >
                >[/color]


                Comment

                • Guoqi Zheng

                  #9
                  Re: add a single byte to byte()

                  This solution sounds fine as well.

                  But if I allocate 500 elements, and I used only 30, how can I remove the
                  extra 470 from that byte(), by Redim Preserve? and will these extra 470
                  elements cost performance problem?

                  --
                  Kind regards

                  Guoqi Zheng
                  guoqi AT meetholland dot com


                  "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                  news:%23o7HXMnl EHA.3968@TK2MSF TNGP11.phx.gbl. ..[color=blue]
                  > David,
                  > Just be aware that an ArrayList with bytes in it is going to cause a lot[/color]
                  of[color=blue]
                  > boxing & unboxing which may hurt performance.
                  >
                  > I would consider an array of bytes & ReDim Preserve, only over allocate[/color]
                  the[color=blue]
                  > array, then keep track of how many actual elements are in the array. By[/color]
                  over[color=blue]
                  > allocate I mean allocate 50 elements in the array although there is only[/color]
                  40[color=blue]
                  > actual elements in it.
                  >
                  > I will check if I have a good example. I have posted one or two examples
                  > previously.
                  >
                  > Hope this helps
                  > Jay
                  >
                  >
                  > "David" <dfoster@woofix .local.dom> wrote in message
                  > news:slrncjv5fg .aof.dfoster@wo ofix.local.dom. ..[color=green]
                  > > On 2004-09-08, Guoqi Zheng <no@sorry.nl> wrote:[color=darkred]
                  > >> I am really do not know so much about byte/bit, etc.
                  > >>
                  > >> Question, if I defined a byte(), how can I add a single byte to it?
                  > >>
                  > >> what I want is that I have an array of bytes, I loop that array, look[/color][/color][/color]
                  at[color=blue][color=green][color=darkred]
                  > >> the
                  > >> value of that individual byte, if that byte is what I want, then I[/color][/color][/color]
                  insert[color=blue][color=green][color=darkred]
                  > >> it
                  > >> into the new byte(). How can I do it?[/color]
                  > >
                  > > For resizable arrays, you really want to use
                  > > System.Collecti ons.ArrayList. You can always convert back into an
                  > > ordinary byte() after you've filled the ArrayList with the contents you
                  > > want.
                  > >
                  > > If you only need to add a single byte one time, then ReDim Preserve will
                  > > work fine, but it sounds like you're doing this in a loop, and
                  > > performance will be terrible if you rely on ReDim here.
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Cor Ligthert

                    #10
                    Re: add a single byte to byte()

                    Jay,

                    Your answer was my first answer as well, however I scratched it to make an
                    in my opinion nicer answer.

                    (I know it is not right to set a unicharacter to an asci value however as
                    sample goes this so easy)

                    The way you describe now is also in my opinion the best way to do it when
                    you do not know the length in advance.

                    Although an other alternative is of course doing it streaming.

                    :-)

                    Cor
                    [color=blue]
                    > David,
                    > Just be aware that an ArrayList with bytes in it is going to cause a lot[/color]
                    of[color=blue]
                    > boxing & unboxing which may hurt performance.
                    >
                    > I would consider an array of bytes & ReDim Preserve, only over allocate[/color]
                    the[color=blue]
                    > array, then keep track of how many actual elements are in the array. By[/color]
                    over[color=blue]
                    > allocate I mean allocate 50 elements in the array although there is only[/color]
                    40[color=blue]
                    > actual elements in it.
                    >
                    > I will check if I have a good example. I have posted one or two examples
                    > previously.
                    >
                    > Hope this helps
                    > Jay
                    >
                    >
                    > "David" <dfoster@woofix .local.dom> wrote in message
                    > news:slrncjv5fg .aof.dfoster@wo ofix.local.dom. ..[color=green]
                    > > On 2004-09-08, Guoqi Zheng <no@sorry.nl> wrote:[color=darkred]
                    > >> I am really do not know so much about byte/bit, etc.
                    > >>
                    > >> Question, if I defined a byte(), how can I add a single byte to it?
                    > >>
                    > >> what I want is that I have an array of bytes, I loop that array, look[/color][/color][/color]
                    at[color=blue][color=green][color=darkred]
                    > >> the
                    > >> value of that individual byte, if that byte is what I want, then I[/color][/color][/color]
                    insert[color=blue][color=green][color=darkred]
                    > >> it
                    > >> into the new byte(). How can I do it?[/color]
                    > >
                    > > For resizable arrays, you really want to use
                    > > System.Collecti ons.ArrayList. You can always convert back into an
                    > > ordinary byte() after you've filled the ArrayList with the contents you
                    > > want.
                    > >
                    > > If you only need to add a single byte one time, then ReDim Preserve will
                    > > work fine, but it sounds like you're doing this in a loop, and
                    > > performance will be terrible if you rely on ReDim here.
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: add a single byte to byte()

                      Guoqi,[color=blue]
                      > extra 470 from that byte(), by Redim Preserve?[/color]
                      I would use Redim Preserve, Array.Copy, or Buffer.BlockCop y.
                      [color=blue]
                      > and will these extra 470
                      > elements cost performance problem?[/color]
                      1 allocation of 500 bytes followed by a Redim Preserve is going to perform
                      better then 30 Redim Preserves. And possible better then 30 boxes & unboxes.
                      The only real way to tell is to use a profiling tool to check.


                      Most of the time the overallocating will be the better performing (both
                      memory usage & speed wise). Sometimes the ArrayList & boxing may perform
                      acceptable.
                      Obviously if you know before hand how much space you will need or can make a
                      very good estimate you have a better chance of it.

                      Remember that reallocating (Redim preserve) in both cases means that a new
                      buffer needs to be defined, then the old buffer needs to be copied to the
                      new array.

                      Hope this helps
                      Jay


                      "Guoqi Zheng" <no@sorry.nl> wrote in message
                      news:%23kyIASnl EHA.2492@TK2MSF TNGP15.phx.gbl. ..[color=blue]
                      > This solution sounds fine as well.
                      >
                      > But if I allocate 500 elements, and I used only 30, how can I remove the
                      > extra 470 from that byte(), by Redim Preserve? and will these extra 470
                      > elements cost performance problem?
                      >
                      > --
                      > Kind regards
                      >
                      > Guoqi Zheng
                      > guoqi AT meetholland dot com
                      > Http://www.meetholland.com
                      >
                      > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                      > news:%23o7HXMnl EHA.3968@TK2MSF TNGP11.phx.gbl. ..[color=green]
                      >> David,
                      >> Just be aware that an ArrayList with bytes in it is going to cause a lot[/color]
                      > of[color=green]
                      >> boxing & unboxing which may hurt performance.
                      >>
                      >> I would consider an array of bytes & ReDim Preserve, only over allocate[/color]
                      > the[color=green]
                      >> array, then keep track of how many actual elements are in the array. By[/color]
                      > over[color=green]
                      >> allocate I mean allocate 50 elements in the array although there is only[/color]
                      > 40[color=green]
                      >> actual elements in it.
                      >>
                      >> I will check if I have a good example. I have posted one or two examples
                      >> previously.
                      >>
                      >> Hope this helps
                      >> Jay
                      >>
                      >>
                      >> "David" <dfoster@woofix .local.dom> wrote in message
                      >> news:slrncjv5fg .aof.dfoster@wo ofix.local.dom. ..[color=darkred]
                      >> > On 2004-09-08, Guoqi Zheng <no@sorry.nl> wrote:
                      >> >> I am really do not know so much about byte/bit, etc.
                      >> >>
                      >> >> Question, if I defined a byte(), how can I add a single byte to it?
                      >> >>
                      >> >> what I want is that I have an array of bytes, I loop that array, look[/color][/color]
                      > at[color=green][color=darkred]
                      >> >> the
                      >> >> value of that individual byte, if that byte is what I want, then I[/color][/color]
                      > insert[color=green][color=darkred]
                      >> >> it
                      >> >> into the new byte(). How can I do it?
                      >> >
                      >> > For resizable arrays, you really want to use
                      >> > System.Collecti ons.ArrayList. You can always convert back into an
                      >> > ordinary byte() after you've filled the ArrayList with the contents you
                      >> > want.
                      >> >
                      >> > If you only need to add a single byte one time, then ReDim Preserve
                      >> > will
                      >> > work fine, but it sounds like you're doing this in a loop, and
                      >> > performance will be terrible if you rely on ReDim here.
                      >> >
                      >> >[/color]
                      >>
                      >>[/color]
                      >
                      >[/color]


                      Comment

                      • David

                        #12
                        Re: add a single byte to byte()

                        On 2004-09-09, Jay B. Harlow [MVP - Outlook] <Jay_Harlow_MVP @msn.com> wrote:[color=blue]
                        > David,
                        > Just be aware that an ArrayList with bytes in it is going to cause a lot of
                        > boxing & unboxing which may hurt performance.[/color]

                        Actually, it wouldn't cause a lot of either, just once per byte, which
                        is trivial unless you're dealing with huge nested loops. Overallocating
                        an array is faster, but unless you are absolutely sure you'll never need
                        a larger array (and that might be the case here), what should be a very
                        simple loop starts looking much uglier than it needs to be.

                        Aren't you the one who constantly posts the 80/20 rule here, or am I
                        confusing you with somebody else?


                        Comment

                        • Jay B. Harlow [MVP - Outlook]

                          #13
                          Re: add a single byte to byte()

                          David,[color=blue]
                          > Aren't you the one who constantly posts the 80/20 rule here, or am I
                          > confusing you with somebody else?[/color]

                          Yes I am. Which is why I stated "may hurt performance", then again today I
                          added to "the only real way to tell is to use a profiling tool to check".
                          [color=blue][color=green]
                          >> Just be aware that an ArrayList with bytes in it is going to cause a lot
                          >> of
                          >> boxing & unboxing which may hurt performance.[/color]
                          >
                          > Actually, it wouldn't cause a lot of either, just once per byte, which
                          > is trivial unless you're dealing with huge nested loops.[/color]
                          Correct. You will have a box for each byte you put into the ArrayList. Plus
                          an Unbox for each byte you remove.

                          So if you have a lot of bytes you will have a lot of boxing. In other words
                          at least 1 box & unbox for each byte. Correct?

                          Hope this helps
                          Jay



                          "David" <dfoster@woofix .local.dom> wrote in message
                          news:slrnck1ivm .dal.dfoster@wo ofix.local.dom. ..[color=blue]
                          > On 2004-09-09, Jay B. Harlow [MVP - Outlook] <Jay_Harlow_MVP @msn.com>
                          > wrote:[color=green]
                          >> David,
                          >> Just be aware that an ArrayList with bytes in it is going to cause a lot
                          >> of
                          >> boxing & unboxing which may hurt performance.[/color]
                          >
                          > Actually, it wouldn't cause a lot of either, just once per byte, which
                          > is trivial unless you're dealing with huge nested loops. Overallocating
                          > an array is faster, but unless you are absolutely sure you'll never need
                          > a larger array (and that might be the case here), what should be a very
                          > simple loop starts looking much uglier than it needs to be.
                          >
                          > Aren't you the one who constantly posts the 80/20 rule here, or am I
                          > confusing you with somebody else?
                          >
                          >[/color]


                          Comment

                          • Cor Ligthert

                            #14
                            Re: add a single byte to byte()

                            David,
                            [color=blue]
                            > Aren't you the one who constantly posts the 80/20 rule here, or am I
                            > confusing you with somebody else?[/color]

                            And the other one in other words is me. I am the one who uses almost the
                            arraylist in samples for everything while others point mostly than too the
                            hashtable, (where I use than when it is needed earlier the sortedlist).

                            When it is about a byte array, than there should be a reason why it is a
                            byte array, because it is in my opinion an archaic piece of storage, altough
                            very good for serializing and deserializing. For those you loads the
                            bytearray streaming and you do not need to set a byte by byte to an array.

                            You can see in this thread a message from me which describes a bytearray
                            with ASCI chareacter, however for what I would directly use an arraylist
                            when it was really needed to get the full unicode in a char. (Although I
                            really cannot find a reason for that)

                            In my idea in this case when there is really a bytearray needed, than we
                            should never go to the arraylist, because than the one who does that has
                            never investigated his problem. Because the byte is the value type you take
                            as last to choose from when it is not streaming.

                            However when it should be a bytearray and you do not know the size however
                            know that the maximum is 400bytes, than you should not even redim it.
                            Probably you will loose with the code to redim more bytes than what you can
                            earn with that.

                            However just my idea.

                            Cor




                            Comment

                            • David

                              #15
                              Re: add a single byte to byte()

                              On 2004-09-09, Jay B. Harlow [MVP - Outlook] <Jay_Harlow_MVP @msn.com> wrote:[color=blue]
                              > David,[color=green]
                              >> Aren't you the one who constantly posts the 80/20 rule here, or am I
                              >> confusing you with somebody else?[/color]
                              >
                              > Yes I am. Which is why I stated "may hurt performance", then again today I
                              > added to "the only real way to tell is to use a profiling tool to check".
                              >[color=green][color=darkred]
                              >>> Just be aware that an ArrayList with bytes in it is going to cause a lot
                              >>> of
                              >>> boxing & unboxing which may hurt performance.[/color]
                              >>
                              >> Actually, it wouldn't cause a lot of either, just once per byte, which
                              >> is trivial unless you're dealing with huge nested loops.[/color]
                              > Correct. You will have a box for each byte you put into the ArrayList. Plus
                              > an Unbox for each byte you remove.
                              >
                              > So if you have a lot of bytes you will have a lot of boxing. In other words
                              > at least 1 box & unbox for each byte. Correct?[/color]

                              Yep, it increases O(n) as the number of bytes increases. It's a rare
                              app where such a thing is a major concern. Though, certainly, such apps
                              are out there.

                              Comment

                              Working...