Rogue Hex String Bytes

I wanted to embed the raw bytes of a certain file - the PNG image of the Plasmacore system font - in a Rogue source file.

It's always been a bit cumbersome to generate literal byte arrays in C++, so I came up with something better for Rogue API: the ability to convert Byte[] arrays to and from hex strings. For example:

local bytes = Byte[]( "0080ff" )
trace bytes                # [0,128,255]
trace bytes.to_hex_string  # 0080FF

Next I wrote a little utility to convert a file into a hex byte string:

# HexString.rogue
require (System.command_line_arguments.count == 1)
local bytes = File.load_as_bytes( System.command_line_arguments.first )
File.save( "Output.txt", bytes.to_hex_string )

That worked fine but it generated one long line of text that I would have to manually break into lines. So I used String.word_wrapped() to add the line breaks and added indentation, quotes, commas, and line continuations while I was at it:

require (System.command_line_arguments.count == 1)
local bytes = File.load_as_bytes( System.command_line_arguments.first )

local writer = PrintWriter( File.writer("Output.txt") )
local first = true
forEach (line in bytes.to_hex_string.word_wrapped(60))
  if (first) first = false
  else       writer.println " ..."
  writer.print( '  "$"' (line) )
endForEach
writer.println.close

I compiled and ran the program with:

roguec HexString.rogue --execute=FontSystem17.png

It gave me the bulk of the following output that I then wrapped in a variable declaration:

local png_bytes = Byte[](
  "89504E470D0A1A0A0000000D49484452000003570000001102030000004C" ...
  "45BE8F00000009504C5445FFFFFF000000FFFFFF7EEF8F4F000000017452" ...
  "4E530040E6D866000004AB49444154785EDD96D1CAEB2810C7F542DF2081" ...
  "F569EC457B6D215358AF7717264F930FD25EF45A0BF129F73FDAA45F430F" ...
  "CB8185B3ACA5C4E838E637338EA3B6668219D4AF6D847F17FE5D9D76EAF3" ...
  "AFA5D2EC9572D387894175FEA7B50D86020DFDEC5813D6E33DECE71569F2" ...
  "143ADFE171A0A1C3D8666188638A202732D23DCA830832E64C03941EE93C" ...
  "6004124D863AAA4344ABB0C254C1BEFD2C0A57DF5190A74ECA1D0679D9B5" ...
  "3DAC09F2D3C320FD6C6F13A7981D1F17EFBC49767EC34AFD726053CEE3EC" ...
  "BC2B7E3C1408F78B6F16869F673DCE7139662B32902AA7F235CECC7651F6" ...
  "2B2756EE74FBCA98C2AA5395595CC450626674CA344EACEC82CF005282C2" ...
  "EA3186F6113D6E5849E165D7DC0EAB9FEC646793FF405FF47D7166EE1FA7" ...
  "3BB0309E3485CD1226B9482293932347073E5092CFF1E4BB60B2D845E7C4" ...
  "F1B29C4486B3A3D3F58C0E9BBC625D6ED79CEBAA2A73B97EC33A5E260EEC" ...
  "0D1B0A2D36EC552203EF3AC3C38CD8D074C8C36512D7FAE6F5A33CE2CEEB" ...
  "2ED8095E2969C52A0B938915AB9F74D68F69B3849DDD189F580C67668057" ...
  "ACD1BBA99F3B0115AC181B1664EC24A0AC13B01E33B0EEB7FC0D6BB053EB" ...
  "70FCD3456CC82A7AC5EB7ECDA3714190243DA631BBA40BB6C0AA72C19478" ...
  "BD6039A696A667F57ADD39D9CB5CB10C9D39C7A025083BEFD03123450468" ...
  "475E3CEB466273F922C1225B423F8D2B560CAEF878BECC3C36AC8145E60A" ...
  "E3AE5813B0FEBA2D2F2CF64FAC07F733B0628044D8B0CC669E78CCD5EB5D" ...
  "D2B16241CF3A75BA63AF484DCFEAF5580D6A8E41D464B071EA1193F63AD4" ...
  "80655BB278D21560B90004560098233B44DAE442CCCF20640F7480077E00" ...
  "AB9C9362C8D802AC71A9582554AC7BCE6277C88418BE61C9B9F50CDBEDB0" ...
  "86E6800A51CFD6637EC7027170FE8955BF279E59B0D83F5302FE9C4DEEAF" ...
  "A77BAA586FDE8ADEDD3DEB76A428020B944B5FEAEAE62DE80756F9EEAD47" ...
  "FD1C7359BD7503160F62E5D0CF1BD6EFD9E007223B6F5835C0AA790ABCB5" ...
  "6195F08E25790A582FAFCB59906C139F5803FE652176E3E53637AC7AB6EA" ...
  "F54C9E2B56CD0B63F182C59ED970F5563B5B085D1E3F9D2D8B3DE757CA50" ...
  "9586D2138BB3C8542293362CF70A669CAD1F7A6B5294DEBC75CCFA83B798" ...
  "7F7BDC2A56D05C33A1C5841A0FF2319EAD24376262C15235CBC9D96A9910" ...
  "A6608AF195091B96C9B68E90C3CC0B2B57AC6309DCD0AF8195CE3BAC2458" ...
  "BA6175D9BB83E4CF372C0D2C123D67CE75E41ADAD9521B96927BAB7B5C17" ...
  "F4FB75E6899555BF004BAE22C52E1B392C31DB54B1DABD05AC5880B5DD5B" ...
  "931E81651753608C7179DE5B0C4DE88DB07B3B520DAB4CC0E26F41B86542" ...
  "9DAA1E578055F55C80D53221B00A0E665F9E7AFA65CB846A0B422555861E" ...
  "CF84BE9D4D9BA90F048232A4C850A0A048AE104FAA1B10A1922B9E558694" ...
  "1F44C7ADCA80E44058A3E9209DB5CA50866B4D6182A8AC32B5C601D10BCB" ...
  "343D9D2CC72609CBC9BB558F2CE7C1C463D0145AD1D2F662B3DE5BBB9A50" ...
  "9197CE5665E0D1A87FD8DC4FD7A5D17F1AE5A0C8004B13ED6634A58AFAB6" ...
  "0E926EAFA7CB5B95B1ABA75E35605847FEA17D2E407F7E85944576A9996B" ...
  "8F55E67645FF13969B2B839DC0F15F691424F63E7B2BB4C07C13FF609E6E" ...
  "73C5FFB8FD0DE662E021BABB6DAE0000000049454E44AE4260820A"
)

If we wanted to see what the PNG file looked like, we could make this simple test program:

# Test.rogue
local png_bytes = Byte[]( ... )
File.save( "Font.png", png_bytes )

Compile and run it with Rogue:

roguec Test.rogue --execute

And then inspect the output Font.png file!