|
|
View previous topic :: View next topic |
Author |
Message |
Picmicro_man
Joined: 01 Jan 2022 Posts: 13
|
string to decimal conversion |
Posted: Tue Jan 04, 2022 3:36 am |
|
|
Hi Guys
I am receiving serial data as follows:
Quote: | 2369,505,2369,505,0047,0000,001,018,000,000,000,000,000,024,000,000,0018,0000,0000,0000,0,0,0,1,0,0,1,0 |
I want to separate this data and store as a decimal value. Like convert 2369 from character to decimal 2369.
For this what I have to do ? Kindly guide me.
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Jan 04, 2022 4:04 am |
|
|
The standard approach, is to 'walk through' the string.
Start with num=0.
A) Read the character, and add this (-0x30) to num.
Is the next character a 'separator' (, or line feed as shown)?.
If so you have a complete number. Store this. If you have got to the
end of the line, exit. Otherwise advance one character, and go back to the
start.
If not, multiply num by ten, advance to the next character, and go back to 'A'. |
|
|
Picmicro_man
Joined: 01 Jan 2022 Posts: 13
|
|
Posted: Tue Jan 04, 2022 5:53 am |
|
|
Hi.
Quote: | If so you have a complete number. Store this. |
How to store one complete number in a single variable? like 2369
Code: |
#include <16F877A.h>
#device adc=8
#FUSES NOWDT,HS,PUT,NOBROWNOUT,NOLVP,NOCPD,NOWRT,NODEBUG,NOPROTECT
#use delay(clock=20000000,RESTART_WDT)
#use rs232(baud=2400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
char array[13] = {"2369,505,2369"};
char *ptr;
int value = 0x30;
int dec_array[4];
int a;
void main()
{
for(a = 0; a < 4; a++)
{
dec_array[a] = array[a] - value;
printf("%d", dec_array[a]);
}
while(TRUE)
{
}
}
|
actually I don't want all string to be converted. I want some data which I can extract individually using index number.
Kindly guide me in the above example 2369 value how I store it in a single variable?
Thanks |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Tue Jan 04, 2022 6:12 am |
|
|
Maybe this would work:
Code: |
unsigned int16 decimal_value;
decimal_value = 1000*dec_array[0] + 100* dec_array[1] + 10*dec_array[2] + dec_array[3]
|
Last edited by PrinceNai on Tue Jan 04, 2022 6:17 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Jan 04, 2022 6:17 am |
|
|
3 comments..
1) Delete the restart_WDT code ! You should never enable the WDT until your code is 100% completely working.
2) Be sure your variables for the decoded numbers are sized correctly. CCS compiler default for an int is 8 bits, so the max number is 255. You'll probably need 'unsigned int16'.
3) I'd have 2 arrays, the raw string data and a 'converted to numbers' array. The index value will then 'point' to actual numbers,so while you're decoding the string, you store the number in the 2nd array THEN increment the index, then decode the next string 'number'. |
|
|
Picmicro_man
Joined: 01 Jan 2022 Posts: 13
|
|
Posted: Tue Jan 04, 2022 6:32 am |
|
|
Hi
I have tried this
Code: |
char array[13] = {"2369,505,2369"};
char *ptr;
int value = 0x30;
int dec_array[4];
int16 converted_value;
int a;
void main()
{
for(a = 0; a <= 3; a++)
{
converted_value = converted_value + (array[a] - value);
if (a <= 2) converted_value *= 10;
}
printf("%ld", converted_value);
while(TRUE)
{
}
}
|
kindly tell me if it is an efficient way to do or some more betterment can be done? |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Tue Jan 04, 2022 6:38 am |
|
|
I'd just initialize the converted_value to 0 at the start, to avoid any problems when you restart the program. |
|
|
Picmicro_man
Joined: 01 Jan 2022 Posts: 13
|
|
Posted: Tue Jan 04, 2022 6:44 am |
|
|
Hi
I have modified the code and make a function but I am confuse with the array and pointer. Can you point me the mistake here? I am not getting the result as it was in my previous program
Code: |
char array[13] = {"2369,505,2369"};
char *ptr;
int value = 0x30;
int dec_array[4];
int16 converted_value = 0;
int a;
int16 ascii_to_dec(char *ptr, starting_index, ending_index)
{
int index;
int16 value;
int hex_value = 0x30;
char *ptr_1;
ptr_1 = ptr;
for(index = 0; index <= starting_index; index++)
{
value = value + (ptr_1[index] - hex_value);
if(index <=2) value *= 10;
}
return value;
}
void main()
{
/*
for(a = 0; a <= 3; a++)
{
converted_value = converted_value + (array[a] - value);
if (a <= 2) converted_value *= 10;
}
*/
converted_value = ascii_to_dec(array,0,3);
printf("%ld", converted_value);
while(TRUE)
{
}
}
|
can i pass array to a function using pointer and how to access it in a function. Kindly guide me |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Jan 04, 2022 7:49 am |
|
|
I'd have to suggest you get a C textbook first.
This is all basic C....
If you don't want to use any except one particular number, then simply
'count' the numbers, till you get to the one you want. |
|
|
|
|
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
|