V
V
Vayladion Gognazdiak2021-08-02 14:14:52
ruby
Vayladion Gognazdiak, 2021-08-02 14:14:52

Is there an analogue of Python's struct.pack in ruby?

Good day.

There is a python code:

import struct
struct.pack('!3i32xhbbi48x', 442,1627896903,1,8054,0,0,0)

Everything works fine. We have:

b'\x00\x00\x01\xbaa\x07\xbcG\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1fv\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'


Let's try the same in Ruby:
[442,1627896903,1,8054,0,0,0].pack('!3i32xhbbi48x')

The result is an error.


Traceback (most recent call last):
22: from /home/deployer/.rvm/gems/[email protected]_fancy_app/bin/ruby_executable_hooks:22:in `'
21: ​​from /home/deployer/.rvm/gems /[email protected]_fancy_app/bin/ruby_executable_hooks:22:in `eval'
20: from /home/deployer/.rvm/gems/[email protected]_fancy_app/bin/irb:23:in `'
19 : from /home/deployer/.rvm/gems/[email protected]_fancy_app/bin/irb:23:in `load'
18: from /home/deployer/.rvm/gems/[email protected]_fancy_app /gems/irb-1.3.6/exe/irb:11:in `'
1: from (irb):45:in `'
:134:in `pack': too few arguments (ArgumentError)


Dear, tell me where to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vayladion Gognazdiak, 2021-08-02
@etspring

[].pack('!NNNx32s>x48')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question