panyes
panyes

Reputation: 13

Convert byte array of signed ints to file - Ruby

I have message from server in following format

result = "123,-23,12,...,54,-53"

That message represents array byte of image. How can I convert this to actual image?

I converted this result to array of ints and tried with:

File.open( 'imageX.png', 'wb' ) do |output|
  splited.each do | byte |
      output.print byte
  end
end

But image is not recognizable. What am I missing?

Upvotes: 1

Views: 883

Answers (1)

Tim Lowrimore
Tim Lowrimore

Reputation: 2052

I suspect you'll want to try something like:

File.write("imageX.png", result.split(',').map(&:to_i).pack('C*'))

This converts it to bytes then "packs" each one as a character (one byte).

I hope that helps.

Cheers!

Upvotes: 4

Related Questions