Menu
JAQForum Ver 19.10.27

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

Posted: 05:18pm
14 Apr 2026
Copy link to clipboard
DigitalDreams
Regular Member

My next challenge is to save signed integers to an I2c memory chip. They will only range from say -300 to +900 so will be saving as two bytes (high and low) as if a 16bit integer, not the incoming MMBasic 64bit.

For example -

Dim as integer Xmin,Dhi,Dlo
Xmin=-298
...
...
I2c write &h50,0,4,1,0,Dhi,Dlo

I'm already writing small positive MMBasic 64bit integers as single bytes as MMBasic converts/truncates to 8bit in the i2c command itself.

Wondering what the fastest and most efficient way would be to do this ?
 
Posted: 05:23pm
14 Apr 2026
Copy link to clipboard
matherp
Guru

bin2str$ was designed for specifically this
Edited 2026-04-15 03:23 by matherp
 
Posted: 05:37pm
14 Apr 2026
Copy link to clipboard
DigitalDreams
Regular Member

  matherp said  bin2str$ was designed for specifically this


I'll have a play sir....
 
Posted: 09:58pm
14 Apr 2026
Copy link to clipboard
bfwolf
Senior Member

A kind of "hack"  according to C, which relies on the Pico using an ARM with a little-endian format:

Dim XminAddr As integer
XminAddr = PEEK(VARADDR Xmin)
Dlo = PEEK(BYTE XminAddr)
Dhi = PEEK(BYTE (XminAddr+1))
 
Posted: 10:02pm
14 Apr 2026
Copy link to clipboard
DigitalDreams
Regular Member

  matherp said  bin2str$ was designed for specifically this


First attempt results in "Error : Argument Count"

a$=Bin2Str$(INT16,xmin,BIG)
I2C2 write &h50,0,4,1,0,a$

Hence writing 4 bytes... '1','0','2byte string' ??
 
Posted: 10:04pm
14 Apr 2026
Copy link to clipboard
DigitalDreams
Regular Member

  bfwolf said  A kind of "hack"  according to C, which relies on the Pico using an ARM with a little-endian format:

Dim XminAddr As integer
XminAddr = PEEK(VARADDR Xmin)
Dlo = PEEK(BYTE XminAddr)
Dhi = PEEK(BYTE (XminAddr+1))


Interesting 😎
 
Posted: 11:33pm
14 Apr 2026
Copy link to clipboard
phil99
Guru


  Quote  First attempt results in "Error : Argument Count"

a$=Bin2Str$(INT16,xmin,BIG)
I2C2 write &h50,0,4,1,0,a$

When writing a string to a 24Cxx EEPROM the 2 address bytes need to be added to the start of the data.
So perhaps try:-
a$ = Chr$(Mem.addr>>8) + Chr$(Mem.addr And 255) + a$ 'prepend 2 byte memory address to the data
I2C2 WRITE I2C.addr, 0, Len(a$), a$   'write 2 memory address bytes + up to 32 data bytes
Pause 6  'allow time to write


Currently you are trying to write a mix of integer bytes and a string which I2C WRITE doesn't accept.
Edited 2026-04-15 11:04 by phil99
 


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