Output in Specific Bytes

11 views
Skip to first unread message

Andy Hurd

unread,
Mar 30, 2010, 7:35:48 AM3/30/10
to Sleep Developers
Hello All,
I am kind of new to the sleep movement but I am really enjoying
it. I am writing a sleep program to basically translate a file into
another format.

I was wondering how to output to a specific byte offset?

I have 6 variables (all my parsing works great) but when I output them
I need:

v1 to take bytes 1-2
v2 to take bytes 3-4
v3 to take bytes 5-16
v4 to take bytes 17
v5 to take bytes 18
v6 to take bytes 19-30


Is there an easy way to do this with sleep?

Any help will be greatly appreciated,

Andy Hurd

Raphael Mudge

unread,
Mar 30, 2010, 11:11:27 AM3/30/10
to sleep-de...@googlegroups.com
Hi Andy,
Marty got to you I see. Sleep has a bunch of functions for working
with bytes. I recommend taking a look at: http://sleep.dashnine.org/manual/io.html#3

Here are two options for you to try. One loops over the byte data and
extracts each byte using byteAt. The other option uses unpack to
unpack the bytes and represent them as hex characters (two at a time).
You can use H to put the high nybble first or h to put the low nybble
first.

$data = readb(openf("data.txt"), -1);

# make sure we're dealing with an event number of bytes

if ((strlen($data) % 2) == 1) {
$data .= "\0";
}

# option A

for ($x = 0; $x < strlen($data); $x += 2) {
# map over the array returns hex format
($a, $b) = map({ return formatNumber($1, 10, 16); }, @(byteAt
($data, $x), byteAt($data, $x + 1)));
println("$[4]x $+ : $a $b");
}

# option B

for ($x = 0; $x < strlen($data); $x += 2) {
# map over the array returns hex format
$temp = unpack("H*", mid($data, $x, 2))[0];
println("$[4]x $+ : $temp");
}

-- Raphael

> --
> -----
> To post to this group, send email to sleep-de...@googlegroups.com
> To unsubscribe from this group, send email to sleep-develope...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sleep-developers?hl=en
>
> The Sleep Scripting Project: http://sleep.dashnine.org/
>
> To unsubscribe from this group, send email to sleep-developers
> +unsubscribegooglegroups.com or reply to this email with the words
> "REMOVE ME" as the subject.

Raphael Mudge

unread,
Mar 30, 2010, 11:23:26 AM3/30/10
to sleep-de...@googlegroups.com
I just noticed you wanted some pretty specific bytes, not every 2
bytes. If you're unpacking a C data structure (which I suspect), then
you can specify in unpack the types you want each variable unpacked
as. I wrote about doing this in the I/O chapter.

Simplest to understand one:

($v1, $v2, $v4, $v5, $v6) = unpack("H2 H2 x13 H1 H1 H11", $data)
($v3) = unpack("x4 H9 x*", $data);

You *could* do this in one line using mark/reset inside of the unpack
pattern but this is pretty straight forward. H[number expected] gets 1
byte at a time and converts it into hex form putting the high nybble
first. x skips a byte with nothing output. unpack returns an array
and here we're using multiple assignment to assign the returned
contents of the array to each variable you're interested in.

($v3, $v1, $v2, $v4, $v5, $v6) = unpack("M x4 H9 R H2 H2 x13 H1 H1
H11", $data);

-- Raphael

On Mar 30, 2010, at 7:35 AM, Andy Hurd wrote:

ah...@nycap.rr.com

unread,
Mar 30, 2010, 11:38:15 AM3/30/10
to sleep-de...@googlegroups.com, Raphael Mudge
Rapheal,
What if I wanted to pack them up? Does it work the same way?

I already have my data into 6 variables. I just need to put them together into the specific bytes. So I would assume I could use the pack methods right?

And yup marty got to me.

Andy

> To unsubscribe from this group, send email to sleep-developers+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.

ah...@nycap.rr.com

unread,
Mar 30, 2010, 11:51:23 AM3/30/10
to sleep-de...@googlegroups.com, Raphael Mudge
What I meant to say is this:

$myout = pack("C x2 C x2 C x12 C C C x12", $first, $second, $third, $fourth, $fifth, $sixth);

Will that work treating each variable as a character and then specifying the number of characters for it to take up in the output variable?

Andy
---- Raphael Mudge <rsm...@gmail.com> wrote:

> To unsubscribe from this group, send email to sleep-developers+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.

Raphael Mudge

unread,
Mar 30, 2010, 11:54:03 AM3/30/10
to sleep-de...@googlegroups.com
x is for skipping a byte. Use a number after the C. So:

$myout = pack("C2 C2 C12 C C C12");

-- Raphael
Reply all
Reply to author
Forward
0 new messages