You would want pack to turn numbers into binary data. There is an old but extremely good node on this site that explains how to use pack.
Having said that you will probably find that you need to do more to turn the raw numbers from the JSON Byte Array back into an image than just pack them. For example what format is the image? Are you being sent the bytes for an image in a known file format such a PNG or JPEG, or are you getting pixel values? How do you know what size the image is, and what format?
If the JSON data you are receving is the byte values for a file, then you may get away with something simple like:
$imgData = pack( "b", @byte_array);
open my $outfile, '>', $fileName or die "Error writing $fileName $!";
binmode $outfile;
print $outfile $imgData;
close $outfile;
(In this example the b pack format string means a list of bytes.)
If the JSON data you are receving is pixel values, then I would forget about using pack, and instead use the pixel data to directly create the image file. You could either use GD, and just set each pixel individualy from the data you have, and then save the image in your prefered file format, or you could generate an image file in Netpbm format which stores the pixel values as plain text in file. If necessary you can then convert that file to a more optimised format with an external libary. |