in reply to Making two 8-bit numbers from indices of set bits

Perhaps this

$ perl -le ' > vec($str, $_, 1) = 1 for 1, 8, 9; > $high = unpack q{xc}, $str; > $low = unpack q{c}, $str; > print qq{$high - $low};' 3 - 2 $

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Making two 8-bit numbers from indices of set bits
by carcassonne (Pilgrim) on Feb 20, 2007 at 02:44 UTC

    Works fine. Thanks. When unselecting the only selection of the multiple choice list, ncurses will return an empty array that cannot go through, so I've added a condition:

    # (inside ncurses listbox callback) my $listbox = shift; my @sel = $listbox->id; $byte2 = 0; $byte1 = 0; if (@sel) { my $code; $code |= 1 << $_ for @sel; $byte2 = $code >> 8; $byte1 = $code & 0xFF; }

    I've taken that solution although the unpack and pack is interesting. Thanks again !

    Cheers.

    Al