Page 1 of 1

INT to Byte conversion for decimals

Posted: Wed Aug 30, 2023 3:36 pm
by walbeek
Hi there,

I have used a INT variable in a counter program.
The INT can be filled with a value from 001 to 999.
I would like to split the value to 3 bytes.
So, the first (right) character should be converted to Byte_Low
The center character to Byte_Middle
The left character to Byte_High.
For example, the value of the int is 432.
After conversion, Byte_Low = 2, Byte_Middle = 3, Byte_High = 4.

The bytes have to be written to a 7-segment display and Eeprom.
Please let me know if this is possible.

Thanks,
Rinie

Re: INT to Byte conversion for decimals

Posted: Wed Aug 30, 2023 4:58 pm
by kersing
Hi Rinie,

Yes this is possible.

Just divide the INT by 100 for byte_high, then take the remainder of the division by 100 and divide it by 10, that is middle and low is the remainder of a division by 10. High, middle and low should be bytes or ints, no floating point variables for this to work.

So:
High = Int / 100
Middle = ( Int % 100 ) / 10
Low = Int % 10

Re: INT to Byte

Posted: Fri Oct 06, 2023 6:47 pm
by artan
Hello, can you help me that an int number for example -5 or -35 which I present in the lcd I can write on the eprom and read later, I am using an internal eprom atmega8. thank you

Re: INT to Byte conversion for decimals

Posted: Sat Oct 07, 2023 4:05 am
by chipfryer27
Hi

As your post isn't really to do with the original post, you should have started a new topic.

You can use the EEPROM [2D[ component found under Storage. Integers require two bytes of storage on an 8-bit device so you will need two bytes per integer stored.

Do note that EEPROM locations have limited erase/write capabilities, 100,000 times for your chip, so it's better to only update the EEPROM value if it has changed.

A search of the forum should show some techniques on using locations, such as monitoring the erase/write cycles and moving location if approaching 100,000 cycles etc.

Regards

Re: INT to Byte conversion for decimals

Posted: Tue Oct 31, 2023 9:33 am
by walbeek
@ kersing,

Thanks for your help, works very well.
I have also managed to get it working the other way around.
So reading from the EEPROM and convert the 3 BYTES into a INT variable.
Thanks again!

Rinie