Python Forum
How to convert 4 bytes to an integer ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert 4 bytes to an integer ?
#3
(Jan-17-2022, 12:06 AM)bowlofred Wrote: Do you have an example of the bytes that you receive and know the specific integer that it should form?

Thanks for the reply. Yes, I need to convert it to little endian. On the slave side, I have for ex. 4094 as a 32bit integer. I convert it to 4 8-bit integers as below and then send it over to RP:
          b1 = (unsigned int)(data_adc & 0xff); //data_adc is 32 bit integer
          b2 = (unsigned int)(data_adc >> 8) & 0xff;
          b3 = (unsigned int)(data_adc >> 16) & 0xff;
          b4 = (unsigned int)(data_adc >> 24);

          int testarray[4]={b1,b2,b3,b4};

          for (int i = 0; i < 4; i++){
                        USART_SpiTransfer(USART0, testarray[i]); 
          }
So basically, when this data_adc is always 4094, the slave always sends [254,15,0,0] (checked using debugger on slave side). So, I should receive the same on Raspberry Pi.

In fact when I change the print statement to:
print (b)


I get this:
.
.
[15L, 224L, 0L, 0L]
[0L, 0L, 253L, 224L]
.
.
And these 2 lines are repeating instead of only repeating [254,15,0,0]. Any suggestion, what I might be doing wrong here?
Reply


Messages In This Thread
How to convert 4 bytes to an integer ? - by GiggsB - Jan-16-2022, 11:53 PM
RE: How to convert 4 bytes to an integer ? - by GiggsB - Jan-17-2022, 12:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with implementation of the logic to convert roman numeral to integer jagasrik 2 2,391 Aug-14-2020, 07:31 PM
Last Post: deanhystad
  Pack integer values as single bytes in a struct bhdschmidt 3 2,486 Jun-09-2020, 09:23 PM
Last Post: bhdschmidt
  convert from csv row to 16 bit bytes adetheheat 1 1,706 Feb-28-2020, 03:04 PM
Last Post: Larz60+
  8-bit integer to bytes Garitron 1 3,119 Sep-14-2019, 11:22 PM
Last Post: ichabod801
  replace bytes with other byte or bytes BigOldArt 1 10,830 Feb-02-2019, 11:00 PM
Last Post: snippsat
Question [Help] Convert integer to Roman numerals? {Screenshot attached} vanicci 10 9,457 Aug-06-2018, 05:19 PM
Last Post: vanicci
  Convert integer to string Maigol 5 5,668 Mar-05-2018, 10:25 PM
Last Post: python_master89
  cannot convert float infinity to integer error despite rounding off floats Afterdarkreader 3 15,374 Dec-15-2017, 02:01 AM
Last Post: micseydel
  How to convert character to integer in python rajeev1729 2 3,969 Oct-03-2017, 04:30 PM
Last Post: nilamo
  Logic to convert an integer to YEAR / MONTH hugobaur 9 8,141 Oct-18-2016, 11:58 AM
Last Post: hugobaur

Forum Jump:

User Panel Messages

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