Converting decimal 100 to binary 100 [duplicate] - perl

This question already has answers here:
How do I convert a binary string to a number in Perl?
(5 answers)
Closed 4 years ago.
I want to convert decimal number to exact same binary number.
Eg. Decimal (10) to Binary (10).
Dec(10000001110000110) to binary(10000001110000110).
I am new to perl. The Lame approach I can think of is to convert dec to string and string to binary but I am sure there has to be a better way to achieve it.
I am using a function to get an array of decimal numbers.
#Arr= {111110000,110100010,...}
Note this is decimal number since when I tried to subtract 1 from it's elements I got 11110000-1=11109999.
I need it in binary format so that I can do a logical AND operation on it.
Can someone please suggest on how to achieve it.

Converting the number 1000000111000011010 to the number 100000011100001102 is rather easy.
Obtain the decimal representation of the number. This is as simple as stringifying the number.
Pass the the decimal representation of the number to a routine that converts from binary to the represented number. oct('0b'.$_) does this.
Solution:
my #bad = ( 111110000, 110100010, ... );
my #fixed = map { oct('0b'.$_) } #bad; # 496, 418, ...

If I understand you correctly you want this:
my $num = 101010;
print interpret_as_binary($num); # 42
You could go through the labor of dividing the number by 2 and so on. Or you can take advantage that oct will interpret a string starting with 0b as a binary number.
print oct("0b". $num);

Related

How can I format a number to 2 decimal? In some case sprintf function could not show correct result [duplicate]

This question already has answers here:
Why is Perl inconsistent with sprintf rounding?
(2 answers)
Closed 10 months ago.
For example:
my $num = 9.4950;
print sprintf("%.2f", $num)
Output: 9.49
But it should print 9.50 result
4950/10000 is a periodic number in binary just like 1/3 is periodic in decimal. Specifically, 9.4950 is
____________________
1.001011 11110101110000101000 × 2^3
It would take infinite resources to store this number as a floating point number. Due to limited resources, a slightly smaller number is stored instead.
1.001011 11110101110000101000 11110101110000101000 111101 x 2^3
In decimal:
$ perl -e'printf "%.100g\n", 9.4950'
9.4949999999999992184029906638897955417633056640625
Since the third decimal digits is less than 5, %.2f correctly rounds this down to 9.49.

Hex conversion in Perl

I have to convert decimal values to hex.In my code code I am using
$hex = sprintf("0x%X", $temp);
But here the problem am facing is if the decimal value is 35, then am getting hex value as 0x23.But Ineed the out put as 0x023. Is there any way to achieve this?
You do not need to add the 0x part, and you can use zeros to justify:
sprintf "%#05x", 35; # 0x023
Documentation here. Note that the "number of zeros" to justify is 5 here and not 3 as one might think.

How to store more than 4 decimal places in an array in MATLAB

I want to store 6 decimal digits into an array, but when I store it into array it only stores up to 4 decimal digits instead of 6. How can I store up to 6 digits into an array?
For example, if
e=0.059995;
W(l,i)=e;
but W(l,i) gives me the result as 4 decimal places
disp(W(l,i))
0.0600
How can I store 6 decimal digits into an array, i.e when I print the array it prints
6 decimal places?
disp(W(l,i))
0.059995
Can anyone help me?
Matlab on default settings stores up to 15 digits. It only your display format. Have a look at the format command.
Or just type at the Matlab command prompt:
format long
If you know you have only 6 digits you can use
sprintf('%0.6f', W(l,i))
instead of disp

Matlab function/script to convert a real number to a hexidecimal single precision repersentation and back again [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
32 bit hex to 32 bit floating point (IEEE 754) conversion in matlab
I am trying to test the functionality of a filter written in VHDL, the input of this filter is a single precision floating point number. To do this I want to convert an array of real numbers in MATLAB, representing a sine wave to an array of hexadecimal representation of floating point numbers. Apply this array to the filter and convert the output to real values.
I.e. I need a function to perform the following -3.48 = 0x"C05EB851", the function performed on this site and it's inverse.
Does anyone have a MATLAB function/m-file to perform this operation? any help is greatly appreciated
Cheers
>> help num2hex
NUM2HEX Convert singles and doubles to IEEE hexadecimal strings.
If X is a single or double precision array with n elements,
NUM2HEX(X) is an n-by-8 or n-by-16 char array of the hexadecimal
floating point representation. The same representation is printed
with FORMAT HEX.
Let's try your example:
>> num2hex(single(-3.48))
ans =
c05eb852
close enough?

lisp program for hexadecimal to decimal

how to write a lisp program to convert given hexadecimal number into decimal. can somebody give me a clue.
thank you
I'm assuming its a homework problem so i'll give you a hint in the right direction.
Here is how to convert decimal to binary ->
Lets say you start with the number 9 in binary its 1001.
Start of by dividing 9 by 2. You get 4 with remainder 1. Save the remainder.
Now divide that 4 by 2 again, you get 2 with remainder 0. Save the remainder.
Divide that 2 again by 2, you get 1 with remainder 0. Save the remainder.
Divide that 1 by 2 and finally you get 0 with reaminder 1. Save the remainder.
If you read the saved remainders backwards you get 1001! The binary number you've been looking for. Best to push the remainders on the stack and pop them back out, that way they'll come out backwards.
It's already provided by Common Lisp.
The input is the string for the hex integer.
Then you parse the integer with radix 16
the result is the number
if you write the number with base 10 to an output stream, then you can get the number as a string in base 10