Converting the received data into binary form in python











up vote
0
down vote

favorite












I want to convert the received hex data into binary form. I get ValueError.
For example, I want the first value in the output to be printed as 0000.



    received_data = " ".join("{:02x}".format(byte) for byte in (data))
print(received_data)
P_data = "{0:b}".format(received_data[0:1])


Output:



    01 04 04


Error:



    Traceback (most recent call last):
File "C:UsersUsereclipse-workspaceTrytest1test2.py", line 22, in
<module>
P_data="{0:b}".format(received_data[0:1])
ValueError: Unknown format code 'b' for object of type 'str'









share|improve this question




















  • 1




    Why the intermediate hex representation? Going from from a stream of bytes to a stream of binary strings would be more straightforward.
    – NPE
    Nov 10 at 9:54










  • You're probably looking for '{0:b}'.format(byte).zfill(8) ? You said binary form not hex?
    – Torxed
    Nov 10 at 9:55










  • @NPE , hex is used for another purpose
    – Nithya
    Nov 10 at 10:04















up vote
0
down vote

favorite












I want to convert the received hex data into binary form. I get ValueError.
For example, I want the first value in the output to be printed as 0000.



    received_data = " ".join("{:02x}".format(byte) for byte in (data))
print(received_data)
P_data = "{0:b}".format(received_data[0:1])


Output:



    01 04 04


Error:



    Traceback (most recent call last):
File "C:UsersUsereclipse-workspaceTrytest1test2.py", line 22, in
<module>
P_data="{0:b}".format(received_data[0:1])
ValueError: Unknown format code 'b' for object of type 'str'









share|improve this question




















  • 1




    Why the intermediate hex representation? Going from from a stream of bytes to a stream of binary strings would be more straightforward.
    – NPE
    Nov 10 at 9:54










  • You're probably looking for '{0:b}'.format(byte).zfill(8) ? You said binary form not hex?
    – Torxed
    Nov 10 at 9:55










  • @NPE , hex is used for another purpose
    – Nithya
    Nov 10 at 10:04













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to convert the received hex data into binary form. I get ValueError.
For example, I want the first value in the output to be printed as 0000.



    received_data = " ".join("{:02x}".format(byte) for byte in (data))
print(received_data)
P_data = "{0:b}".format(received_data[0:1])


Output:



    01 04 04


Error:



    Traceback (most recent call last):
File "C:UsersUsereclipse-workspaceTrytest1test2.py", line 22, in
<module>
P_data="{0:b}".format(received_data[0:1])
ValueError: Unknown format code 'b' for object of type 'str'









share|improve this question















I want to convert the received hex data into binary form. I get ValueError.
For example, I want the first value in the output to be printed as 0000.



    received_data = " ".join("{:02x}".format(byte) for byte in (data))
print(received_data)
P_data = "{0:b}".format(received_data[0:1])


Output:



    01 04 04


Error:



    Traceback (most recent call last):
File "C:UsersUsereclipse-workspaceTrytest1test2.py", line 22, in
<module>
P_data="{0:b}".format(received_data[0:1])
ValueError: Unknown format code 'b' for object of type 'str'






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 9:50

























asked Nov 10 at 9:46









Nithya

258




258








  • 1




    Why the intermediate hex representation? Going from from a stream of bytes to a stream of binary strings would be more straightforward.
    – NPE
    Nov 10 at 9:54










  • You're probably looking for '{0:b}'.format(byte).zfill(8) ? You said binary form not hex?
    – Torxed
    Nov 10 at 9:55










  • @NPE , hex is used for another purpose
    – Nithya
    Nov 10 at 10:04














  • 1




    Why the intermediate hex representation? Going from from a stream of bytes to a stream of binary strings would be more straightforward.
    – NPE
    Nov 10 at 9:54










  • You're probably looking for '{0:b}'.format(byte).zfill(8) ? You said binary form not hex?
    – Torxed
    Nov 10 at 9:55










  • @NPE , hex is used for another purpose
    – Nithya
    Nov 10 at 10:04








1




1




Why the intermediate hex representation? Going from from a stream of bytes to a stream of binary strings would be more straightforward.
– NPE
Nov 10 at 9:54




Why the intermediate hex representation? Going from from a stream of bytes to a stream of binary strings would be more straightforward.
– NPE
Nov 10 at 9:54












You're probably looking for '{0:b}'.format(byte).zfill(8) ? You said binary form not hex?
– Torxed
Nov 10 at 9:55




You're probably looking for '{0:b}'.format(byte).zfill(8) ? You said binary form not hex?
– Torxed
Nov 10 at 9:55












@NPE , hex is used for another purpose
– Nithya
Nov 10 at 10:04




@NPE , hex is used for another purpose
– Nithya
Nov 10 at 10:04












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You should first convert your string into an integer



P_data = '{0:b}'.format(int(received_data[0:1], 16)).zfill(4)





share|improve this answer























  • If I receive hex in alphabets, it shows some error ValueError: invalid literal for int() with base 10: 'e'
    – Nithya
    Nov 10 at 10:14










  • Effectively, you should use base 16 instead (as it is hexadecimal), I edited my answer.
    – mistiru
    Nov 10 at 10:18











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%2f53237743%2fconverting-the-received-data-into-binary-form-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You should first convert your string into an integer



P_data = '{0:b}'.format(int(received_data[0:1], 16)).zfill(4)





share|improve this answer























  • If I receive hex in alphabets, it shows some error ValueError: invalid literal for int() with base 10: 'e'
    – Nithya
    Nov 10 at 10:14










  • Effectively, you should use base 16 instead (as it is hexadecimal), I edited my answer.
    – mistiru
    Nov 10 at 10:18















up vote
1
down vote



accepted










You should first convert your string into an integer



P_data = '{0:b}'.format(int(received_data[0:1], 16)).zfill(4)





share|improve this answer























  • If I receive hex in alphabets, it shows some error ValueError: invalid literal for int() with base 10: 'e'
    – Nithya
    Nov 10 at 10:14










  • Effectively, you should use base 16 instead (as it is hexadecimal), I edited my answer.
    – mistiru
    Nov 10 at 10:18













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You should first convert your string into an integer



P_data = '{0:b}'.format(int(received_data[0:1], 16)).zfill(4)





share|improve this answer














You should first convert your string into an integer



P_data = '{0:b}'.format(int(received_data[0:1], 16)).zfill(4)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 10:18

























answered Nov 10 at 9:59









mistiru

40012




40012












  • If I receive hex in alphabets, it shows some error ValueError: invalid literal for int() with base 10: 'e'
    – Nithya
    Nov 10 at 10:14










  • Effectively, you should use base 16 instead (as it is hexadecimal), I edited my answer.
    – mistiru
    Nov 10 at 10:18


















  • If I receive hex in alphabets, it shows some error ValueError: invalid literal for int() with base 10: 'e'
    – Nithya
    Nov 10 at 10:14










  • Effectively, you should use base 16 instead (as it is hexadecimal), I edited my answer.
    – mistiru
    Nov 10 at 10:18
















If I receive hex in alphabets, it shows some error ValueError: invalid literal for int() with base 10: 'e'
– Nithya
Nov 10 at 10:14




If I receive hex in alphabets, it shows some error ValueError: invalid literal for int() with base 10: 'e'
– Nithya
Nov 10 at 10:14












Effectively, you should use base 16 instead (as it is hexadecimal), I edited my answer.
– mistiru
Nov 10 at 10:18




Effectively, you should use base 16 instead (as it is hexadecimal), I edited my answer.
– mistiru
Nov 10 at 10:18


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53237743%2fconverting-the-received-data-into-binary-form-in-python%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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini