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

[REBOL] Re: HEX to-binary!

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 - > > 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?
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