Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What is a faster way to Flip Bytes

What is a faster way to Flip Bytes

Scheduled Pinned Locked Moved C#
tutorialquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yitzchok Dev
    wrote on last edited by
    #1

    What is a faster way to do this (changing example 255 = 0, 254 = 1, 253 = 2, etc...) byte[] buffer = new byte[size]; br.Read(buffer, 0, 5000000); //******************************************************* **for (int i = 0; i < buffer.Length; i++) { buffer[i] = byte.Parse((255 - buffer[i]).ToString()); }** //******************************************************* bw.Write(buffer); Thank you all

    C G 2 Replies Last reply
    0
    • Y Yitzchok Dev

      What is a faster way to do this (changing example 255 = 0, 254 = 1, 253 = 2, etc...) byte[] buffer = new byte[size]; br.Read(buffer, 0, 5000000); //******************************************************* **for (int i = 0; i < buffer.Length; i++) { buffer[i] = byte.Parse((255 - buffer[i]).ToString()); }** //******************************************************* bw.Write(buffer); Thank you all

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Why do you need the ToString and the byte.Parse, are they not both doing stuff that is completely superfluous ? 255 ^ buffer[i] would do it, I'd have thought. ^ == XOR

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      Y G 2 Replies Last reply
      0
      • C Christian Graus

        Why do you need the ToString and the byte.Parse, are they not both doing stuff that is completely superfluous ? 255 ^ buffer[i] would do it, I'd have thought. ^ == XOR

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        Y Offline
        Y Offline
        Yitzchok Dev
        wrote on last edited by
        #3

        Thanks I will try out later and see what happens

        1 Reply Last reply
        0
        • C Christian Graus

          Why do you need the ToString and the byte.Parse, are they not both doing stuff that is completely superfluous ? 255 ^ buffer[i] would do it, I'd have thought. ^ == XOR

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Christian Graus wrote:

          255 ^ buffer[i] would do it, I'd have thought. ^ == XOR

          Or using the ^= operator: buffer[i] ^= 255; Subtracting the value from 255 gives the same result, only the calculation is done using integers, so you would have to cast the result to byte (I guess that's the intention of the ToString and Parse in the original code): buffer[i] = (byte)(255 - buffer[i]);

          --- single minded; short sighted; long gone;

          Y 1 Reply Last reply
          0
          • G Guffa

            Christian Graus wrote:

            255 ^ buffer[i] would do it, I'd have thought. ^ == XOR

            Or using the ^= operator: buffer[i] ^= 255; Subtracting the value from 255 gives the same result, only the calculation is done using integers, so you would have to cast the result to byte (I guess that's the intention of the ToString and Parse in the original code): buffer[i] = (byte)(255 - buffer[i]);

            --- single minded; short sighted; long gone;

            Y Offline
            Y Offline
            Yitzchok Dev
            wrote on last edited by
            #5

            well this is what I tried and it worked buffer[i] = (byte)(255 ^ buffer[i]); and for some reseon I still have to cast it and the results are incredibly fast From about 7 Minutes to a half a minute, on about a 600MB File Thanks all for your help (I just started playing around with the Streams)

            Y 1 Reply Last reply
            0
            • Y Yitzchok Dev

              well this is what I tried and it worked buffer[i] = (byte)(255 ^ buffer[i]); and for some reseon I still have to cast it and the results are incredibly fast From about 7 Minutes to a half a minute, on about a 600MB File Thanks all for your help (I just started playing around with the Streams)

              Y Offline
              Y Offline
              Yitzchok Dev
              wrote on last edited by
              #6

              This is my results in seconds

              ^= 31, 28, 27, 26, 29, 27, 25
              cast - 28, 25, 25, 25, 26, 25, 26
              cast ^ 30, 26, 26, 25, 26, 26, 26

              1 Reply Last reply
              0
              • Y Yitzchok Dev

                What is a faster way to do this (changing example 255 = 0, 254 = 1, 253 = 2, etc...) byte[] buffer = new byte[size]; br.Read(buffer, 0, 5000000); //******************************************************* **for (int i = 0; i < buffer.Length; i++) { buffer[i] = byte.Parse((255 - buffer[i]).ToString()); }** //******************************************************* bw.Write(buffer); Thank you all

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                F16I wrote:

                br.Read(buffer, 0, 5000000);

                Warning! The Read method returns how many bytes that was actually read, and that doesn't have to be all the bytes that are possible to read. You have to get the return value of the call so that you know if you need more calls to read the rest of the file. Just loop until the method returns zero, that means that there are no more bytes to read. If you use framework 2, you can use the File.ReadAllBytes method to read the entire file.

                --- single minded; short sighted; long gone;

                Y 1 Reply Last reply
                0
                • G Guffa

                  F16I wrote:

                  br.Read(buffer, 0, 5000000);

                  Warning! The Read method returns how many bytes that was actually read, and that doesn't have to be all the bytes that are possible to read. You have to get the return value of the call so that you know if you need more calls to read the rest of the file. Just loop until the method returns zero, that means that there are no more bytes to read. If you use framework 2, you can use the File.ReadAllBytes method to read the entire file.

                  --- single minded; short sighted; long gone;

                  Y Offline
                  Y Offline
                  Yitzchok Dev
                  wrote on last edited by
                  #8

                  I just put thet in as an example, just wanted to show a long array

                  G 1 Reply Last reply
                  0
                  • Y Yitzchok Dev

                    I just put thet in as an example, just wanted to show a long array

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    I think that you missed the point. If you call the Read method without checking what it returns, it may read only part of the file without you realising it, and you will merrily continue, believing that the entire file is in the array while in fact part of the data is just garbage.

                    --- single minded; short sighted; long gone;

                    Y 1 Reply Last reply
                    0
                    • G Guffa

                      I think that you missed the point. If you call the Read method without checking what it returns, it may read only part of the file without you realising it, and you will merrily continue, believing that the entire file is in the array while in fact part of the data is just garbage.

                      --- single minded; short sighted; long gone;

                      Y Offline
                      Y Offline
                      Yitzchok Dev
                      wrote on last edited by
                      #10

                      I know, (it is inside a for loop) and by the way I am not trying to XOR the whole file, but anyway "thanks vary much for your help"

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups