Appending 0 before binary number based on a value in a variableHow do I use sprintf to zero fill to a variable length in Perl?Right shift a binary no and get the shifted bits in a variablePerl Decimal to Binary 32-bit then 8-bitPerl appending (s)printf output to stringHow to determine bitwise binary scores for a list of number in perl?read/input hexadecimal number from keyboardPerl remove digits from a binary numberPerl int to arrayConvert decimal to four hex digitsHow to store the number of rows returned using SELECT in a variableConvert a byte stored as string into bits in Perl and modify itConverting decimal 100 to binary 100

Why limit to revolvers?

Manually select/unselect lines before forwarding to stdout

Why do legislative committees exist?

Is dividends exclusively a part of earnings?

Will it hurt my career to work as a graphic designer in a startup for beauty and skin care?

Why did Spider-Man take a detour to Dorset?

Can both line and load go to same screw on a GFCI outlet?

Why does the Trade Federation become so alarmed upon learning the ambassadors are Jedi Knights?

Can you perfectly wrap a cube with this blocky shape?

Can't update Ubuntu 18.04.2

Re-negotiate salary once I earn my diploma

Why doesn't philosophy have higher standards for its arguments?

In special relativity is mass just a measure of all other energy than kinetic?

Problem with interpolating function returned by NDEigensystem

Is there a way to handmake alphabet pasta?

What impact would a dragon the size of Asia have on the environment?

I gave my characters names that are exactly like another book. Is it a problem?

What are the arguments for California’s nonpartisan blanket primaries?

What's the phrasal verb for carbonated drinks exploding out of the can after being shaken?

Why do the faithful have to say "And with your spirit " in Catholic Mass?

When does Fisher's "go get more data" approach make sense?

If I stood next to a piece of metal heated to a million degrees, but in a perfect vacuum, would I feel hot?

Is `curl something | sudo bash -` a reasonably safe installation method?

Can a polymorphed creature understand languages spoken under the effect of Tongues?



Appending 0 before binary number based on a value in a variable


How do I use sprintf to zero fill to a variable length in Perl?Right shift a binary no and get the shifted bits in a variablePerl Decimal to Binary 32-bit then 8-bitPerl appending (s)printf output to stringHow to determine bitwise binary scores for a list of number in perl?read/input hexadecimal number from keyboardPerl remove digits from a binary numberPerl int to arrayConvert decimal to four hex digitsHow to store the number of rows returned using SELECT in a variableConvert a byte stored as string into bits in Perl and modify itConverting decimal 100 to binary 100






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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









share|improve this question

















  • 3





    Possible duplicate of How do I use sprintf to zero fill to a variable length in Perl?

    – Dada
    Mar 26 at 7:44

















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









share|improve this question

















  • 3





    Possible duplicate of How do I use sprintf to zero fill to a variable length in Perl?

    – Dada
    Mar 26 at 7:44













0












0








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









share|improve this question














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






perl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 7:23









rikkirikki

1099 bronze badges




1099 bronze badges







  • 3





    Possible duplicate of How do I use sprintf to zero fill to a variable length in Perl?

    – Dada
    Mar 26 at 7:44












  • 3





    Possible duplicate of How do I use sprintf to zero fill to a variable length in Perl?

    – Dada
    Mar 26 at 7:44







3




3





Possible duplicate of How do I use sprintf to zero fill to a variable length in Perl?

– Dada
Mar 26 at 7:44





Possible duplicate of How do I use sprintf to zero fill to a variable length in Perl?

– Dada
Mar 26 at 7:44












2 Answers
2






active

oldest

votes


















1














See perldoc -f sprintf:



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


For instance,



printf "%0*bn", 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).






share|improve this answer























  • Just found out that this is a duplicate; gonna delete this answer.

    – Dada
    Mar 26 at 7: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 at 9: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";





share|improve this answer


















  • 1





    That's unnecessarily complicated. Also, why are you converting $binary_length to a float first?

    – melpomene
    Mar 26 at 8: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 at 8:17







  • 1





    int($binary_length) is not a safeguard. It unnecessarily converts the number to a float.

    – melpomene
    Mar 26 at 8: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 at 8:22






  • 1





    @AnFi This question is a duplicate. The better answers on the dup target.

    – Dada
    Mar 26 at 9:40













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55351735%2fappending-0-before-binary-number-based-on-a-value-in-a-variable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














See perldoc -f sprintf:



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


For instance,



printf "%0*bn", 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).






share|improve this answer























  • Just found out that this is a duplicate; gonna delete this answer.

    – Dada
    Mar 26 at 7: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 at 9:43
















1














See perldoc -f sprintf:



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


For instance,



printf "%0*bn", 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).






share|improve this answer























  • Just found out that this is a duplicate; gonna delete this answer.

    – Dada
    Mar 26 at 7: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 at 9:43














1












1








1







See perldoc -f sprintf:



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


For instance,



printf "%0*bn", 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).






share|improve this answer













See perldoc -f sprintf:



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


For instance,



printf "%0*bn", 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).







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 26 at 7:37









DadaDada

1,9021 gold badge10 silver badges21 bronze badges




1,9021 gold badge10 silver badges21 bronze badges












  • Just found out that this is a duplicate; gonna delete this answer.

    – Dada
    Mar 26 at 7: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 at 9:43


















  • Just found out that this is a duplicate; gonna delete this answer.

    – Dada
    Mar 26 at 7: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 at 9:43

















Just found out that this is a duplicate; gonna delete this answer.

– Dada
Mar 26 at 7:45





Just found out that this is a duplicate; gonna delete this answer.

– Dada
Mar 26 at 7: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 at 9:43






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 at 9: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";





share|improve this answer


















  • 1





    That's unnecessarily complicated. Also, why are you converting $binary_length to a float first?

    – melpomene
    Mar 26 at 8: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 at 8:17







  • 1





    int($binary_length) is not a safeguard. It unnecessarily converts the number to a float.

    – melpomene
    Mar 26 at 8: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 at 8:22






  • 1





    @AnFi This question is a duplicate. The better answers on the dup target.

    – Dada
    Mar 26 at 9:40















-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";





share|improve this answer


















  • 1





    That's unnecessarily complicated. Also, why are you converting $binary_length to a float first?

    – melpomene
    Mar 26 at 8: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 at 8:17







  • 1





    int($binary_length) is not a safeguard. It unnecessarily converts the number to a float.

    – melpomene
    Mar 26 at 8: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 at 8:22






  • 1





    @AnFi This question is a duplicate. The better answers on the dup target.

    – Dada
    Mar 26 at 9:40













-1












-1








-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";





share|improve this answer













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";






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 26 at 8:02









AnFiAnFi

8,2203 gold badges17 silver badges41 bronze badges




8,2203 gold badges17 silver badges41 bronze badges







  • 1





    That's unnecessarily complicated. Also, why are you converting $binary_length to a float first?

    – melpomene
    Mar 26 at 8: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 at 8:17







  • 1





    int($binary_length) is not a safeguard. It unnecessarily converts the number to a float.

    – melpomene
    Mar 26 at 8: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 at 8:22






  • 1





    @AnFi This question is a duplicate. The better answers on the dup target.

    – Dada
    Mar 26 at 9:40












  • 1





    That's unnecessarily complicated. Also, why are you converting $binary_length to a float first?

    – melpomene
    Mar 26 at 8: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 at 8:17







  • 1





    int($binary_length) is not a safeguard. It unnecessarily converts the number to a float.

    – melpomene
    Mar 26 at 8: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 at 8:22






  • 1





    @AnFi This question is a duplicate. The better answers on the dup target.

    – Dada
    Mar 26 at 9:40







1




1





That's unnecessarily complicated. Also, why are you converting $binary_length to a float first?

– melpomene
Mar 26 at 8:04





That's unnecessarily complicated. Also, why are you converting $binary_length to a float first?

– melpomene
Mar 26 at 8: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 at 8:17






@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 at 8:17





1




1





int($binary_length) is not a safeguard. It unnecessarily converts the number to a float.

– melpomene
Mar 26 at 8:19





int($binary_length) is not a safeguard. It unnecessarily converts the number to a float.

– melpomene
Mar 26 at 8:19




1




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 at 8:22





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 at 8:22




1




1





@AnFi This question is a duplicate. The better answers on the dup target.

– Dada
Mar 26 at 9:40





@AnFi This question is a duplicate. The better answers on the dup target.

– Dada
Mar 26 at 9:40

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55351735%2fappending-0-before-binary-number-based-on-a-value-in-a-variable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript