here i want to divide 300 such that half
of it get stores in $dataIn[20] and other
half get stores in $data[21]
You neglected to explain why you want to do this.
It's a fairly unusual thing to want to do. I
suppose most likely you're trying to deal with
some kind of weird binary protocol or file format,
but you don't say. Consequently, it's hard to
be certain what you mean by "half of it".
(That was Jaap's point.) Anonymous Monk's
answer is one thing you might plausibly have
meant, although I doubt it. But it's hard to tell.
Your node title implies that you believe array
elements only hold one byte; that is not the case.
Based on your code, though, I'm guessing maybe you
have some other reason for wanting values in the
array that can, in some later part of the code,
be used as one-byte numbers. But that's a guess,
since you don't show us the part of the code that
needs the one-byte numbers.
Your code also has a couple of other problems in
it, that seem unrelated to the question you asked,
but may potentially be related to the problem you
are having. For instance, you assign a number into
an array called errorlist in one line, and in the
next line you assume that the number is in a scalar
called var. Also, you seem to be confused about the
name of the other array; is it dataIn, or just data?
If you're having this type of problem, you probably
should use warnings, and possibly also
use strict. These things will catch
that kind of error for you automatically, and warn
you about it, so that you don't have to spend a lot
of time tracking it down. Very handy.
In any event, you can use modular arithmetic (the
% operator) to split up a number into byte-sized
chunks, if you really do need that. Here is one
(untested) way to do that...
my $fullsizenumber = 1027;
my @bytesizechunk = splitintobytes($fullsizenumber);
sub splitintobytes {
my ($num) = @_;
warn "Not numeric: $num" unless $num eq ($num+0);
my @result;
while ($num >= 256) {
push @result, $num % 256;
$num -= $result[-1];
$num /= 256;
}
return (@result, $num);
}
Note that this version returns the least significant
byte first; if you want the MSB first, use
reverse. The need to worry about byte
order is one of a number of reasons why most
programmers prefer not to fiddle with
byte-oriented binary file formats and protocols.
(Sometimes it cannot be avoided, of course... but
if it can be avoided, it should be,
because it consumes time on fiddly details that
you could rather spend on the actual
programming logic instead.)
HTH.HAND
"In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."
— Pratico & Van Pelt, BBHG, p68
|