Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

HEX to-binary!

 [1/2] from: ptretter:norcom2000 at: 20-Feb-2001 20:54


I am trying to do some cool stuff with file data. However I am have troubles :( What I want to do take data that was extracted from binary for example - file1: read/binary %somefile.??? file2: reform file1 which is now in a string format so that I have the hex data. I want to manipulate the data and put it back into binary so that I see the same hex again when I read the file. How do I do that? It seems I keep trying things and not getting the original look of the original file1 with my changes. Any help? Paul Tretter

 [2/2] from: jeff:rebol at: 21-Feb-2001 11:49


Howdy, Paul:
> What I want to do take data that was extracted from binary > for example -
<<quoted lines omitted: 3>>
> binary so that I see the same hex again when I read the > file. How do I do that?
You have a variety of options available: ENBASE takes binary and puts it into a string representation in a particular base (2, 8, 16, 64). The default base can be set in system/options/binary-base, and the default is 16. For example, here we take a good peek at the REBOL source: print enbase/base read/binary %rebol 2 You can take the string representation of a binary value as a particular base, manipulate it and then turn it back into binary using DEBASE, for example, maybe there seems to be too many 1s in that REBOL code for your taste, so: system/options/binary-base: 2 write/binary %zilch debase replace/all enbase read/binary %rebol "1" "0" ( Above is just a goofy example (-: ) Other options for playing with binary data are: AND, OR, and XOR of binary. #{FF} or #{00FF} == #{FFFF} #{AFFA} xor #{FAAF} == #{5555} #{FF} and #{00FF} == #{00FF} A new binary is returned which is as long as the longest binary you supplied. You can do masking operations, what not. In the latest experimentals of CORE, these two important and useful operations on binary have been fixed: Conversion of tuples to binary: to-binary 255.255.255 == #{FFFFFF} And insertions of binary into other binary, which you can use with something like rejoin: rejoin [#{DE} #{AD} #{BE} #{EF}] == #{DEADBEEF} I may have missed some other tips with binary, but that's what I can think of right now. Cheers! -jeff

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted