Missing subject Split Long integer 32bit or 64bit into Bytes

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
Huib Koppers
Posts: 63
Joined: Fri Oct 12, 2007 5:13 pm
Location: Netherlands
Been thanked: 1 time
Contact:

Missing subject Split Long integer 32bit or 64bit into Bytes

Post by Huib Koppers »

Hello,

The C4AVR coarse handles Integer, but not "long integer" and "split long integer" variable for 32 or 64bit to 4 or 8 bytes funtions.
Also conversions from 32bit or 48bit to 9 or 10 digit decimal into 4 or 6 byte or string format explained into C is very usefull to me.

Maybe my subject question lays out of the level of this coarse and i can understand

But this is for me an important part which has become a lot of interrest for my DDS project.

Don't understand my wrong its a good coarse with very usefull information but was also espacially interrested in the split and long integer variants and my hope was finding it in C4AVR

There are some asm examples but i am also interrested in the C version to be explained.

I have googled about the subject but give me not the full explanation about syntacts build in C

Are there some books who handle the full explanation long integer and in combination with split functions ?

The reason about this is that i have DDS chips with 32bit and 48bit tuning words to make 1 Hz tunning possible.
The split function is needed to split the 32bit into 4 bytes to save to 8bit memory locations for EEprom and SPI port adressing.
Also conversion from 32bit to 9 digits decimal to save into 4byte or string format is an interresting and important subject for me

I hope you can help me

Kind Regards

Huib

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Missing subject Split Long integer 32bit or 64bit into B

Post by Benj »

Hello Huib,

Here is a quick example of a 32 bit variable and splitting it up into 4 bytes.

unsigned long var32;

var32 = 0xFFFFFFFF;

char bytes[4];

bytes[0] = var32 & 0xFF;
bytes[1] = (var32 >> 8 ) & 0xFF;
bytes[2] = (var32 >> 16) & 0xFF;
bytes[3] = (var32 >> 24) & 0xFF;

Not sure about 64-bit variables using GCC.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Missing subject Split Long integer 32bit or 64bit into B

Post by Benj »

Converting a number into a string is quite easy. Ill show you a quick example.

idx = 0;
if (var >= 100)
{
string[idx] = (var / 100) + '0';
var = var % 100;
idx = idx + 1;
}
if (var >= 10)
{
string[idx] = (var / 10) + '0';
var = var % 10;
idx = idx + 1;
}

string[idx] = var + '0';
idx = idx + 1;

string[idx] = 0;

Huib Koppers
Posts: 63
Joined: Fri Oct 12, 2007 5:13 pm
Location: Netherlands
Been thanked: 1 time
Contact:

Re: Missing subject Split Long integer 32bit or 64bit into B

Post by Huib Koppers »

Hello Ben,

Thanks for your reply

On your conversion example I understand that it does also the compleet the decimal conversion to decimal numbers because of the weight calcs 10, 100 etc, etc.
I shall try this out

Kind Regards

Huib

Post Reply