首页 > 解决方案 > 在批处理脚本中将 Big Endian 的字符串转换为 Little Endian

问题描述

有没有办法使用批处理脚本命令将 Big Endian 中的给定字符串转换为 Little Endian?例如,给定字符串:9295147A58EACCAA 将其转换为 Int32 little Endian 为:AACCEA587A149592

标签: batch-file

解决方案


这是更改字符串字符顺序的代码。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "BytesBE=%~1"
if not defined BytesBE goto :EOF

set "BytesCO=%BytesBE%"
set "BytesLE="

:ChangeOrder
set "BytesLE=%BytesLE%%BytesCO:~-2%"
set "BytesCO=%BytesCO:~0,-2%"
if defined BytesCO goto ChangeOrder

echo    Big Endian: %BytesBE%
echo Little Endian: %BytesLE%
endlocal

批处理文件可以使用类似9295147A58EACCAA命令提示符窗口中的字符串运行,以测试简单循环并查看存储在BytesLE输入中的生成输出(存储在BytesBE. 输入字符串长度应该是正确的结果。

要了解使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • set /?
  • setlocal /?

推荐阅读