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;
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
add a comment |
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
3
Possible duplicate of How do I use sprintf to zero fill to a variable length in Perl?
– Dada
Mar 26 at 7:44
add a comment |
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
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
perl
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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).
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
add a comment |
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";
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 whereprintf
can take a runtime string. A truly general solution would besprintf '%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
|
show 1 more comment
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
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
add a comment |
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).
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
add a comment |
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).
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).
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
add a comment |
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
add a comment |
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";
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 whereprintf
can take a runtime string. A truly general solution would besprintf '%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
|
show 1 more comment
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";
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 whereprintf
can take a runtime string. A truly general solution would besprintf '%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
|
show 1 more comment
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";
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";
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 whereprintf
can take a runtime string. A truly general solution would besprintf '%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
|
show 1 more comment
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 whereprintf
can take a runtime string. A truly general solution would besprintf '%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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
Possible duplicate of How do I use sprintf to zero fill to a variable length in Perl?
– Dada
Mar 26 at 7:44