在大文件上将十六进制转换为二进制(Convert Hex to Binary on large file)

编程入门 行业动态 更新时间:2024-10-27 06:22:47
在大文件上将十六进制转换为二进制(Convert Hex to Binary on large file)

我有一个约230万行的文本文件。 每行包含一个64个字符的十六进制字符串。 我试图逐行读取文件并将十六进制字符串转换为二进制并输出到文件。 我在下面用bash编写了这个简单的循环,但我知道它不是最优的,它将需要永远完成。

有没有更快的方法,例如使用awk? 最好使用perl? 我只需要更快的东西。

cat /tmp/hexFile.log | while read line do bin=$(echo "obase=2; ibase=16; $line" | bc ) bin=`echo $bin | sed 's/\\\ //g'` echo $bin >> /tmp/binOutput.log done

I have a text file with ~2.3 million lines. Each line contains a 64 character hexadecimal string. I am trying to read in the file line by line and convert the hex string to binary and output to a file. I wrote this simple loop below in bash, but I know it is not optimal and it will take forever to complete.

Is there a faster way, using awk for instance? Better to use perl? I just need something that is much faster.

cat /tmp/hexFile.log | while read line do bin=$(echo "obase=2; ibase=16; $line" | bc ) bin=`echo $bin | sed 's/\\\ //g'` echo $bin >> /tmp/binOutput.log done

最满意答案

这对我有用。

#!/usr/bin/perl while (<>) { chomp; for (my $i = 0; $i < length($_); $i += 1) { printf('%04b', hex(substr($_, $i, 1))) } print "\n"; }

This works for me.

#!/usr/bin/perl while (<>) { chomp; for (my $i = 0; $i < length($_); $i += 1) { printf('%04b', hex(substr($_, $i, 1))) } print "\n"; }

更多推荐

本文发布于:2023-07-16 00:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1120971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上将   转换为   大文件   十六进制   Convert

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!