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

noob question -- insert 2 byte hexidecimal value into binary string

 [1/6] from: dougedmunds::gmail 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

 [2/6] from: tomc:cs:uoregon at: 14-Apr-2008 14:15


hi Doug search for url-encode on rebol.org you will get some old examples that should still work here is one http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=cookies-client.r DougEdmunds wrote:
> I am building a binary string, composed of > 1) some hex code
<<quoted lines omitted: 31>>
> If someone can show me how to do this, much appreciated. > == Doug Edmunds
-- ... nice weather eh tomc-cs.uoregon.edu

 [3/6] from: tomc:cs:uoregon at: 14-Apr-2008 14:25


Sorry I need to actually read your question. Tom Conlin wrote:
> hi Doug > search for url-encode on rebol.org
<<quoted lines omitted: 56>>
>> >>
-- ... nice weather eh tomc-cs.uoregon.edu

 [4/6] from: anton:wilddsl:au at: 15-Apr-2008 15:38


Hi Doug, I think this is one of the shortest ways: load append insert form #0024 "#{" "}" == #{0024} Anton. DougEdmunds wrote:

 [5/6] from: carl:cybercraft at: 15-Apr-2008 20:00


Hi Doug, Here's a method of getting individual bytes or characters from a hex value...
>> hex: to-hex length? "Fred Flintstone and Wilma Flintstone"
== #00000024
>> to-integer copy/part skip hex 6 2
== 36
>> to-char to-integer copy/part skip hex 6 2
== #"$"
>> to-integer copy/part skip hex 4 2
== 0
>> to-char to-integer copy/part skip hex 4 2
== #"^-" Obviously Anton's approach is much shorter for your specific problem, but the above at least shows there's a relatively pure way in REBOL to extract bytes from a hex value. -- Carl Read. On Tuesday, 15-April-2008 at 15:38:49 Anton Rolls wrote,

 [6/6] from: logan:steeve:g:mail at: 15-Apr-2008 10:31


don't forget debase !!! dec-to-bin: func [d][debase/base at to-hex d 5 16]
>> dec-to-bin 36
== #{0024}

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