Saving a 64-bit intgeger

22 views
Skip to first unread message

pvr...@btinternet.com

unread,
Apr 16, 2025, 5:15:01 PM4/16/25
to fltk.general
What's the best way to store a 64-bit integer in Fl_Preferences?

Phil Rose

Matthias Melcher

unread,
Apr 17, 2025, 11:06:59 AM4/17/25
to fltk.general
There is no direct call to save 64 bit values, but I will happily add that in 1.5. You can work around this using sprintf to create a numeric string and save the string. This will be compatible with a future implementation. You can retrieve the value into a text buffer and use sscanf to convert it back into binary.

As an alternative,  use the binary set and get of Fl_Preferences to directly write the data. Note that the byte order my be different on other machines, so if you want to make the preferences file portable, you need to call `htonl`, for example, before and after setting and getting you value.



pvr...@btinternet.com

unread,
Apr 17, 2025, 11:53:53 AM4/17/25
to fltk.general



From: 'Matthias Melcher' via fltk.general <fltkg...@googlegroups.com>
Sent: Thursday, April 17, 2025 4:06 PM
To: fltk.general <fltkg...@googlegroups.com>
Subject: [fltk.general] Re: Saving a 64-bit intgeger
 
There is no direct call to save 64 bit values, but I will happily add that in 1.5. You can work around this using sprintf to create a numeric string and save the string. This will be compatible with a future implementation. You can retrieve the value into a text buffer and use sscanf to convert it back into binary.

As an alternative,  use the binary set and get of Fl_Preferences to directly write the data. Note that the byte order my be different on other machines, so if you want to make the preferences file portable, you need to call `htonl`, for example, before and after setting and getting you value.

Thanks Matthias, That's what I ended up doing. I found those versions of get/set when I was trying to save and recover encrypted text. As this would very easily be invalid UTF-8 characters, I needed some way of storing as binary. The only catch was that I already had the entry in the .prefs file as an empty string and that confused me as it looked like the get seemed to produce random data. After stepping through the Fl_Preferences::get code I saw what it was doing, and deleting the existing entries from the .prefs made the problem go away.

Thanks for the warning on byte order. That confused me for a while when I saw 1024 saved as 0004000000000000.  I would like the code to be portable, but I am not too concerned about copying the .prefs file, as I have other differences between the  usages on W11 and Debian. So as long as I get reproducible behaviour on one system, I'm happy.  


Phil.



pvr...@btinternet.com schrieb am Mittwoch, 16. April 2025 um 23:15:01 UTC+2:
What's the best way to store a 64-bit integer in Fl_Preferences?

Phil Rose
--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/fltkgeneral/f3b216cc-5df9-4cd5-8661-5025fe84d5cen%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages