Encoding in 8 Bit Integer to Text in Python 3Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Converting integer to string?Does Python have a string 'contains' substring method?
May a hotel provide accommodation for fewer people than booked?
Can living where magnetic ore is abundant provide any protection from cosmic radiation?
"DDoouubbllee ssppeeaakk!!"
Went to a big 4 but got fired for underperformance in a year recently - Now every one thinks I'm pro - How to balance expectations?
Were there any unmanned expeditions to the moon that returned to Earth prior to Apollo?
IBM mainframe classic executable file formats
How to derive trigonometric Cartesian equation from parametric
Is there a general term for the items in a directory?
Should students have access to past exams or an exam bank?
Can I say "Gesundheit" if someone is coughing?
Why is “deal 6 damage” a legit phrase?
Why do MS SQL Server SEQUENCEs not have an ORDER parameter like Oracle?
A game of red and black
Why did the United States not resort to nuclear weapons in Vietnam?
Gold Battle KoTH
Adjective for when skills are not improving and I'm depressed about it
Why should I use a big powerstone instead of smaller ones?
"Will flex for food". What does this phrase mean?
Error with uppercase in titlesec's label field
How to compare files with diffrent extensions and delete extra files?
Accurately recalling the key - can everyone do it?
How did Biff return to 2015 from 1955 without a lightning strike?
Feedback diagram
How do I respond appropriately to an overseas company that obtained a visa for me without hiring me?
Encoding in 8 Bit Integer to Text in Python 3
Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Converting integer to string?Does Python have a string 'contains' substring method?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I updating a script to Python 3 from Python 2 and having trouble with the line below, it decodes and displays the RSSI value of a BLE Beacon:
rssi = struct.unpack("b", packet[packetOffset -1])
I found an alternative method to get the same result, which takes the 2 last characters in the broadcast string and converts them to text. Using an 8 Bit Signed Integer Encoder.
For example c3
becomes -61
(https://cryptii.com/pipes/integer-encoder)
However I am struggling to find a way to do this in python 3!
How do I decode the string?
Any help will be much appreciated.
python python-3.x bluetooth bluetooth-lowenergy
add a comment |
I updating a script to Python 3 from Python 2 and having trouble with the line below, it decodes and displays the RSSI value of a BLE Beacon:
rssi = struct.unpack("b", packet[packetOffset -1])
I found an alternative method to get the same result, which takes the 2 last characters in the broadcast string and converts them to text. Using an 8 Bit Signed Integer Encoder.
For example c3
becomes -61
(https://cryptii.com/pipes/integer-encoder)
However I am struggling to find a way to do this in python 3!
How do I decode the string?
Any help will be much appreciated.
python python-3.x bluetooth bluetooth-lowenergy
1
Can you give us tha string?
– Pedro Rodrigues
Mar 26 at 23:33
Did you notice thatint('c3', 16)
->195
and accidentally195 - 256 = -61
?
– Klaus D.
Mar 26 at 23:34
add a comment |
I updating a script to Python 3 from Python 2 and having trouble with the line below, it decodes and displays the RSSI value of a BLE Beacon:
rssi = struct.unpack("b", packet[packetOffset -1])
I found an alternative method to get the same result, which takes the 2 last characters in the broadcast string and converts them to text. Using an 8 Bit Signed Integer Encoder.
For example c3
becomes -61
(https://cryptii.com/pipes/integer-encoder)
However I am struggling to find a way to do this in python 3!
How do I decode the string?
Any help will be much appreciated.
python python-3.x bluetooth bluetooth-lowenergy
I updating a script to Python 3 from Python 2 and having trouble with the line below, it decodes and displays the RSSI value of a BLE Beacon:
rssi = struct.unpack("b", packet[packetOffset -1])
I found an alternative method to get the same result, which takes the 2 last characters in the broadcast string and converts them to text. Using an 8 Bit Signed Integer Encoder.
For example c3
becomes -61
(https://cryptii.com/pipes/integer-encoder)
However I am struggling to find a way to do this in python 3!
How do I decode the string?
Any help will be much appreciated.
python python-3.x bluetooth bluetooth-lowenergy
python python-3.x bluetooth bluetooth-lowenergy
asked Mar 26 at 23:27
tbowdentbowden
3922 silver badges18 bronze badges
3922 silver badges18 bronze badges
1
Can you give us tha string?
– Pedro Rodrigues
Mar 26 at 23:33
Did you notice thatint('c3', 16)
->195
and accidentally195 - 256 = -61
?
– Klaus D.
Mar 26 at 23:34
add a comment |
1
Can you give us tha string?
– Pedro Rodrigues
Mar 26 at 23:33
Did you notice thatint('c3', 16)
->195
and accidentally195 - 256 = -61
?
– Klaus D.
Mar 26 at 23:34
1
1
Can you give us tha string?
– Pedro Rodrigues
Mar 26 at 23:33
Can you give us tha string?
– Pedro Rodrigues
Mar 26 at 23:33
Did you notice that
int('c3', 16)
-> 195
and accidentally 195 - 256 = -61
?– Klaus D.
Mar 26 at 23:34
Did you notice that
int('c3', 16)
-> 195
and accidentally 195 - 256 = -61
?– Klaus D.
Mar 26 at 23:34
add a comment |
2 Answers
2
active
oldest
votes
Is it what you are looking for?
a = bytes.fromhex('c3')
res = a[0] - 256 if a[0] > 127 else a[0]
bytes.fromhex
takes a string and transforms it to a byte
object. You take the first byte and make it unsigned by checking whether it's greater than 127.
Alternatively:
res = int.from_bytes(bytes.fromhex('c3'), byteorder='big', signed=True)
Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?
– tbowden
Mar 26 at 23:53
There is. If you tryint('0f', 16) - 256
you get-241
. You need to check whether the value is >127 or not. Usingint
is a good idea. Not sure of the consequences. I need to think about it.
– Jacques Gaudin
Mar 26 at 23:57
add a comment |
You haven't shown exactly what's in packet
, but here's a guess:
import struct
packet = b'x00xc3x00xff'
packetOffset = 2
rssi = struct.unpack("b", packet[packetOffset-1: packetOffset])[0]
print(repr(rssi)) # -> -61
# For a text string result, just do:
rssi = str(rssi)
print(repr(rssi)) # -> '-61'
add a 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%2f55367633%2fencoding-in-8-bit-integer-to-text-in-python-3%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
Is it what you are looking for?
a = bytes.fromhex('c3')
res = a[0] - 256 if a[0] > 127 else a[0]
bytes.fromhex
takes a string and transforms it to a byte
object. You take the first byte and make it unsigned by checking whether it's greater than 127.
Alternatively:
res = int.from_bytes(bytes.fromhex('c3'), byteorder='big', signed=True)
Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?
– tbowden
Mar 26 at 23:53
There is. If you tryint('0f', 16) - 256
you get-241
. You need to check whether the value is >127 or not. Usingint
is a good idea. Not sure of the consequences. I need to think about it.
– Jacques Gaudin
Mar 26 at 23:57
add a comment |
Is it what you are looking for?
a = bytes.fromhex('c3')
res = a[0] - 256 if a[0] > 127 else a[0]
bytes.fromhex
takes a string and transforms it to a byte
object. You take the first byte and make it unsigned by checking whether it's greater than 127.
Alternatively:
res = int.from_bytes(bytes.fromhex('c3'), byteorder='big', signed=True)
Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?
– tbowden
Mar 26 at 23:53
There is. If you tryint('0f', 16) - 256
you get-241
. You need to check whether the value is >127 or not. Usingint
is a good idea. Not sure of the consequences. I need to think about it.
– Jacques Gaudin
Mar 26 at 23:57
add a comment |
Is it what you are looking for?
a = bytes.fromhex('c3')
res = a[0] - 256 if a[0] > 127 else a[0]
bytes.fromhex
takes a string and transforms it to a byte
object. You take the first byte and make it unsigned by checking whether it's greater than 127.
Alternatively:
res = int.from_bytes(bytes.fromhex('c3'), byteorder='big', signed=True)
Is it what you are looking for?
a = bytes.fromhex('c3')
res = a[0] - 256 if a[0] > 127 else a[0]
bytes.fromhex
takes a string and transforms it to a byte
object. You take the first byte and make it unsigned by checking whether it's greater than 127.
Alternatively:
res = int.from_bytes(bytes.fromhex('c3'), byteorder='big', signed=True)
edited Mar 27 at 0:21
answered Mar 26 at 23:49
Jacques GaudinJacques Gaudin
7,6883 gold badges28 silver badges51 bronze badges
7,6883 gold badges28 silver badges51 bronze badges
Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?
– tbowden
Mar 26 at 23:53
There is. If you tryint('0f', 16) - 256
you get-241
. You need to check whether the value is >127 or not. Usingint
is a good idea. Not sure of the consequences. I need to think about it.
– Jacques Gaudin
Mar 26 at 23:57
add a comment |
Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?
– tbowden
Mar 26 at 23:53
There is. If you tryint('0f', 16) - 256
you get-241
. You need to check whether the value is >127 or not. Usingint
is a good idea. Not sure of the consequences. I need to think about it.
– Jacques Gaudin
Mar 26 at 23:57
Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?
– tbowden
Mar 26 at 23:53
Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?
– tbowden
Mar 26 at 23:53
There is. If you try
int('0f', 16) - 256
you get -241
. You need to check whether the value is >127 or not. Using int
is a good idea. Not sure of the consequences. I need to think about it.– Jacques Gaudin
Mar 26 at 23:57
There is. If you try
int('0f', 16) - 256
you get -241
. You need to check whether the value is >127 or not. Using int
is a good idea. Not sure of the consequences. I need to think about it.– Jacques Gaudin
Mar 26 at 23:57
add a comment |
You haven't shown exactly what's in packet
, but here's a guess:
import struct
packet = b'x00xc3x00xff'
packetOffset = 2
rssi = struct.unpack("b", packet[packetOffset-1: packetOffset])[0]
print(repr(rssi)) # -> -61
# For a text string result, just do:
rssi = str(rssi)
print(repr(rssi)) # -> '-61'
add a comment |
You haven't shown exactly what's in packet
, but here's a guess:
import struct
packet = b'x00xc3x00xff'
packetOffset = 2
rssi = struct.unpack("b", packet[packetOffset-1: packetOffset])[0]
print(repr(rssi)) # -> -61
# For a text string result, just do:
rssi = str(rssi)
print(repr(rssi)) # -> '-61'
add a comment |
You haven't shown exactly what's in packet
, but here's a guess:
import struct
packet = b'x00xc3x00xff'
packetOffset = 2
rssi = struct.unpack("b", packet[packetOffset-1: packetOffset])[0]
print(repr(rssi)) # -> -61
# For a text string result, just do:
rssi = str(rssi)
print(repr(rssi)) # -> '-61'
You haven't shown exactly what's in packet
, but here's a guess:
import struct
packet = b'x00xc3x00xff'
packetOffset = 2
rssi = struct.unpack("b", packet[packetOffset-1: packetOffset])[0]
print(repr(rssi)) # -> -61
# For a text string result, just do:
rssi = str(rssi)
print(repr(rssi)) # -> '-61'
edited Mar 27 at 0:07
answered Mar 27 at 0:01
martineaumartineau
74.3k11 gold badges101 silver badges193 bronze badges
74.3k11 gold badges101 silver badges193 bronze badges
add a comment |
add a 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%2f55367633%2fencoding-in-8-bit-integer-to-text-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
1
Can you give us tha string?
– Pedro Rodrigues
Mar 26 at 23:33
Did you notice that
int('c3', 16)
->195
and accidentally195 - 256 = -61
?– Klaus D.
Mar 26 at 23:34