CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Convert long integer into single digits number

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ernest



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

Convert long integer into single digits number
PostPosted: Thu Apr 08, 2004 2:08 am     Reply with quote

Hi,

I have written a program to acquire ASCII data ie some numerical numbers such as 123456 for example.

How can I split them into half ie 3 most significant numbers into one variable while 3 least significant numbers into another variable.
ie.
123456 ==> 123 saved in one variable, 456 saved in different variable

Besides that, how can I extract each and every single digit out of the long integer. I want to save the value of one SINGLE digit into different variables.
That means, '1' into one variable, '2' into another, '3' into another variable, '4' into another different variable and so on for ALL 6 digits.

Please advise...

Thanks a lot.
Ttelmah
Guest







Re: Convert long integer into single digits number
PostPosted: Thu Apr 08, 2004 2:23 am     Reply with quote

ernest wrote:
Hi,

I have written a program to acquire ASCII data ie some numerical numbers such as 123456 for example.

How can I split them into half ie 3 most significant numbers into one variable while 3 least significant numbers into another variable.
ie.
123456 ==> 123 saved in one variable, 456 saved in different variable

Besides that, how can I extract each and every single digit out of the long integer. I want to save the value of one SINGLE digit into different variables.
That means, '1' into one variable, '2' into another, '3' into another variable, '4' into another different variable and so on for ALL 6 digits.

Please advise...

Thanks a lot.


Same basic route for both. If you take the number, and perform num%10, then you have the value of the right hand digit. If you then divide by ten, and repeat, you have the next digit. Unfortunately, the 'nicest' way to do this (the ldiv function, which qives you both the division result, and the remainder, in one operation), only works for 'long' (16bit) values, not for int32 (which you will need to use to store the seperate digits).
So:
Code:

int32 val=123456;
int16 low;
int16 high;

low=val % 1000L;
high=val / 1000L;

Will split the number into 123', and '456'.
While:
Code:

int8 digits[7];
signed int8 counter;
int32 val=123456;

for (counter=7;counter>-1;counter--) {
    digits[counter]=val%10;
    val=val/10;
}

Will give you an array of seven integers, containing:
digits[0] 6
digits[1] 5
digits[2] 4
digits[3] 3
digits[4] 2
digits[5] 1
digits[6] 0

Realistically, given that the numbers are arriving as seperate digits, it would probably be more efficient (remove the need for quite a bit of arithmetic), to keep the digits seperate in the first place.

Best Wishes
ernest



Joined: 11 Feb 2004
Posts: 51

View user's profile Send private message

Convert long integer into single digits number
PostPosted: Thu Apr 08, 2004 3:11 am     Reply with quote

Thanks a lot...That certainly is very helpful.

But besides splitting them, I actually want to separate each and every single digits ie. split the 6 digits number to the following variable format:

Hundreds_Thou : Tens_Thou : Thou : Hundreds : Tens : Ones

That means the final answere will give us 6 different variables containing 6 independent digits (1 digit for each variable).

Cheers...
Ttelmah
Guest







Re: Convert long integer into single digits number
PostPosted: Thu Apr 08, 2004 5:08 am     Reply with quote

ernest wrote:
Thanks a lot...That certainly is very helpful.

But besides splitting them, I actually want to separate each and every single digits ie. split the 6 digits number to the following variable format:

Hundreds_Thou : Tens_Thou : Thou : Hundreds : Tens : Ones

That means the final answere will give us 6 different variables containing 6 independent digits (1 digit for each variable).

Cheers...

Er. That is what I have posted...

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group