Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] how to split long integer number like

Status
Not open for further replies.

buddhikaneel

Member level 1
Joined
Jul 27, 2011
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,542
Hi.
I have long integer number. Ex:- 1234567890;
i want to get numbers withing for loop like this
for (i=0;i<10;i++){
xArray=??????

}

result could be
xarray[0]=1
xarray[1]=2
xarray[2]=3
xarray[3]=4
xarray[4]=5
xarray[5]=6
xarray[6]=7
xarray[7]=8
xarray[8]=9
xarray[9]=0

If i get like this i can use it for my work. Can i have idea to do this. In Visual Basic i can do this using <b>substring Function.</b> But with mikroc i don't know what is the command.

Thanks
 

i think sprint is more complicate to do this.
 

Hi,

Actually you could use MikroC's sprintf() or sprintl() routines. The sprintl() does not support floats, however consumes less program memory.

An example:

Code:
void main(void)
{
     char buffer[20];
     long test = -1234567890;

     sprintl(buffer, "%ld", test);

     while(1);
}

After the execution of the sprintl() routine the char array buffer[] will contain the long integer converted into character string.

If you want or need to conserve program memory there are alternatives which do not use MikroC's sprintf library and are more efficient.

If you would like to see the alternatives just ask.

BigDog
 
your code also working properly. i used this code.
void display_Phn_No(){
char txt[12];
long xepoch;
xepoch=1234567809;
LongToStr(xepoch, txt);
lcd_cmd(_LCD_CLEAR);
for (i=0;i<11;i++){
lcd_chr(1,i,txt);
lcd_chr(2,1,i+48);
delay_ms(1000);
}
}


what is the best code..?
my need is this 10 Digit number will brake into 10 parts and each digit will run another programm according to Digit's value.
void (char xDigit){
switch (xDigit){
case 0: //Code written for 0; break;
case 1: //Code written for 1; break;
.....
.....
case 1: //Code written for 9; break;
default: //Code written for not 0 to 9 value;
}

i think both code's are same... which one is the good for my job?
thanks Mr. BigDog...
 

what is the best code..?

I'm not particularly fond of MikroC's LongToStr() as it requires a minimum char buffer length of 12 and pads the remaining positions with character space.

my need is this 10 Digit number will brake into 10 parts and each digit will run another programm according to Digit's value.
void (char xDigit){
switch (xDigit){
case 0: //Code written for 0; break;
case 1: //Code written for 1; break;
.....
.....
case 1: //Code written for 9; break;
default: //Code written for not 0 to 9 value;
}

i think both code's are same... which one is the good for my job?

Both would work for your application, however the sprintl() gives you more control over the length of the character buffer, which would be advantageous if you know with certainty what the length of the string is a head of time.

Actually, there is a third option with might be even a better fit for you application. And that is write your own routine.

You could even store each digit in an array of integers without converting it to a character.

BigDog
 
This is a custom function to convert a number to the ASCII representation


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void ULongToStr(unsigned long value, char *str,char trailing_zero,char digits)
{
    char first_non_zero=1;
    str+=(digits-1);  // move the pointer to the last char of the array
    while (digits!=0)  // execute the loop for the times defined in digits variable
    {   if (value==0)   // if the remaining number is 0 then just add '0' to speed up the conversion
        {   *str='0';
            str--;  // move to the previous char of the array
        }
        else
        {
            *str = (char) ((value%10)+48); // find the digit value using modulo and assign the ASCII representation
            str--;  // move to the previous char of the array
            value= value/10;    // divide input input number by 10 for next loop
        }
        digits--;   // used as a counter to detect when we have reached the first char of the array
    }
 
    // at this point the array pointer is at the address &array[0]-1
    if (trailing_zero==0)   // if parameter is set for no trailing zeroes
    {
        while(first_non_zero==1)    // if the first digit of the number is not found
        {   str++;  // move to next array character
            if (*str=='0') *str=0;      // if digit value is ADCII 0 then replace with null (user selectable)
            else first_non_zero=0;      // else set the flag that the first digit of the number was found
 
        }
    }
}



to call it ULongToStr(value, my_char_array, trailing_zero, digits);
where
value is an unsigned variable but can be modified to use a signed variable and you have to take care of the sign for negative numbers
my_char_array is a pointer to the first char of the array (the same as &my_char_array[0])
trailing_zero can be 0 so that trailing zeroes are replaced with null or 1 to leave them as 0
digits is the number of digits that will be converted starting from the last digit of the provided number, if you use 5 and the number is 123567890 then the array result will be 67890

the routine takes about 12200 clocks (in AVR) for 10 characters and is reduced about 1000 cycles for each lower character count (9,8,7 etc) while sprintf takes about 2700 clock but at the same time this routine uses about 1500 bytes less space than sprintf.

I would be happy to hear any suggestions to make the function more efficient or an alternative custom function

Alex

EDIT: I have made a small modification in line 6 to speed up the conversion when the number of digits of the input number are less that the specified result digits
 
Last edited:
This is a much faster function compared to the previous version, it is also about three times faster than sprintf, the parameters are exactly the same as the previous version but it gives a result in about 1100 cycles (12 times faster than the previous one) for 10 digits with input of 3999999999 (worse case input number), anything else will take less, for example the number 1234567890 will need about 770 clocks


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void ULongToStr(unsigned long value, char *str, char trailing_zero, char digits)
{
    static long powers[9] = {10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
    char temp_digit,digit_cnt=10,first_non_zero=1;
 
    while (digit_cnt!=1)  // initially this value is 10 so the loop is executed 9 times
    {
        temp_digit = 48;  // ASCII 0
        while (value >= powers[digit_cnt-2])  // while number bigger than power value
        {
            value -= powers[digit_cnt-2];       // subtract the power value
            temp_digit++;                       // and increase variable
        }
        if (digit_cnt<=digits) *str++ = temp_digit; // only store requested number of digits  
 
        digit_cnt--;   // used as a counter to detect when we have reached the last char of the array
    }
    *str = (char) value+48; // assign the last character
 
    if (trailing_zero==0)   // if parameter is set for no trailing zeroes
    {   str-=digits;    // go to the first char of the array
        while(first_non_zero==1)    // if the first digit of the number is not found
        {   str++;  // move to next array character
            if (*str=='0') *str=0;      // if digit value is ADCII 0 then replace with null (user selectable)
            else first_non_zero=0;      // else set the flag that the first digit of the number was found
 
        }
    }
}



to call it ULongToStr(value, my_char_array, trailing_zero, digits);
where
value is an unsigned variable but can be modified to use a signed variable and you have to take care of the sign for negative numbers
my_char_array is a pointer to the first char of the array (the same as &my_char_array[0])
trailing_zero can be 0 so that trailing zeroes are replaced with null or 1 to leave them as 0
digits is the number of digits that will be converted starting from the last digit of the provided number, if you use 5 and the number is 123567890 then the array result will be 67890

Alex
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top