Edward Rawde wrote: > > This code outputs bytes in hex and binary from a 16K file: > > Files.setOffset 0x0000 > address = 0x0000 > while address < 0x4000 > byte = Files.readCodeByte(address) > puts address.to_s(16) + " " + byte.to_s(2).upcase + " " + byte.to_s(16) > address += 1 > end > > But I want fixed width. So if byte 0 is at address 0 > I want 0000 00000000 00 > > I can probably figure some code to add the leading zeros but > what's the most efficient way to do that in Ruby? > Using String % (sprintf) http://www.rubycentral.com/book/ref_c_string.html#String._pc I've added the ascii character (or '.' if it's not printable) address = 0x3ff8 while address < 0x4000 byte = Files.readCodeByte(address) puts '%04X %08b %02X %c' % [address, byte, byte, ((32..126) === byte) ? byte : ?.] address += 1 end #-> 3FF8 01011011 5B [ #-> 3FF9 00111101 3D = #-> 3FFA 01000000 40 @ #-> 3FFB 00001000 08 . #-> 3FFC 01001011 4B K #-> 3FFD 01010100 54 T #-> 3FFE 10111000 B8 . #-> 3FFF 00010010 12 . daz