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.
Related
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);
I need to convert the following integers numbers
29900
17940
1
in to decimal format like
299.00
179.40
0.01
I tried already Data/Types.pm but
to_decimal(1, 2)
return 1.00
Perl does not have data types in the sense of integer, float or string. All you need to do is divide by 100. If you want an output with two decimals, use sprintf to format it.
printf '%.02d', 29900 / 100;
Will output 299.00. Note that printf is like sprintf, but with printing instead of returning.
You can read perldata to learn more about what kinds of data Perl has.
Under the hood at the XS and C layer, there are of course data types. You can learn about them in perlguts. But the whole point of a higher language is to abstract those things away. So if all you do is write Perl code, you never need to care that those exist or how they work.
How to change the number of decimal digits?
Changing the format Matlab can show only 4 (if short) or 15 (if long). But I want exactly 3 digits to show.
To elaborate on Hamataro's answer, you could also use roundn function to round to a specific decimal precision, e.g.: roundn(1.23456789,-3) will yield 1.235. However, Matlab will still display the result in either of the formats you have mentioned, i.e 1.2350 if format is set to short, and 1.235000000000000 if format is set for long.
Alternatively, if you use sprintf, you can use the %g formatting option to display only a set number of digits, regardless of where the decimal point is. sprintf('%0.3g',1.23456789) yields 1.23; sprintf('%0.3g',12.3456789) yields 12.3
You can either use sprintf or do *
var2 = round(var1*1000)/1000
I am trying to unpack a variable containing a string received from a spectrum analyzer:
#42404?û¢-+Ä¢-VÄ¢-oÆ¢-8æ¢-bÉ¢-ôÿ¢-+Ä¢-?Ö¢-sÉ¢-ÜÖ¢-¦ö¢-=Æ¢-8æ¢-uô¢-=Æ¢-\Å¢-uô¢-?ü¢-}¦¢-=Æ¢-)...
The format is real 32 which uses four bytes to store each value. The number #42404 represents 4 extra bytes present and 2404/4 = 601 points collected. The data starts after #42404. Now when I receive this into a string variable,
$lp = ibqry($ud,":TRAC:DATA? TRACE1;*WAI;");
I am not sure how to convert this into an array of numbers :(... Should I use something like the followin?
#dec = unpack("d", $lp);
I know this is not working, because I am not getting the right values and the number of data points for sure is not 601...
First, you have to strip the #42404 off and hope none of the following binary data happens to be an ASCII number.
$lp =~ s{^#\d+}{};
I'm not sure what format "Real 32" is, but I'm going to guess that it's a single precision floating point which is 32 bits long. Looking at the pack docs. d is "double precision float", that's 64 bits. So I'd try f which is "single precision".
#dec = unpack("f*", $lp);
Whether your data is big or little endian is a problem. d and f use your computer's native endianness. You may have to force endianness using the > and < modifiers.
#dec = unpack("f*>", $lp); # big endian
#dec = unpack("f*<", $lp); # little endian
If the first 4 encodes the number of remaining digits (2404) before the floats, then something like this might work:
my #dec = unpack "x a/x f>*", $lp;
The x skips the leading #, the a/x reads one digit and skips that many characters after it, and the f>* parses the remaining string as a sequence of 32-bit big-endian floats. (If the output looks weird, try using f<* instead.)
I have a really big number in Perl. I use "bignum". How can I extract single digits out of this big number. For example if I have a number like this and what to get the 3rd digit from the end:
1029384710985234058763045203948520945862986209845729034856
-> 8
bignums are transparently available, so this will work:
$digit = substr($bignum, -3, 1);
The bignum package uses Math::BigInt under the hood for integers.
From the Math::BigInt man page:
$x->digit($n); # return the nth digit, counting from right
Note that counting starts at 0 for the rightmost digit.