crm114 reading/writing binary data (integers) --> sample script

"Ger Hobbelt" <[email protected]>
Newsgroups gmane.mail.spam.crm114
Message-ID <[email protected]>
This is a sample script showing a way to convert binary input to
hexadecimal printed (i.e. human readable) numbers.

Default is big-endian 32-bit.
You can use the '--little-endian' commandline option to make the
script cough up little-endian 32-bit numbers.

I did a (partial) write-up on this earlier this evening, saved it in
drafts and now gmail doesn't want to show it because (my guess)
there's a '-->' in the subject line. How easy to screw it up it is.
:-(
Now I lost my writeup draft. !@#$%^&*

Anyway, sorta rerun of the explanation:

run script
type this (^Z for Windows, ^D is end-of-file for UNIX)
0123
ABCD
5555


^Z
then see the 32-bit hex numbers dumped (this time, off the top of my
head, so typos included):
30313233
0d0a4142
43440d0a
0d0a0d0a

that's for windows (of course :-) ) and this should be about it for UNIX:
30313233
0d414243
440d0d0d


Note that the script discards the last few bytes of input if those are
less then 4 bytes total.


The process can be watched in the crm114 debugger - <plug>if you use
the latest gerH build (> 2100) the debugger commandlines below work
their magic; more flexible than the original ;-) </plug> - by running
'crm114 -d' etc.
Then type the data as shown above to feed input to the crm114 script,
after which it will jump into the debugger. Here type:
  t
to turn off auto-enabled user tracing
  m ie
to single-step from executable to executable statement (= skip
comments and the like)
  + *
to add all currently know non-system variables to the 'watch' list,
which is auto-printed after every single-step or other execute
debugger command
  n ; v > 5
to single-step one statement and have the current source line plus the
5 after that printed on screen
  .
to repeat the above ('.' repeats the last debugger commandline;
multiple commands can be merged by separating with ';' as shown above)
 .
some more.
  + *
again is a quick way to add any newly created crm114 script vars to
the watch list while you're running the script
  co
to run script until completion.



The concept is to take those bytes of input 'one integer' (4 bytes) at
a time, duplicate the bytes 3 times, so we can convert each copy to
ONE octet digit, thus creating a set of three octet digits per byte,
which can then be mixed together using 'eval' into the corresponding
'character/byte value'. Do this for all 4 bytes at the same time on
you've got a 32-bit number to play with.

When you're only interested in hex-dumping the bytes, you can be
faster using the first -DISABLED- section of the script, as then you
can do with splitting the bytes into nibbles and then convert each to
a hex character representing that nibble, followed by a simple
'output' line which just concatenates those hex characters. Same idea,
no 'eval' required.



This little exercise turned up a few minor bugs in my builds and one
big one (thanks to the crm114 debugger 'watch expression' feature in
GerH):

eval /:@: :@: 1 + 1 ::/

does NOT do what you'd expect, despite it being a valid statement
according to the docs ;-) -- it will NOT produce the number '2'. Fix
for that is planned, but is not a '5 minute' thing if you want to do
this properly so will be taken care of when I'm back from abroad
mid-may.





-- 
Met vriendelijke groeten / Best regards,

Ger Hobbelt

--------------------------------------------------
web: http://www.hobbelt.com/
 http://www.hebbut.net/
mail: [email protected]
mobile: +31-6-11 120 978
--------------------------------------------------

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone

_______________________________________________
Crm114-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/crm114-general
print_binary2decimal_int32.crm (application/octet-stream, 7.8 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.