0

I am writing a code where I need to convert some forms of no to binary no for some processing. I want the size of binary no based on the value stored in a variable for which I need to append some leading zeroes. for example, say there is variable $size =15 Now I want to convert a no from decimal to 15 bit binary

I know how to manually give the count for ex - if we want 8 digit binary no from some decimal no then we will use-

    $data_binary = sprintf( "%08b", $initial_data );

But how to specify the size of binary no if the size is stored in some variable??

For example=

    If size =10
    decimal no =12
    required binary no = 0000001100
rikki
  • 431
  • 1
  • 8
  • 18
  • 3
    Possible duplicate of [How do I use sprintf to zero fill to a variable length in Perl?](https://stackoverflow.com/questions/2471298/how-do-i-use-sprintf-to-zero-fill-to-a-variable-length-in-perl) – Dada Mar 26 '19 at 07:44

2 Answers2

1

See perldoc -f sprintf:

$data_binary = sprintf( "%0*b", $size, $initial_data );

For instance,

printf "%0*b\n", 10, 12

prints

0000001100

If the binary representation of your number contains more than $size bits, it won't be truncated. Depending on your use case, you might want to add a substr afterward (but I'd be surprised if you'd actually need to do that).

Dada
  • 6,313
  • 7
  • 24
  • 43
  • Just found out that this is a duplicate; gonna delete this answer. – Dada Mar 26 '19 at 07:45
  • Well, I definitely don't like the accepted answer, so I'm undeleting this answer, which I think provides a much better way to achieve what OP wants. – Dada Mar 26 '19 at 09:43
-1

You can use one sprintf to generate format string for another sprintf.
[ There is more than one way to do it ]

use strict; use warnings;
my $binary_length = 10;
my $initial_data = 12;

my $format = sprintf('%%0%db', int($binary_length));
my $data_binary = sprintf( $format, $initial_data );
print $data_binary,"\n";
AnFi
  • 10,493
  • 3
  • 23
  • 47
  • 1
    That's unnecessarily complicated. Also, why are you converting `$binary_length` to a float first? – melpomene Mar 26 '19 at 08:04
  • @melpomene 1. It is "general" (any programming language) way. It may be easily improved for perl. 2. `int($binary_length)` - I prefer such extra safeguards. – AnFi Mar 26 '19 at 08:17
  • 1
    `int($binary_length)` is not a safeguard. It unnecessarily converts the number to a float. – melpomene Mar 26 '19 at 08:19
  • 1
    This solution is not general: It only works in programming languages where `printf` can take a runtime string. A truly general solution would be `sprintf '%0*b', $binary_length, $initial_data`, which is also the shortest / simplest solution (except `%b` is non-standard). – melpomene Mar 26 '19 at 08:22
  • @melpomene You can provide better answer (in your opinion). – AnFi Mar 26 '19 at 09:35
  • 1
    @AnFi This question is a duplicate. The better answers on the [dup target](https://stackoverflow.com/questions/2471298/how-do-i-use-sprintf-to-zero-fill-to-a-variable-length-in-perl). – Dada Mar 26 '19 at 09:40