What is a faster way to Flip Bytes
-
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 -
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 allWhy 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 )
-
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 )
Thanks I will try out later and see what happens
-
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 )
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;
-
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;
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)
-
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)
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 -
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 allF16I 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 theFile.ReadAllBytes
method to read the entire file.--- single minded; short sighted; long gone;
-
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 theFile.ReadAllBytes
method to read the entire file.--- single minded; short sighted; long gone;
I just put thet in as an example, just wanted to show a long array
-
I just put thet in as an example, just wanted to show a long array
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;
-
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;
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"