Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 14:08 05 May 2026 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Convert small 64bit signed integer into a high and low byte ?

     Page 2 of 2    
Author Message
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4309
Posted: 02:30pm 01 May 2026
Copy link to clipboard 
Print this post

  DigitalDreams said  
  matherp said  bin2str$ was designed for specifically this


Hi Peter

Worked well in that case yet I face a new challenge. Reading temperature and pressure data in 24-bit signed format. Three consecutive bytes (or 6 consevutive bytes for both). Bin2str$ and it's counterpart have no 24-bit options. It seems I have to do this conversion manually....

You can use 32-bit with some hassle.

Are they signed or unsigned 24-bit?

John
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 3186
Posted: 09:41pm 01 May 2026
Copy link to clipboard 
Print this post

  DigitalDreams said  ... data in 24-bit signed format.
 
mozzie
Senior Member

Joined: 15/06/2020
Location: Australia
Posts: 289
Posted: 06:11am 02 May 2026
Copy link to clipboard 
Print this post

G'day John (DigitalDreams)
Had a similar problem with a sensor a while ago, there are probably better solutions but the one I used was:
Option Default Integer

a$="   " ' Set string length to 3 (24 bit)
byte(a$,1)=0:byte(a$,2)=0:byte(a$,3)=0 ' Set default value

a$=chr$(0)+a$ ' make 24 bit value 32 bit
b=str2bin(int32,a$)/256 ' correct to 24 bit value


If the data is big endian the extra chr$(0) will need to be added to the end:
a$=a$+chr$(0) ' make 24 bit value 32 bit
b=str2bin(int32,a$,BIG)/256 ' correct to 24 bit value


Hope this helps.

Regards,
Lyle.
Edited 2026-05-02 16:19 by mozzie
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4309
Posted: 09:22am 02 May 2026
Copy link to clipboard 
Print this post

Looks like unsigned.

John
 
DigitalDreams
Regular Member

Joined: 03/05/2025
Location: United Kingdom
Posts: 65
Posted: 05:56pm 02 May 2026
Copy link to clipboard 
Print this post

Data read into an array (all signed as mentioned in first post). Pressure is virtually always positive so....
press=pdat(3)+pdat(4)*256+pdat(5)*65536

Temperature I've handled the same for now but will be testing for a 2's compliment negative bit and doing the necessary fiddle.
Edited 2026-05-03 03:57 by DigitalDreams
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 3186
Posted: 09:51pm 02 May 2026
Copy link to clipboard 
Print this post

As 24 bit ADC values greatly exceed the accuracy of the sensors, perhaps try leaving out the least significant bytes as they will be mostly noise.

16 bits will still give greater resolution than the sensor accuracy but might be processed more easily with Bin2str$.
Eg. press = pdat(4) + (pdat(5)<<8) '16 bit

I did a similar thing with the AHT10 temperature & humidity sensor which has 20 bits for each using 5 bytes, with the middle byte having 4 bits of each.
Using 12 bits for temp. and 8 for humidity gave the same results with less code.

  AHT10 datasheet said   Humidity accuracy: typical ± 2%
 Humidity resolution: 0.024%
 Temperature accuracy: typical ± 0.3 ° C
 Temperature resolution: Typical 0.01 °C


Edit.
Another possibility is that all the data is in the lower bytes and the high byte is all 0s (or 1s for negative values). If so ignore the high byte.

What sensor are you using? I will have a look at the datasheet to see what is easiest.
Edited 2026-05-03 11:30 by phil99
 
DigitalDreams
Regular Member

Joined: 03/05/2025
Location: United Kingdom
Posts: 65
Posted: 12:53pm 03 May 2026
Copy link to clipboard 
Print this post

  phil99 said  As 24 bit ADC values greatly exceed the accuracy of the sensors, perhaps try leaving out the least significant bytes as they will be mostly noise.

What sensor are you using? I will have a look at the datasheet to see what is easiest.


Keep forgetting the << shift function, must do a speed comparison...

Bosch BMP581. Early days yet loving the full pressure resolution oversampled x64 so wont be ignoring the lower byte. Impressing folk when turning pressure into tone and hearing it smoothly change as my Picocalc is simply raised from floor to ceiling 😎.
 
mozzie
Senior Member

Joined: 15/06/2020
Location: Australia
Posts: 289
Posted: 02:29pm 03 May 2026
Copy link to clipboard 
Print this post

G'day,
Thats a very interesting sensor, thanks for pointing it out.

Should have added that this and the code posted previously work with signed integers.

With a bit of luck the following should be all that's needed to read the BMP581 (not sure about byte order, assumes low byte first)

Option Default Integer

I2C read addr,0,6,I2Cdat$

BMP_prss = str2bin(int32,chr$(0)+right$(I2Cdat$,3))/256
BMP_temp = str2bin(int32,chr$(0)+left$(I2Cdat$,3))/256


Regards,
Lyle.
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2026