Convert Hex to Hex with base 2 python [duplicate]
This question already has an answer here:
Decorating Hex function to pad zeros
4 answers
How i convert number HEX with base 1 (0x1) to base 2 (0x01).
Hex_B1 = 0x1
Hex_B2 = 0x01
hex(int('110', 2)) = 0x06
python python-3.x
marked as duplicate by Prune, Martijn Pieters♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 at 17:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Decorating Hex function to pad zeros
4 answers
How i convert number HEX with base 1 (0x1) to base 2 (0x01).
Hex_B1 = 0x1
Hex_B2 = 0x01
hex(int('110', 2)) = 0x06
python python-3.x
marked as duplicate by Prune, Martijn Pieters♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 at 17:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I think you mean you want leading zeros?
– Stephen Cowley
Nov 12 at 17:34
If you Google the phrase "Python convert hex to binary", you’ll find tutorials that can explain it much better than we can in an answer here.
– Prune
Nov 12 at 17:36
You are using the wrong terminology here. Hex is a base 16 numerical notation, base 2 is binary. You are instead trying to format hexadecimal to using 2 digits, padding with zero as needed.
– Martijn Pieters♦
Nov 12 at 17:37
@GuiLuiz You mean converting binary (base 2) to hex (base 16) ?
– p._phidot_
Nov 12 at 17:37
add a comment |
This question already has an answer here:
Decorating Hex function to pad zeros
4 answers
How i convert number HEX with base 1 (0x1) to base 2 (0x01).
Hex_B1 = 0x1
Hex_B2 = 0x01
hex(int('110', 2)) = 0x06
python python-3.x
This question already has an answer here:
Decorating Hex function to pad zeros
4 answers
How i convert number HEX with base 1 (0x1) to base 2 (0x01).
Hex_B1 = 0x1
Hex_B2 = 0x01
hex(int('110', 2)) = 0x06
This question already has an answer here:
Decorating Hex function to pad zeros
4 answers
python python-3.x
python python-3.x
asked Nov 12 at 17:30
Gui Luiz
1
1
marked as duplicate by Prune, Martijn Pieters♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 at 17:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Prune, Martijn Pieters♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 at 17:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
I think you mean you want leading zeros?
– Stephen Cowley
Nov 12 at 17:34
If you Google the phrase "Python convert hex to binary", you’ll find tutorials that can explain it much better than we can in an answer here.
– Prune
Nov 12 at 17:36
You are using the wrong terminology here. Hex is a base 16 numerical notation, base 2 is binary. You are instead trying to format hexadecimal to using 2 digits, padding with zero as needed.
– Martijn Pieters♦
Nov 12 at 17:37
@GuiLuiz You mean converting binary (base 2) to hex (base 16) ?
– p._phidot_
Nov 12 at 17:37
add a comment |
I think you mean you want leading zeros?
– Stephen Cowley
Nov 12 at 17:34
If you Google the phrase "Python convert hex to binary", you’ll find tutorials that can explain it much better than we can in an answer here.
– Prune
Nov 12 at 17:36
You are using the wrong terminology here. Hex is a base 16 numerical notation, base 2 is binary. You are instead trying to format hexadecimal to using 2 digits, padding with zero as needed.
– Martijn Pieters♦
Nov 12 at 17:37
@GuiLuiz You mean converting binary (base 2) to hex (base 16) ?
– p._phidot_
Nov 12 at 17:37
I think you mean you want leading zeros?
– Stephen Cowley
Nov 12 at 17:34
I think you mean you want leading zeros?
– Stephen Cowley
Nov 12 at 17:34
If you Google the phrase "Python convert hex to binary", you’ll find tutorials that can explain it much better than we can in an answer here.
– Prune
Nov 12 at 17:36
If you Google the phrase "Python convert hex to binary", you’ll find tutorials that can explain it much better than we can in an answer here.
– Prune
Nov 12 at 17:36
You are using the wrong terminology here. Hex is a base 16 numerical notation, base 2 is binary. You are instead trying to format hexadecimal to using 2 digits, padding with zero as needed.
– Martijn Pieters♦
Nov 12 at 17:37
You are using the wrong terminology here. Hex is a base 16 numerical notation, base 2 is binary. You are instead trying to format hexadecimal to using 2 digits, padding with zero as needed.
– Martijn Pieters♦
Nov 12 at 17:37
@GuiLuiz You mean converting binary (base 2) to hex (base 16) ?
– p._phidot_
Nov 12 at 17:37
@GuiLuiz You mean converting binary (base 2) to hex (base 16) ?
– p._phidot_
Nov 12 at 17:37
add a comment |
1 Answer
1
active
oldest
votes
Because hex
produces a string, you can just parse the string after conversion to hex.
leading_zeros = 2
str_hex = hex(int('110', 2))
final_hex = '0x'+str_hex[2:].zfill(leading_zeros)
As pointed out by Martijn Pieters in the comments, the much better solution is to use Pythons built in format
function.
format(5, '#04x')
1
The correct solution is to not use thehex()
function to begin with.format(value, '#04x')
would give you the correct format without string concatenation andstr.zfill()
calls.
– Martijn Pieters♦
Nov 12 at 17:39
@Martijn maybe with avalue & 0xFF
if needing to restrict - depends what the OP's really after though...
– Jon Clements♦
Nov 12 at 17:41
@MartijnPieters Much better solution (though you actually need `'#04x'). Thanks for pointing this out.
– Stephen Cowley
Nov 12 at 17:44
@Stephen: ah yes, I always forget to adjust the full width when adding#
...
– Martijn Pieters♦
Nov 12 at 17:46
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because hex
produces a string, you can just parse the string after conversion to hex.
leading_zeros = 2
str_hex = hex(int('110', 2))
final_hex = '0x'+str_hex[2:].zfill(leading_zeros)
As pointed out by Martijn Pieters in the comments, the much better solution is to use Pythons built in format
function.
format(5, '#04x')
1
The correct solution is to not use thehex()
function to begin with.format(value, '#04x')
would give you the correct format without string concatenation andstr.zfill()
calls.
– Martijn Pieters♦
Nov 12 at 17:39
@Martijn maybe with avalue & 0xFF
if needing to restrict - depends what the OP's really after though...
– Jon Clements♦
Nov 12 at 17:41
@MartijnPieters Much better solution (though you actually need `'#04x'). Thanks for pointing this out.
– Stephen Cowley
Nov 12 at 17:44
@Stephen: ah yes, I always forget to adjust the full width when adding#
...
– Martijn Pieters♦
Nov 12 at 17:46
add a comment |
Because hex
produces a string, you can just parse the string after conversion to hex.
leading_zeros = 2
str_hex = hex(int('110', 2))
final_hex = '0x'+str_hex[2:].zfill(leading_zeros)
As pointed out by Martijn Pieters in the comments, the much better solution is to use Pythons built in format
function.
format(5, '#04x')
1
The correct solution is to not use thehex()
function to begin with.format(value, '#04x')
would give you the correct format without string concatenation andstr.zfill()
calls.
– Martijn Pieters♦
Nov 12 at 17:39
@Martijn maybe with avalue & 0xFF
if needing to restrict - depends what the OP's really after though...
– Jon Clements♦
Nov 12 at 17:41
@MartijnPieters Much better solution (though you actually need `'#04x'). Thanks for pointing this out.
– Stephen Cowley
Nov 12 at 17:44
@Stephen: ah yes, I always forget to adjust the full width when adding#
...
– Martijn Pieters♦
Nov 12 at 17:46
add a comment |
Because hex
produces a string, you can just parse the string after conversion to hex.
leading_zeros = 2
str_hex = hex(int('110', 2))
final_hex = '0x'+str_hex[2:].zfill(leading_zeros)
As pointed out by Martijn Pieters in the comments, the much better solution is to use Pythons built in format
function.
format(5, '#04x')
Because hex
produces a string, you can just parse the string after conversion to hex.
leading_zeros = 2
str_hex = hex(int('110', 2))
final_hex = '0x'+str_hex[2:].zfill(leading_zeros)
As pointed out by Martijn Pieters in the comments, the much better solution is to use Pythons built in format
function.
format(5, '#04x')
edited Nov 12 at 17:43
answered Nov 12 at 17:37
Stephen Cowley
1,161418
1,161418
1
The correct solution is to not use thehex()
function to begin with.format(value, '#04x')
would give you the correct format without string concatenation andstr.zfill()
calls.
– Martijn Pieters♦
Nov 12 at 17:39
@Martijn maybe with avalue & 0xFF
if needing to restrict - depends what the OP's really after though...
– Jon Clements♦
Nov 12 at 17:41
@MartijnPieters Much better solution (though you actually need `'#04x'). Thanks for pointing this out.
– Stephen Cowley
Nov 12 at 17:44
@Stephen: ah yes, I always forget to adjust the full width when adding#
...
– Martijn Pieters♦
Nov 12 at 17:46
add a comment |
1
The correct solution is to not use thehex()
function to begin with.format(value, '#04x')
would give you the correct format without string concatenation andstr.zfill()
calls.
– Martijn Pieters♦
Nov 12 at 17:39
@Martijn maybe with avalue & 0xFF
if needing to restrict - depends what the OP's really after though...
– Jon Clements♦
Nov 12 at 17:41
@MartijnPieters Much better solution (though you actually need `'#04x'). Thanks for pointing this out.
– Stephen Cowley
Nov 12 at 17:44
@Stephen: ah yes, I always forget to adjust the full width when adding#
...
– Martijn Pieters♦
Nov 12 at 17:46
1
1
The correct solution is to not use the
hex()
function to begin with. format(value, '#04x')
would give you the correct format without string concatenation and str.zfill()
calls.– Martijn Pieters♦
Nov 12 at 17:39
The correct solution is to not use the
hex()
function to begin with. format(value, '#04x')
would give you the correct format without string concatenation and str.zfill()
calls.– Martijn Pieters♦
Nov 12 at 17:39
@Martijn maybe with a
value & 0xFF
if needing to restrict - depends what the OP's really after though...– Jon Clements♦
Nov 12 at 17:41
@Martijn maybe with a
value & 0xFF
if needing to restrict - depends what the OP's really after though...– Jon Clements♦
Nov 12 at 17:41
@MartijnPieters Much better solution (though you actually need `'#04x'). Thanks for pointing this out.
– Stephen Cowley
Nov 12 at 17:44
@MartijnPieters Much better solution (though you actually need `'#04x'). Thanks for pointing this out.
– Stephen Cowley
Nov 12 at 17:44
@Stephen: ah yes, I always forget to adjust the full width when adding
#
...– Martijn Pieters♦
Nov 12 at 17:46
@Stephen: ah yes, I always forget to adjust the full width when adding
#
...– Martijn Pieters♦
Nov 12 at 17:46
add a comment |
I think you mean you want leading zeros?
– Stephen Cowley
Nov 12 at 17:34
If you Google the phrase "Python convert hex to binary", you’ll find tutorials that can explain it much better than we can in an answer here.
– Prune
Nov 12 at 17:36
You are using the wrong terminology here. Hex is a base 16 numerical notation, base 2 is binary. You are instead trying to format hexadecimal to using 2 digits, padding with zero as needed.
– Martijn Pieters♦
Nov 12 at 17:37
@GuiLuiz You mean converting binary (base 2) to hex (base 16) ?
– p._phidot_
Nov 12 at 17:37