本帖最后由 pcl_test 于 2016-11-12 15:33 编辑
求助解析批处理直接分割字符串和把字符串导入文本再分割的差异
处理以下文本:
&请求#8022 07 AE 30 06 00 00 00 00
#8042 07 AE 30 06 00 00 00 00
#8042 07 AE 30 06 00 00 00 00
&应答#8122 aa AE 30 04 00 00 00 00
#8122 55 55 30 04 00 00 00 00 33
#8122 55 55 30 04 00 00 00 00 33
&结束#8122 aa AE 30 00 00 00 00 00
&逻辑错误#8122 aa AE 34 0C 00 00 00 00
&请求#8122 aa AE 34 08 00 00 00 00
&链接#8122 aa AE 00 00 00 00 00 00 00 00
达到如下效果:
请求:lenth=10,0x80,0x22,0x07,0xAE,0x30,0x06,0x00,0x00,0x00,0x00
请求:lenth=10,0x80,0x42,0x07,0xAE,0x30,0x06,0x00,0x00,0x00,0x00
请求:lenth=10,0x80,0x42,0x07,0xAE,0x30,0x06,0x00,0x00,0x00,0x00
应答:lenth=10,0x81,0x22,0xaa,0xAE,0x30,0x04,0x00,0x00,0x00,0x00
应答:lenth=11,0x81,0x22,0x55,0x55,0x30,0x04,0x00,0x00,0x00,0x00,0x33
应答:lenth=11,0x81,0x22,0x55,0x55,0x30,0x04,0x00,0x00,0x00,0x00,0x33
结束:lenth=10,0x81,0x22,0xaa,0xAE,0x30,0x00,0x00,0x00,0x00,0x00
逻辑错误:lenth=10,0x81,0x22,0xaa,0xAE,0x34,0x0C,0x00,0x00,0x00,0x00
请求:lenth=10,0x81,0x22,0xaa,0xAE,0x34,0x08,0x00,0x00,0x00,0x00
链接:lenth=12,0x81,0x22,0xaa,0xAE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
我用以下代码做处理,但是效率很慢:- @Echo Off&Setlocal Enabledelayedexpansion
-
- del b.txt
-
- for /f "tokens=*" %%i in (a.txt) do (
- echo %%i>line_content.txt
-
- for /f "tokens=1 delims=&,#:" %%i in ('findstr "&" line_content.txt') do (
- set string_content=%%i
- )
-
- for /f "tokens=1,2 delims=#" %%i in ('findstr "#" line_content.txt') do (
-
- set cut_bnt_id=%%i
- set cut_bnt=!cut_bnt_id:~0,1!
- if "!cut_bnt!"=="&" (
- set hex_content=%%j
- ) else (set hex_content=%%i)
-
- for %%i in (!hex_content!) do (
- set /a n+=1
- if "!n!"=="1" (
- set cut_hex_id=%%i
- set cut_hex_id1=0x!cut_hex_id:~0,2!
- set cut_hex_id2=,0x!cut_hex_id:~2,2!
- set line_hex=!cut_hex_id1!!cut_hex_id2!
- set /a n+=1
- ) else (set line_hex=!line_hex!,0x%%i)
- )
- echo;!string_content!:lenth=!n!,!line_hex!>>b.txt
- set n=&set line_hex=
- )
-
- )
-
- pause
复制代码 转为以下代码:- @Echo Off&Setlocal Enabledelayedexpansion
-
- del b.txt
-
- for /f "tokens=*" %%i in (a.txt) do (
-
- set line_content=%%i
-
- for /f "tokens=1 delims=&,#:" %%i in ("!line_content!") do (
- set string_content=%%i
- )
-
- for /f "tokens=1,2 delims=#" %%i in ("!line_content!") do (
-
- set cut_bnt_id=%%i
- set cut_bnt=!cut_bnt_id:~0,1!
- if "!cut_bnt!"=="&" (
- set hex_content=%%j
- ) else (set hex_content=%%i)
-
- for %%i in (!hex_content!) do (
- set /a n+=1
- if "!n!"=="1" (
- set cut_hex_id=%%i
- set cut_hex_id1=0x!cut_hex_id:~0,2!
- set cut_hex_id2=,0x!cut_hex_id:~2,2!
- set line_hex=!cut_hex_id1!!cut_hex_id2!
- set /a n+=1
- ) else (set line_hex=!line_hex!,0x%%i)
- )
- echo;!string_content!:lenth=!n!,!line_hex!>>b.txt
- set n=&set line_hex=
- )
-
- )
-
- pause
复制代码 效率是提高了,但是得出的结果完全不对,有哪位大神指点指点遍历字符串和把字符串放到文本中再遍历的区别 |