How many elements are there in the array.

H

hara

Suppose @result=1101(this value may vary)
How can i get the number of bits in this array and then make it 8 digit
by adding 0s on the left of this array.
then i will have to convert that 8 bit variable to decimal by taking 4
bits at a time.

How can i do this?
regards
 
J

Jürgen Exner

hara said:
Suppose @result=1101(this value may vary)

1101 is a number, not an array. It is the same as saying
$array[0]=1101;
Your are assigning the number onethousandonehundredandone to the first
element of that array.
How can i get the number of bits in this array

Why would you want to know those implementation details? They are handled by
the interpreter and of no concern for you unless you are doing some
lowest-level hacking.

If you want to know the number of elements in an array then just use it in
scalar context (can be enforces using scalar()), but in your example that
will be exactly one because you assigned the first element of that array
only.
and then make it 8
digit by adding 0s on the left of this array.

??? Whatdayamean???

If you want to add something to an array then you can use push(0) or
unshift(0), depending upon what you mean by "left side".

jue
 
A

Alucard

Agree, and regarding to your second questions, it may help:

my $num = 1101;
printf("%08d", $num);
 
N

news reader

Hi,


Your question is not 100% clear.
But I assume that that 1101 is not a decimal number, but a string
representing a base '2' value, so you mean actually that 1101 is
equivalent to dec 13 or 0x0d, right?

I assume you want to convert this binary string into an 8 bit string
value in order to do then a bin to decimal conversion with some
(non specified) functions.


So if you just want to have an integer variable containing the decimal
version of your binary string, then try


$a= "1101"; # as already pointed out this is just a scalar,
# not an array if it really were an array with one
# character per element, then you could just
# do $a=join('',@yourarray);

$val=oct("0b$a"); #oct can be used to convert binary / octal or
# hex values to decimal integers
# oct("010") will be 8
# oct("0x10") will be 16
# oct("0b10") will be 2
# This idea has been taken from the
# perl cook book 2nd edition (recipe 2.15)

or with

$val=unpack("C",pack("b".length($a),$a);



However I cannot comment and never tried of both constructs may be faster.


If you want to have a byte corresponding to your binary string
such, that '1000001' would become 'A', then you could try


$val = chr(oct("ob$a"));
or
$val = pack("b".length($a),$a);


Again: I never profiled if you'd be better off with oct()/chr() or with
pack()/unpack() commands.


bye


N
 
H

hara

Thanks a lot. It helped me solving my problem to some extent.
But where i am stuck is
i have got a scalar variable which stores 32 bits of 1s and 0s.
like-
$res=00000000001000000000000000000000
I want to convert it to octal number by taking 4 bits at a time
like
0000 0000 0010 0000 0000 0000 0000 0000
0 0 2 0 0 0 0 0
so the $res will become
$res=00200000
How can i do this.
Regards
 
H

hara

but how can i save this printf("%08d", $num) into a variable.
i tried doing this but not working.
like
$res=("%08d", $num) is not working.
can u suggest something
like
printf("%08d", $num) is printing on the screen but not saving it in a
variable.
 
T

tuser

hara said:
i have got a scalar variable which stores 32 bits of 1s and 0s.
like-
$res=00000000001000000000000000000000
I want to convert it to octal number by taking 4 bits at a time
like
0000 0000 0010 0000 0000 0000 0000 0000
0 0 2 0 0 0 0 0
so the $res will become
$res=00200000
How can i do this.

use warnings;
use strict;

# starting with $res:
# *******************
my $res = '00000000001000000000000000000000';
print "Starting with ", length($res), " characters:\n";
print "'$res'\n\n";

# Taking 4 bits at a time, result will always be hex:
# ***************************************************
{
my $hex = '';
my $temp = $res;
while ($temp =~ s/(\d{4})$//) {
$hex = sprintf('%X', oct("0b$1")).$hex;
}
unless ($temp eq '') {
$hex = sprintf('%X', oct("0b$temp")).$hex;
}
print "Taking 4 bits at a time, result will be hex:\n";
print "'$hex'\n\n";
}

# Taking 3 bits at a time, result will always be oct:
# ***************************************************
{
my $oct = '';
my $temp = $res;
while ($temp =~ s/(\d{3})$//) {
$oct = oct("0b$1").$oct;
}
unless ($temp eq '') {
$oct = oct("0b$temp").$oct;
}
print "Taking 3 bits at a time, result will be oct:\n";
print "'$oct'\n\n";
}

=result
Starting with 32 characters:
'00000000001000000000000000000000'

Taking 4 bits at a time, result will be hex:
'00200000'

Taking 3 bits at a time, result will be oct:
'00010000000'
=cut
 
D

David Squire

hara said:
but how can i save this printf("%08d", $num) into a variable.
i tried doing this but not working.
like
$res=("%08d", $num) is not working.
can u suggest something
like
printf("%08d", $num) is printing on the screen but not saving it in a
variable.

Good grief! You have already been shown how to do this several times.
Here's one:
http://groups.google.com/group/comp.lang.perl.misc/msg/29b4f7d5cd1d550a

Make sure you read it all.

DS
 
J

Jürgen Exner

hara said:
Thanks a lot. It helped me solving my problem to some extent.

What is "it"? Please quote appropriate context such that people have a
chance to know what you are talking about!
But where i am stuck is
i have got a scalar variable which stores 32 bits of 1s and 0s.

No, you don't.
like-
$res=00000000001000000000000000000000

The content of $res is _NOT_ 32 bits of 1s an 0s. It's the number 10^20 (if
I counted all those 0s correctly). If this number is internally stored as 32
bits or 64 bits or 16 bits is first depending on your Perl installation and
second of no interest to a Perl programmer.
I want to convert it to octal number by taking 4 bits at a time
like
0000 0000 0010 0000 0000 0000 0000 0000
0 0 2 0 0 0 0 0
so the $res will become
$res=00200000

Now, this $res is the number twohundredthousand. Obviously 10^20 and
twohundredthousand have just about nothing to do with each other.
How can i do this.

What about finally accepting that there is a difference between a number and
a numeral. When you are writing $res = 1234 then by definition of Perl this
is always the decimal numeral that is representing the number
onethousandtwohundredthirtyfour.
When printing this number onethousandtwohundredthirtyfour by default Perl
uses the decimal numeral system again, but that can be changed by specifying
a different numeral system in printf. Details see the documentation of
printf().

jue
 
J

Jürgen Exner

hara said:
but how can i save this printf("%08d", $num) into a variable.
i tried doing this but not working.
like
$res=("%08d", $num) is not working.

Dohhh, why would you possibly expect that assigning a list to a variable
would evaluate that list as arguments to printf?
can u suggest something
like
printf("%08d", $num) is printing on the screen but not saving it in a
variable.

Well, yes, that is what printf is designed to do. Did you read the
documentation of printf?
From "perldoc -f printf":
printf FORMAT, LIST
Equivalent to "print FILEHANDLE sprintf(FORMAT, LIST)",
[...]

So, just check out sprintf.

jue
 
H

hara

Suppose i have got a hex number
i.e $hex
i want to concatenate it with "E0000000"
that is if hex number is 00200000
then i want the result to be "E0200000"
How can i do that.
Join is giving a garbage value
If i am doing 00200000 | E0000000
then also getting garbage value.
Kindly help me getting it right.
 
T

Tad McClellan

hara said:
but how can i save this printf("%08d", $num) into a variable.


They way it says to in the documentation for the function you are using.

can u suggest something


Read the documentation for the functions that you use.

perldoc -f printf

When is says "See <other function>", you should also go read
the documentation for <other function>.

This newsgroup is not a "read the docs to me" service.
 
T

Tad McClellan

hara said:
Thanks a lot.


Thank who for what?

Please quote context in followups.

Please start to do this very very soon.

i have got a scalar variable which stores 32 bits of 1s and 0s.


You are confusing what is stored, with what is represented.

$res=00000000001000000000000000000000


Post real Perl code!

Post real Perl code!

Please start to do this very soon too.

You have got a scalar variable which stores 32 characters, *not*
32 bits. (the characters _represent_ 32 bits of 1s and 0s.)

I want to convert it to octal number by taking 4 bits at a time
How can i do this.


----------------------
#!/usr/bin/perl
use warnings;
use strict;

my $res = '00000000001000000000000000000000';
foreach my $four_chars ( $res =~ /(\d{4})/g ) {
print "$four_chars \n";
}
 
T

Tad McClellan

hara said:
Suppose i have got a hex number


There is NO SUCH THING as a "hex number"!

Join is giving a garbage value


If you show us your broken code, then we can probably help you fix it.

If you don't, then we can't.

Kindly help me getting it right.


Kindly help us help you in getting right, by *posting real code*.
 
J

Jürgen Exner

hara said:
Suppose i have got a hex number

There is no such thing but for the sake of argument let's assume you meant a
number in hexadezimal format
i.e $hex
i want to concatenate it with "E0000000"
that is if hex number is 00200000

That is not a number in hexadezimal but in octal format. If you want
hexadezimal format then use 0x as the prefix.
See "perldoc perldata"
then i want the result to be "E0200000"

???
If I concatenate "E0000000" with "00200000" then I would expect to get
""E000000000200000" (or something similar after figuring out what you
actually mean with all those contradicting numeral systems).
Did you mean "add" instead of "concatenate" by chance?
How can i do that.

Step one: figure out which numeral system you want to use AND THEN USE THE
PROPER PREFIX.
Step two: figure out which operation you want to perform AND THEN USE IT
(obviously it is not concatenation)
Step three: printf() the result using the correct printf format specifier
for your desired numeral system
Join is giving a garbage value
If i am doing 00200000 | E0000000
then also getting garbage value.

That's not surprising. You are starting out with garbage (00200000 is not
hex but octal, "E0000000" is a string with a numerical value of 0). I cannot
say if a bitwise is what you are looking for. To me it looks more like a
plain addition, but of course I may be wrong.
And join is just concatenation on steroids.
Kindly help me getting it right.

Then what about if FINALLY you follow the posting guidelines and post some
real Perl code? How do you expect us to help you when keep throwing tiny,
unrelated code fragments at us which contradict not only each other but also
what you are writing in clear text.

jue
 
J

John Bokma

Jürgen Exner said:
There is no such thing but for the sake of argument let's assume you
meant a number in hexadezimal format

I've heard of hex, I never heard of hexadezimal though :-D.

I think it's ok to call 0xf3e a hexadecimal number, altough a number given
hexadecimal notation would probably be better. And the former is shortened
to hex number, or even hex.
 
J

John Bokma

Jürgen Exner said:

I quote:

"Some hexadecimal numbers are indistinguishable from a decimal number (to
both humans and computers). Therefore, some convention is usually used to
flag them."

"In mathematics and computer science, base-16, hexadecimal, or simply hex,
is a numeral system with a radix or base of 16 usually written using the
symbols 0–9 and A–F or a–f."

which agrees with my earlier post, and shows that hex number is ok (if we
can trust the quoted source, that is).

The never heard of, was a joke, I guessed it was German, so:
http://de.wikipedia.org/wiki/Hexadezimal

"Um eine hexadezimale Zahl von einer normalen Dezimalzahl unterscheiden zu
können, existieren mehrere Schreibweisen."

hexadezimale Zahl is shortened to hex Zahl in German, or Google gives
close to 10,000 results for "hex Zahl".
 
T

Tad McClellan

John Bokma said:
I think it's ok to call 0xf3e a hexadecimal number, altough a number given
hexadecimal notation would probably be better.


The abstract concept of a number is independant of its representation.

But us feeble humans need to represent it somehow to aid our understanding.

If you just say "a number", humans will take it to be base 10 (as
does perl).

If you have a number in some other representation, then you'd better
indicate that somehow (eg. in hex notation) or confusion will ensue.

And the former is shortened
to hex number, or even hex.


It is not the former (hexadecimal number) that is shortened
that way, it is the _later_ (given in hexadecimal notation).

in hex notation

is often shortened to "in hex" or even to just "hex" when the
context is clear.


But boy howdy! The context is not at all clear with this OP...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top