用 Ruby 编写二进制文件 [英] Write binary file in Ruby

查看:52
本文介绍了用 Ruby 编写二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有像我们以前在 C/C++ 中那样将二进制数据写入二进制文件的简单方法?例如,如何在不使用花哨的数学运算的情况下创建一个具有序列化 4 字节整数值的 4 字节文件?

解决方案

您可以使用 Array#packString#unpack 来转换二进制表示.将它们与 IO#writeIO#read,然后离开.>

Is there a simple way to write binary data into binary file like we used to do in C/C++? For example, how can I create a 4-byte file with serialized 4-byte integer value without using fancy math?

解决方案

You can use Array#pack and String#unpack to convert to and from binary representations. Combine them with IO#write and IO#read, and away you go.

这篇关于用 Ruby 编写二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆