Language agnostic 反转文件中的每个字符

Language agnostic 反转文件中的每个字符,language-agnostic,character,endianness,Language Agnostic,Character,Endianness,我这里有点麻烦 有谁能帮我实现一个解决方案,将每个字节反转,使0xAB变为0xBA,但不使“abcd”变为“dcba”。我需要它使AB CD EF变成BA DC FE 最好是C或C++,但只要它能运行,就没关系了。 到目前为止,我已经在PureBasic中实现了一个UBER蹩脚的解决方案,它甚至不起作用(是的,我知道转换为字符串并返回到二进制是一个蹩脚的解决方案) OpenConsole() filename$ = OpenFileRequester("Open File","","All ty

我这里有点麻烦

有谁能帮我实现一个解决方案,将每个字节反转,使0xAB变为0xBA,但使“abcd”变为“dcba”。我需要它使AB CD EF变成BA DC FE

<>最好是C或C++,但只要它能运行,就没关系了。 到目前为止,我已经在PureBasic中实现了一个UBER蹩脚的解决方案,它甚至不起作用(是的,我知道转换为字符串并返回到二进制是一个蹩脚的解决方案)

OpenConsole()
filename$ = OpenFileRequester("Open File","","All types | *.*",0)
If filename$ = ""
End
EndIf
OpenFile(0,filename$)
*Byte = AllocateMemory(1)
ProcessedBytes = 0
Loc=Loc(0)
Repeat
FileSeek(0,Loc(0)+1)
PokeB(*Byte,ReadByte(0))
BitStr$ = RSet(Bin(Asc(PeekS(*Byte))),16,"0")
FirstStr$ = Left(BitStr$,8)
SecondStr$ = Right(BitStr$,8)
BitStr$ = SecondStr$ + FirstStr$
Bit.b = Val(BitStr$)
WriteByte(0,Bit)
ProcessedBytes = ProcessedBytes + 1
ClearConsole()
Print("Processed Bytes: ")
Print(Str(ProcessedBytes))
Loc=Loc(0)
Until Loc = Lof(0)
Delay(10000)

感谢阅读。

阅读您的PureBasic代码(我一开始跳过了它),您似乎确实想要交换endian,即使这不是您的文本所要求的-0xAB实际上总是意味着一个十进制值为171的字节,而不是两个字节,并且在示例中使用a-F时,将一个字节显示为两个十六进制数字是非常常见的

#include <iostream>
int main() {
  using namespace std;
  for (char a; cin.get(a);) {
    char b;
    if (!cin.get(b)) {
      cout.put(a); // better to write it than lose it
      cerr << "Damn it, input ends with an odd byte, is it in "
        "the right format?\n";
      return 1;
    }
    cout.put(b);
    cout.put(a);
  }
  return 0;
}
// C version is a similar easy translation from the original code

原始答复:

我不知道你为什么想要这个(例如,如果你想要的话,它不会转换endian),但是你可以这样做:

#include <stdio.h>
int main() {
  for (char c; (c == getchar()) != EOF;) {
    putchar((c & 0xF << 4) | ((int)c & 0xF0 >> 4));
  }
  return 0;
}
#包括
int main(){
for(char c;(c==getchar())!=EOF;){
putchar((c&0xF>4));
}
返回0;
}

#包括
int main(){
for(char c;std::cin.get(c);){
标准输出((c&0xF>4));
}
返回0;
}

导入系统 对于sys.stdin中的行: sys.stdout.write(“.”join( chr((命令(c)和0xF>4)) 对于c行 ))
所有这些都假定不会发生文本翻译(例如从
\n
\r\n
,反之亦然);如果是这种情况,您必须将它们更改为以二进制模式打开文件。如果您不熟悉stdin,它们从stdin读取,然后写入stdout,因此只需使用
programnameoutputfile
来运行它们。

通过一个简单的算术公式可以反转高半字节和低半字节(假设您对无符号字节进行操作):

Haskell解决方案:

module ReverseBytes where

import qualified Data.ByteString as B
import Data.Bits
import Data.Word

-----------------------------------------------------------

main :: IO ()
main = B.getContents >>= B.putStr . B.map reverseByte

reverseByte :: Word8 -> Word8
reverseByte = flip rotate 4

runghc reverseebytes.hsoutputfile

只要小心符号问题(这就是我回答中使用强制转换的原因),它适用于算术和位运算。(255->-1,-1/16是0,不是15)谁在12月31日得到家庭作业???我觉得我们应该继续增加这个答案,所以这里有一行:
perl-ne'print chr($\15>4)for map ord,split/,$\code>
#include <iostream>
int main() {
  for (char c; std::cin.get(c);) {
    std::cout.put((c & 0xF << 4) | ((int)c & 0xF0 >> 4));
  }
  return 0;
}
import sys
for line in sys.stdin:
  sys.stdout.write("".join(
    chr((ord(c) & 0xF << 4) | (ord(c) & 0xF0 >> 4))
    for c in line
  ))
reversed = (original % 16) * 16 + (original / 16);
module ReverseBytes where

import qualified Data.ByteString as B
import Data.Bits
import Data.Word

-----------------------------------------------------------

main :: IO ()
main = B.getContents >>= B.putStr . B.map reverseByte

reverseByte :: Word8 -> Word8
reverseByte = flip rotate 4
runghc ReverseBytes.hs < inputfile > outputfile