4

Is there a program or CMD command with which I can simply reverse or flip all the bytes of a file? For example if I have a text file (as a simple example) that says "Hello, world!", the program/command would flip it to say "!dlrow ,olleH".

So yeah, is there any way to do this? I'm a programmer and know that it would be trivial to write my own program for this, but I'd rather not go through the trouble if there's already something that can do it. A batch script would also be OK.

puggsoy
  • 407
  • 1
  • 7
  • 16
  • 2
    I'd say, go through the trouble. Apparently it is trivial and you can then share your few moments work. Regards, – Xavierjazz Nov 04 '14 at 23:33
  • Of course, if there isn't already anything for this I don't mind writing a program myself (and I'm sure others might also find it useful). However if there's already something for this then there's no point reinventing the wheel. – puggsoy Nov 04 '14 at 23:52
  • xxd goes part of the way there. xxd -p yourfile dumps the hex You can get xxd with vim7.x But then what to type that to reverse it as you want, i'm not sure. I suppose a perl one-liner though I don't really know perl as yet. – barlop Nov 05 '14 at 00:05

2 Answers2

7
powershell $s='Hello, world!';$s[-1..-($s.length)]-join''

file:

way 1:

powershell $f=[IO.File]::ReadAllBytes('.\file.txt');$t=[Text.Encoding]::ASCII.GetString($f);$t[-1..-($t.length)]-join''

way 2:

powershell [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic');$s=gc .\file.txt;[Microsoft.VisualBasic.Strings]::StrReverse($s)

byte reverse:

slow:

powershell [byte[]]$b=gc '.\file.bin' -En byte;[array]::Reverse($b);[IO.File]::WriteAllBytes('.\Reverse.bin',$b)

fast:

powershell [byte[]]$b=[IO.File]::ReadAllBytes('.\file.bin');[array]::Reverse($b);[IO.File]::WriteAllBytes('.\Reverse.bin',$b)
STTR
  • 6,767
  • 2
  • 18
  • 20
  • How would I specify a file? I don't want to have to write the characters in myself, and in most cases I can't (since it's the actually bytes I want to reverse, not just text). – puggsoy Nov 04 '14 at 23:49
  • Ah that works great, thanks! I think I might make a program for this myself though, since this seems more complex than necessary. All the same, thank you for your answer, it works and has answered my question! – puggsoy Nov 05 '14 at 04:10
  • Actually wait no, it isn't working properly. It works with strings, but for my use I want it to reverse all the *bytes* in a file, not just text characters. – puggsoy Nov 05 '14 at 04:28
  • @puggsoy Update 2 – STTR Nov 05 '14 at 07:49
  • 1
    Great, that works. I've made my own program anyway but it answers my question and is helpful :) – puggsoy Nov 06 '14 at 01:58
0

"CMD command with which I can simply reverse or flip all the bytes of a file":

REM set content to file
set /p="Hello, world!"> test
certutil -encodehex -f test temp 4

REM reverse file contents
set rev=
(for /f "delims=" %i in (temp) do for %j in (%i) do set rev=%j !rev!)
set /p="%rev:~0,-6%">temp
certutil -decodehex temp out.txt

Tested on Win 10 CMD

Zimba
  • 1,051
  • 11
  • 15