Convert 32 bit value to 8 bit

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
User avatar
sysprofessional
Posts: 54
Joined: Sat Oct 26, 2013 7:16 pm
Has thanked: 28 times
Been thanked: 4 times
Contact:

Convert 32 bit value to 8 bit

Post by sysprofessional »

Hi 2 all,

How can convert 32 bit ULong number into 8 bit?
Basically i want use a counter veriable that can count 0-99999,and then i want to store that data into eeprom,because eeprom support 8 bit method so i need conversion that can convert 32 bit number to 4 pieces of 8 bit value ,
and then back 8 bit to 32 bit for display

Please guide me a quick way.

Thanks

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Convert 32 bit value to 8 bit

Post by kersing »

A little searching the forum would have saved time as this has been discussed before, there is even a knowledge exchange article on the subject. (Link to PDF is in the first message)
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
sysprofessional
Posts: 54
Joined: Sat Oct 26, 2013 7:16 pm
Has thanked: 28 times
Been thanked: 4 times
Contact:

Re: Convert 32 bit value to 8 bit

Post by sysprofessional »

Hi kersing

Thanks for your response ,

i saw that article "knowledge exchange article"
after studying that i am able to convert 32 bit to 8 bit ,and combine 8 bit to 16 bit,but i could not understand how can combine 8 to 32 bit ,

can you please little explain that

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Convert 32 bit value to 8 bit

Post by kersing »

Split:

Code: Select all

byte0 = ((longInt >> 24) & 0xFF) ;
byte1 = ((longInt >> 16) & 0xFF) ;
byte2 = ((longInt >> 8 ) & 0XFF);
byte3 = (longInt & 0XFF);
Combine:

Code: Select all

longInt = byte3
longInt = longInt | (byte2 << 8)
longInt = longInt | (byte1 << 16)
longInt = longInt | (byte0 << 24)
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
sysprofessional
Posts: 54
Joined: Sat Oct 26, 2013 7:16 pm
Has thanked: 28 times
Been thanked: 4 times
Contact:

Re: Convert 32 bit value to 8 bit

Post by sysprofessional »

32bit to 8.fcf
(20.91 KiB) Downloaded 368 times
Thanks kersing
Plz check my attachments ,in this example file simply i convert 32 bit number to four piece of 8 bit ,store into eeprom and then combine back into one Ulong 32 bit number,and display on lcd,first line display foure 8 bit values,seperated with comma,and 2nd line display Ulong variable,
All simulation work correctly in flowcode,but after compilation ,on real hardware, or in proteus Ulong 32 bit variable work as 16 bit int ,while there is one more Ulong variable "count" work correctly in main program,
where is mistake plz correct me.

User avatar
sysprofessional
Posts: 54
Joined: Sat Oct 26, 2013 7:16 pm
Has thanked: 28 times
Been thanked: 4 times
Contact:

Re: Convert 32 bit value to 8 bit

Post by sysprofessional »

Hello

If some one has little spare time ,plz check my attachment and guide me

i m waiting .......

thanks in advance

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Convert 32 bit value to 8 bit

Post by kersing »

Your flowchart is for Flowcode version 5, please signup for the Flowcode v5 forum so we can move these messages there. (See the bottom of this message for the procedure)
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
sysprofessional
Posts: 54
Joined: Sat Oct 26, 2013 7:16 pm
Has thanked: 28 times
Been thanked: 4 times
Contact:

Re: Convert 32 bit value to 8 bit

Post by sysprofessional »

ok now i designe new flowchart in FC6,and it is very short and simple file,calculation method described below,

there are four fixed value 8 bit variable ,byte0=0 , byte1=1 , byte2=0 , and byte3=0 in flowchart.i want convert these four 8 bit values into one 32 bit value,using this method in calculation ,
Total = Byte3
Total = Total | Byte2 << 8
Total = Total | Byte1 << 16
Total = Total | Byte0 << 24

while Total is a Ulong 32 bit variable in flowchart,

when i start simulation in Flowcode it show 65536 on lcd, That is ok,
But when i compile this flowchart and run on real hardware LCD show 00000,
If Total value under or equal to 65535 it works fine ,but when Total increase 65535 Variable start form 0 and so on,
Why total variable did not work as ULong 32 bit ?
why it work as 16 bit
Example file Attached below

any idea will be appreciated
Attachments
Flowcode1.fcfx
(7.21 KiB) Downloaded 265 times

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Convert 32 bit value to 8 bit

Post by LeighM »

Hi
This relates to C integer promotion and the size of an integer on the target platform,
32 bit maths on 8 bit processor is never easy :roll:

Try something like this, with a Temp Ulong 32 bit variable

Code: Select all

Total = Byte3
Temp = Byte2
Total = Total | Temp << 8
Temp = Byte1
Total = Total | Temp << 16
Temp = Byte0
Total = Total | Temp << 24

User avatar
sysprofessional
Posts: 54
Joined: Sat Oct 26, 2013 7:16 pm
Has thanked: 28 times
Been thanked: 4 times
Contact:

Re: Convert 32 bit value to 8 bit

Post by sysprofessional »

Hi LeighM :D ,
This method is working fine,You solve my big problem,I'm really happy.
I am very much thankful to you,and I realy thankfull to FLOWCODE team and all the people who have tried to help me.
I spent a lot of time on this job,Finally,I completed with your help.

Post Reply