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

translate string number to unsigned long

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







translate string number to unsigned long
PostPosted: Wed Apr 24, 2002 2:22 am     Reply with quote

Hi All

I try to read an unsigned long via the RS232 into my pic.
I dont know how to do.

In the following code I read a sting (number) into my pic (16F877).
Is it possible to translate it in a unsigned long?
Are there other (more simple) possebilities?

////////////////code/////////////////////////////////////

#fuses HS,NOWDT,NOPROTECT

#use delay(clock=20000000)
#use rs232(baud=1200, xmit=PIN_C6, rcv=PIN_C7)


main() {

char string[50];

printf("give the number of pulses\r\n");
gets(string);
printf("\%S\n", string);
while(1);
//// now translate string to unsigned long
}

//////////////////////////////////////////////////////////////

thanks for help,
ruud
___________________________
This message was ported from CCS's old forum
Original Post ID: 3976
Hans Wedemeyer
Guest







Re: translate string number to unsigned long
PostPosted: Wed Apr 24, 2002 7:08 am     Reply with quote

If it is an ASCII string the simply use the atoi(&string); function.

If you simply want to receive the bytes directly as binary values, then put them into a struct/union one by one and you do not have to convert anything.
Example:-

struct HighLow
{
char l;
char h;
};

union HL
{
long L;
struct HighLow hl;
};


union HL MyNumber;

MyNumber.hl.h = getchar(); // receive the high byte first
MyNumber.hl.l = getchar(); // receive the low byte next

Now you have the unsigned long value as MyNumber.L and no conversion needed.

unsigned long XYZ = MyNumber.L;

Then when you need to send it back out simply do this:-

putchar(MyNumber.hl.h);
putchar(MyNumber.hl.l);




:=////////////////code/////////////////////////////////////
:=
:=#fuses HS,NOWDT,NOPROTECT
:=
:=#use delay(clock=20000000)
:=#use rs232(baud=1200, xmit=PIN_C6, rcv=PIN_C7)
:=
:=
:=main() {
:=
:= char string[50];
:=
:= printf("give the number of pulses\r\n");
:= gets(string);
:= printf("\%S\n", string);
:= while(1);
:= //// now translate string to unsigned long
:=}
:=
:=//////////////////////////////////////////////////////////////
:=
:=thanks for help,
:= ruud
___________________________
This message was ported from CCS's old forum
Original Post ID: 3981
Todd C. Micallef
Guest







Re: translate string number to unsigned long
PostPosted: Wed Apr 24, 2002 11:08 am     Reply with quote

Another way to convert is to use the get_long()function in the input.c file inside the drivers dir. The function defaults to returning a signed value though.

:=Hi All
:=
:=I try to read an unsigned long via the RS232 into my pic.
:=I dont know how to do.
:=
:=In the following code I read a sting (number) into my pic (16F877).
:=Is it possible to translate it in a unsigned long?
:=Are there other (more simple) possebilities?
:=
:=////////////////code/////////////////////////////////////
:=
:=#fuses HS,NOWDT,NOPROTECT
:=
:=#use delay(clock=20000000)
:=#use rs232(baud=1200, xmit=PIN_C6, rcv=PIN_C7)
:=
:=
:=main() {
:=
:= char string[50];
:=
:= printf("give the number of pulses\r\n");
:= gets(string);
:= printf("\%S\n", string);
:= while(1);
:= //// now translate string to unsigned long
:=}
:=
:=//////////////////////////////////////////////////////////////
:=
:=thanks for help,
:= ruud
___________________________
This message was ported from CCS's old forum
Original Post ID: 3986
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