View previous topic :: View next topic |
Author |
Message |
Requan
Joined: 11 May 2008 Posts: 74
|
Different way to convert four digits to one int16 |
Posted: Fri Jul 20, 2012 6:51 am |
|
|
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: 9229 Location: Greensville,Ontario
|
|
Posted: Fri Jul 20, 2012 7:34 am |
|
|
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
|
|
Posted: Fri Jul 20, 2012 7:35 am |
|
|
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
|
|
Posted: Fri Jul 20, 2012 7:57 am |
|
|
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
|
|
Posted: Fri Jul 20, 2012 8:32 am |
|
|
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
|
|
Posted: Fri Jul 20, 2012 8:39 am |
|
|
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
|
|
Posted: Fri Jul 20, 2012 11:57 am |
|
|
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
|
|
|
|
|