Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
binary integer confusion
#1
Hi all.

Im receving a number through serial.

b'34\n'
I do this to remove the end of line

data=data.rstrip()
Then I do this to remove the b'

data=data.decode('utf-8')
Then I try to use what seems to be an integer but I get an error.

Is something evidently wrong?
Reply
#2
It would help to see the code that's causing the error and what exactly the error is. Remember that we can't see your screen.

Note that "removing the b'" is really the wrong terminology - decode converts a bytes object into a string.

Given the above, are you actually converting the string into an integer before using it?
Reply
#3
(Dec-25-2020, 06:01 PM)ndc85430 Wrote: It would help to see the code that's causing the error and what exactly the error is. Remember that we can't see your screen.

Note that "removing the b'" is really the wrong terminology - decode converts a bytes object into a string.

Given the above, are you actually converting the string into an integer before using it?

Thanks for the answer.

I did not convert the string to integer. I thought decode converted to integer.

Can I converte directly from bytes to int?
Reply
#4
Per the docs, int does accept bytes.
Reply
#5
(Dec-25-2020, 07:31 PM)ndc85430 Wrote: Per the docs, int does accept bytes.

Doesnt seem to be it.

Im trying to light a led on a leds panel, using x and y coordinates

if I do this:

While True:

    data=ser.read()
    trellis.color(3,5, RED)  # these are the x and y coordinates
It works.
But if I do this:

While True:
     
     data=ser.read()
  
     trellis.color(data,5, RED)
I get

Error:
File "this.py", line 102, in <moduel> trellis.color(data,3,RED) File "/usr/local/lib/python3.7/dist-packages/adafruit_neotrellis/multitrellis.py", line 71, in color xkey =x%4 TypeError: not all arguments converted during bytes formatting
Reply
#6
I can print the serial read with no problem.
I get

b'4'
Reply
#7
So data is a bytes object? You need to convert it to an int then. Again, the int function accepts bytes as the documentation says.
Reply
#8
(Dec-25-2020, 10:01 PM)ndc85430 Wrote: So data is a bytes object? You need to convert it to an int then. Again, the int function accepts bytes as the documentation says.


Data is sent from PC (max/msp) via Serial. Either as an integer or ascii (with same results)

I try:

integer=int.from_bytes(data,¨little¨)
and

integer=int.from_bytes(data, "big")
either way a get

255 or 254
Reply
#9
It's not immediately obviously why from_bytes doesn't do what you expect (but my excuse is that it's early and I'm not fully awake!). What about just calling int directly?
Reply
#10
(Dec-26-2020, 05:58 AM)ndc85430 Wrote: It's not immediately obviously why from_bytes doesn't do what you expect (but my excuse is that it's early and I'm not fully awake!). What about just calling int directly?

Sorry. IT WORKS!!

My bad. I had the serial ports with different baud rates.

Thank you !!

Hope you had a good sleep
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hex file to binary or pcap to binary baran01 1 5,702 Dec-11-2019, 10:19 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020