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

[REBOL] noob question -- insert 2 byte hexidecimal value into binary string

From: dougedmunds:gm:ail at: 14-Apr-2008 14:07

I am building a binary string, composed of 1) some hex code 2) the length of a string (as 2 bytes in hex16) 3) the string Without the length of the string in the middle, it looks like this:
>> join #{836B} "Fred Flintstone and Wilma Flintstone"
== #{ 836B4672656420466C696E7473746F6E6520616E642057696C6D6120466C696E 7473746F6E65 } I need to insert the length of the string as a two byte heximdecimal value after the initial #{836B} and before the string-as-hex part.
>> length? "Fred Flintstone and Wilma Flintstone"
== 36 What I need is a 2 byte representation of the length (#0024), inserted so the finished binary is: #{ 836B00244672656420466C696E7473746F6E6520616E642057696C6D6120466C696E 7473746F6E65} I can get a 2 byte representation in hex of the length:
>> to-hex length? "Fred Flintstone and Wilma Flintstone"
== #00000024
>> remove/part to-hex length? "Fred Flintstone and Wilma Flintstone" 4
== #0024 But if I try to join it with the first part, it gets converted to ascii first.
>> x: remove/part to-hex length? "Fred Flintstone and Wilma Flintstone" 4
== #0024
>> y: #{836B}
== #{836B}
>> join y x
== #{836B30303234} ; no good If someone can show me how to do this, much appreciated. == Doug Edmunds