KEEP K.I.S.S.

tk's blog

Ruby 转换 Hex文本文件为二进制文件

tk posted @ Dec 12, 2011 10:53:56 AM in Ruby with tags Hex 文本操作 , 4325 阅读

用串口调试助手接受哦串口数据,貌似没找到怎么直接存储为二进制文件,所以用文本框显示十六进制字节流表示,然后存储(如果直接显示二进制会乱码,而且转换会丢失数据)。然后就写了个Ruby脚本来还原这个十六进制文本流。

 

filename = ARGV[0]
bytes = []
File.open(filename, "r") do |file|
  while str = file.gets
    bytes += str.split.map {|byte| byte.hex}
  end
end

puts bytes.count

newfilename = filename + ".bin"
File.open(newfilename, "wb") do |file|
  file.write bytes.pack("C*")
end
clenbuterol for sale 说:
Mar 10, 2023 12:20:34 AM

Thank a lot. You have done excellent job. I enjoyed your blog . Nice efforts comments clenbuterol for sale

Which Bank Swift Cod 说:
Jul 26, 2023 11:03:03 PM

Chase SWIFT code is Chasus33. It specifies your responder's bank, branch, and country. Your wire transfer may fail without that. Which Bank Swift Code Is Chasus33?

gacha life mobile 说:
Jul 28, 2023 06:16:40 AM

I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details. gacha life mobile

تحميل كتب مجانية 说:
Jul 28, 2023 06:42:41 AM

This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the good work. I have been meaning to write something like this on my website and you have given me an idea. تحميل كتب مجانية

tropicalviet 说:
Aug 01, 2023 11:02:41 PM

It is perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips. Perhaps you could write next articles referring to this article. I want to read more things about it! tropicalviet

singapore house clea 说:
Aug 01, 2023 11:34:07 PM

This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the good work. I have been meaning to write something like this on my website and you have given me an idea. singapore house cleaning

iptv 说:
Aug 25, 2023 09:02:07 AM

I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon… iptv

seo service london 说:
Jan 16, 2024 10:44:37 PM

i am for the first time here. I found this board and I in finding It truly helpful & it helped me out a lot. I hope to present something back and help others such as you helped me.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter