unpack syntax in python 3
up vote
2
down vote
favorite
I am trying to convert hex numbers into decimals using unpack.
When I use:
from struct import *
unpack("<H",b"xe2x07")
The output is: 2018, which is what I want.
The thing is I have my hex data in a list as a string in the form of:
asd = ['e2','07']
My question is is there a simple way of using unpack without the backslashes, the x? Something like so:
unpack("<H","e207")
I know this doesn't work but I hope you get the idea.
For clarification I know I could get the data in the form of b'x11' in the list but then it's interpreted as ASCII, which I don't want, that's why I have it in the format I showed.
python-3.x unpack
add a comment |
up vote
2
down vote
favorite
I am trying to convert hex numbers into decimals using unpack.
When I use:
from struct import *
unpack("<H",b"xe2x07")
The output is: 2018, which is what I want.
The thing is I have my hex data in a list as a string in the form of:
asd = ['e2','07']
My question is is there a simple way of using unpack without the backslashes, the x? Something like so:
unpack("<H","e207")
I know this doesn't work but I hope you get the idea.
For clarification I know I could get the data in the form of b'x11' in the list but then it's interpreted as ASCII, which I don't want, that's why I have it in the format I showed.
python-3.x unpack
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am trying to convert hex numbers into decimals using unpack.
When I use:
from struct import *
unpack("<H",b"xe2x07")
The output is: 2018, which is what I want.
The thing is I have my hex data in a list as a string in the form of:
asd = ['e2','07']
My question is is there a simple way of using unpack without the backslashes, the x? Something like so:
unpack("<H","e207")
I know this doesn't work but I hope you get the idea.
For clarification I know I could get the data in the form of b'x11' in the list but then it's interpreted as ASCII, which I don't want, that's why I have it in the format I showed.
python-3.x unpack
I am trying to convert hex numbers into decimals using unpack.
When I use:
from struct import *
unpack("<H",b"xe2x07")
The output is: 2018, which is what I want.
The thing is I have my hex data in a list as a string in the form of:
asd = ['e2','07']
My question is is there a simple way of using unpack without the backslashes, the x? Something like so:
unpack("<H","e207")
I know this doesn't work but I hope you get the idea.
For clarification I know I could get the data in the form of b'x11' in the list but then it's interpreted as ASCII, which I don't want, that's why I have it in the format I showed.
python-3.x unpack
python-3.x unpack
edited Nov 19 at 15:16
Mihai Chelaru
2,0927922
2,0927922
asked Nov 19 at 15:10
James
476
476
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You have hex-encoded data, in a text object. So, to go back to raw hex bytes, you can decode the text string. Please note that this is not the usual convention in Python 3.x (generally, text strings are already decoded).
>>> codecs.decode('e207', 'hex')
b'xe2x07'
A convenience function for the same thing:
>>> bytes.fromhex('e207')
b'xe2x07'
Now you can struct.unpack those bytes. Putting it all together:
>>> asd = ['e2','07']
>>> text = ''.join(asd)
>>> encoded = codecs.decode(text, 'hex')
>>> struct.unpack("<H", encoded)
(2018,)
will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
– James
Nov 19 at 15:27
I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
– wim
Nov 19 at 15:33
what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
– James
Nov 20 at 12:40
As long as they are all 0-9 a-f then it doesn't matter.
– wim
Nov 20 at 14:57
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You have hex-encoded data, in a text object. So, to go back to raw hex bytes, you can decode the text string. Please note that this is not the usual convention in Python 3.x (generally, text strings are already decoded).
>>> codecs.decode('e207', 'hex')
b'xe2x07'
A convenience function for the same thing:
>>> bytes.fromhex('e207')
b'xe2x07'
Now you can struct.unpack those bytes. Putting it all together:
>>> asd = ['e2','07']
>>> text = ''.join(asd)
>>> encoded = codecs.decode(text, 'hex')
>>> struct.unpack("<H", encoded)
(2018,)
will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
– James
Nov 19 at 15:27
I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
– wim
Nov 19 at 15:33
what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
– James
Nov 20 at 12:40
As long as they are all 0-9 a-f then it doesn't matter.
– wim
Nov 20 at 14:57
add a comment |
up vote
1
down vote
You have hex-encoded data, in a text object. So, to go back to raw hex bytes, you can decode the text string. Please note that this is not the usual convention in Python 3.x (generally, text strings are already decoded).
>>> codecs.decode('e207', 'hex')
b'xe2x07'
A convenience function for the same thing:
>>> bytes.fromhex('e207')
b'xe2x07'
Now you can struct.unpack those bytes. Putting it all together:
>>> asd = ['e2','07']
>>> text = ''.join(asd)
>>> encoded = codecs.decode(text, 'hex')
>>> struct.unpack("<H", encoded)
(2018,)
will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
– James
Nov 19 at 15:27
I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
– wim
Nov 19 at 15:33
what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
– James
Nov 20 at 12:40
As long as they are all 0-9 a-f then it doesn't matter.
– wim
Nov 20 at 14:57
add a comment |
up vote
1
down vote
up vote
1
down vote
You have hex-encoded data, in a text object. So, to go back to raw hex bytes, you can decode the text string. Please note that this is not the usual convention in Python 3.x (generally, text strings are already decoded).
>>> codecs.decode('e207', 'hex')
b'xe2x07'
A convenience function for the same thing:
>>> bytes.fromhex('e207')
b'xe2x07'
Now you can struct.unpack those bytes. Putting it all together:
>>> asd = ['e2','07']
>>> text = ''.join(asd)
>>> encoded = codecs.decode(text, 'hex')
>>> struct.unpack("<H", encoded)
(2018,)
You have hex-encoded data, in a text object. So, to go back to raw hex bytes, you can decode the text string. Please note that this is not the usual convention in Python 3.x (generally, text strings are already decoded).
>>> codecs.decode('e207', 'hex')
b'xe2x07'
A convenience function for the same thing:
>>> bytes.fromhex('e207')
b'xe2x07'
Now you can struct.unpack those bytes. Putting it all together:
>>> asd = ['e2','07']
>>> text = ''.join(asd)
>>> encoded = codecs.decode(text, 'hex')
>>> struct.unpack("<H", encoded)
(2018,)
edited Nov 19 at 16:57
answered Nov 19 at 15:21
wim
154k47292423
154k47292423
will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
– James
Nov 19 at 15:27
I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
– wim
Nov 19 at 15:33
what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
– James
Nov 20 at 12:40
As long as they are all 0-9 a-f then it doesn't matter.
– wim
Nov 20 at 14:57
add a comment |
will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
– James
Nov 19 at 15:27
I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
– wim
Nov 19 at 15:33
what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
– James
Nov 20 at 12:40
As long as they are all 0-9 a-f then it doesn't matter.
– wim
Nov 20 at 14:57
will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
– James
Nov 19 at 15:27
will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
– James
Nov 19 at 15:27
I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
– wim
Nov 19 at 15:33
I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
– wim
Nov 19 at 15:33
what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
– James
Nov 20 at 12:40
what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
– James
Nov 20 at 12:40
As long as they are all 0-9 a-f then it doesn't matter.
– wim
Nov 20 at 14:57
As long as they are all 0-9 a-f then it doesn't matter.
– wim
Nov 20 at 14:57
add a comment |
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%2f53377529%2funpack-syntax-in-python-3%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