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
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.
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:
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.
$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.