   Editing hex files in Perl
  In my perl script, I have an integer; say 48.  And I'd like to write this number to a file in such a way that when I open the file in a hex editor, I literally see 48.  In other words, I'd like to go from 48 to \x48.So far, I've tried the following:Writing 48 itself to the file doesn't work because it writes the hex values of '4' and '8' rather than the byte 0x48.I've tried concatenating "\x" and "48", but perl treats the x as a separate hex character.I've tried concatenating "\\x" and "48", but this ends up writing "\x48" to the file in ASCII.So, I turn to gamedev for help.  The solution is probably really simple, but I've been working on this for a day or so now, and I'm at a loss for what to do.   
  48 in hex is 72 in dec, totally different values..(4 * 16) + 8 = 72   ;  The literal 48 is in decimal representation, in your hex editor it will appear in hex -- e.g., as 0x30. What you're looking for is pack, I think:$value = 48;print SOMEFILE pack 'C',$value;This will write 48, or 0x30 (or the ASCII '0') to SOMEFILE.   ;Quote:Original post by Captain_ThunderIn my perl script, I have an integer; say 48.  And I'd like to write this number to a file in such a way that when I open the file in a hex editor, I literally see 48.  In other words, I'd like to go from 48 to \x48.OK, and what integer are you doing to use when you decide you want to see FF in the hex editor?   ;Quote:Original post by jpetrieThe literal 48 is in decimal representation, in your hex editor it will appear in hex -- e.g., as 0x30. What you're looking for is pack, I think:$value = 48;print SOMEFILE pack 'C',$value;This will write 48, or 0x30 (or the ASCII '0') to SOMEFILE.I'm not looking to write the hex equivalent of 48 decimal to the file; I have 48 decimal, and I want to write the hex value 48 to the file.  I need to see 48 when I open the file in a hex editor.Quote:Original post by ZahlmanOK, and what integer are you doing to use when you decide you want to see FF in the hex editor?I don't know.  The hex digits are generated at runtime; in that case, I would need to go from FF to \xFF (I think).I'm sorry if I'm not making this problem clear.  The basic point is that I have a number and I need to write it to a file in such a way that I see that number when I open the file in a hex editor (I'm taking 48 and writing the byte 0x48).   ;  I think you might be a little confused about how files are stored. There's no such thing as "hex values" in a file. All files are collections of bytes. A text file happens to mostly store byte values between 32 and 127, which can be printed as human-readable characters on the screen. A file which is not a text file will store all sorts of byte values, between 0 and 255. And none of this has anything to do with hex.To write the byte value 48 to a file, you can use pack, as described earlier. In a hex editor that'll show up as 30, because 30 is the hexadecimal notation for the number 48; and you can pass 0x30 to pack for equivalent results. If you write the byte value 72 to a file, in a hex editor that'll show up as 48 since 48 is the hexadecimal notation for the number 72; and you can pass 0x48 to pack for equivalent results.And if for some bizarre reason you're trying to find the number which in hexadecimal happens to "look" the same as a particular decimal number, extract the digits one by one and then construct the equivalent number in base 16. Unless you have a very, very strange problem, though, this is a wrong answer.   ;  I'm still not describing the problem correctly; I do not want to see 0x30 when I open the file in a hex editor, even though that is the proper hex value of decimal 48.  I really do want to see 0x48 when I open the file.  I need to convert from 48 to \x48 (\x is the perl way of writing hexadecimal).  How would I do this?EDIT: I'll try what you said in the last paragraph, Sneftel.  My overall goal is scanning through a list of characters and writing their hex values to a file, as hex (look at Ascii table for the different hex values).   ;Quote:EDIT: I'll try what you said in the last paragraph, Sneftel. My overall goal is scanning through a list of characters and writing their hex values to a file, as hex (look at Ascii table for the different hex values).ASCII table's printout is done by printing the same value in many different representations. What you're trying to do is print the same representation in many different values (as noted by Sneftel).You can just use $result = sprintf("%c %d %x",$value,$value,$value) if you want to print something like ASCII table. If you really want "48" to produce 0x48, then the conversion mentioned above will work. It's just not analogous to what ASCII table printouts are doing.   ;  As it turns out, I was making the problem too complicated; all I really had to do was write the character to the file.But thanks for helping, guys.   