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

Different way to convert four digits to one int16

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



Joined: 11 May 2008
Posts: 74

View user's profile Send private message

Different way to convert four digits to one int16
PostPosted: Fri Jul 20, 2012 6:51 am     Reply with quote

Hi,
I received in buffer via TCP/IP four digits in dec e.g:
Code:

buffer[0] = 54;
buffer[0] = 48;
buffer[0] = 49;
buffer[0] = 55;

to convert to in16 i made calculation:
Code:

int16 Addres = ((buffer[4]-48)*1000) + ((buffer[5]-48)*100) + ((buffer[6]-48)*10) + (buffer[7]-48);


What do You think about this method ? is it easer way to do it?
temtronic



Joined: 01 Jul 2010
Posts: 9219
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jul 20, 2012 7:34 am     Reply with quote

While there are probably N+1 ways to solve the problem..
you have to ask yourself...

Q1: Does it work exactly the way you need it to ?

Q2:Do you understand how it works ?

Q3:Is it fast enough for your application?

Q4: Is it small enough?

If you answered YES to all of the above, then there is no need to change the code.

Sure there are other ways to do it, but if 'happy' with your solution why change ?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Jul 20, 2012 7:35 am     Reply with quote

1) Make ABSOLUTELY sure the input values are in the correct range. If not it could be a nightmare to debug. Consider using the function isdigit() or isxdigit().

2) Use ((buffer[4]-'0')*1000) instead of ((buffer[4]-48)*1000). In the future you or someone else supporting your code may wonder where that magic number 48 came from?
_________________
The search for better is endless. Instead simply find very good and get the job done.
Requan



Joined: 11 May 2008
Posts: 74

View user's profile Send private message

PostPosted: Fri Jul 20, 2012 7:57 am     Reply with quote

When i have 4 digit to convert it works ok, but when i have less it doesn't.
I can use "if statement" but maybe it is better solution
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Jul 20, 2012 8:32 am     Reply with quote

isdigit() should tell you if there is a valid digit available.
_________________
The search for better is endless. Instead simply find very good and get the job done.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Jul 20, 2012 8:39 am     Reply with quote

Requan wrote:
When i have 4 digit to convert it works ok, but when i have less it doesn't.
So how do you know when there are less than 4 digits?
The standard method would be to have the text as a string, i.e. your array is to have a terminating 0x00 as the last character. That way you can use the C-function atoi() for the conversion and it will handle strings with all number of digits.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Jul 20, 2012 11:57 am     Reply with quote

Quote:

That way you can use the C-function atoi() for the conversion


for 16 bits it is atoL()

but when i don't KNOW what value i am expecting, i tend to do this:
Code:


int32 temp;
 temp=atoi32(instrng);


// then cast the eventual type DOWN as i may need
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