| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| pilar 
 
 
 Joined: 30 Jan 2008
 Posts: 197
 
 
 
			    
 
 | 
			
				| String Ascii to int |  
				|  Posted: Tue Jan 29, 2013 4:50 pm |   |  
				| 
 |  
				| Hi, some one can help me... 
 I have a string ascii:
 
  	  | Code: |  	  | Buffer[0] = 0x31; Buffer[1] = 0x31;
 Buffer[2] = 0x33;
 Buffer[3] = 0x39;
 | 
 
 I should I do to get the equivalent: 4409 decimal?, I need this to do math
 |  | 
	
		|  | 
	
		| Mike Walne 
 
 
 Joined: 19 Feb 2004
 Posts: 1785
 Location: Boston Spa UK
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Jan 29, 2013 6:14 pm |   |  
				| 
 |  
				| There are 'C' functions for this, ATOI(), ATOL(), ATOI32. 
 Take your pick.
 
 Mike
 
 EDIT Your ASCII values convert to 1139 !
 |  | 
	
		|  | 
	
		| pilar 
 
 
 Joined: 30 Jan 2008
 Posts: 197
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Jan 29, 2013 6:17 pm |   |  
				| 
 |  
				|  Maybe I misspoke, I'm getting for the Uart the Ascii characters 1,1,3 and 9, representing a hexadecimal number 0x1139, these characters must be equivalent to 4409 in decimal value, my problem is that I have the any idea how to make their equivalence ... : |  | 
	
		|  | 
	
		| Mike Walne 
 
 
 Joined: 19 Feb 2004
 Posts: 1785
 Location: Boston Spa UK
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Jan 29, 2013 6:30 pm |   |  
				| 
 |  
				| To do it by hand:- 
 1) Convert first digit from HEX to decimal. //1
 2) Multiply by 16. // 16
 3) Convert second digit to HEX.  // 1
 4) Add results from 2 and 3 together. // 17
 5) Multiply new result by 16. // 272
 6) Continue until you run out of digits.
 
 
 Mike
 |  | 
	
		|  | 
	
		| PCM programmer 
 
 
 Joined: 06 Sep 2003
 Posts: 21708
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Jan 29, 2013 7:03 pm |   |  
				| 
 |  
				| Here is a 16-bit version of a function that I used to use for this: 
  	  | Code: |  	  | #include <18F4520.h>
 #fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
 #use delay(clock=4M)
 #use rs232(baud=9600, UART1, ERRORS)
 
 #include <ctype.h>
 
 //-------------------------------------------------
 // Ascii to unsigned long -
 // Converts a string of 1 to 4 ascii hex characters to an
 // unsigned long (16-bit) value.
 
 int16 axtoul(char *ptr)
 {
 char i, temp;
 int16 result;
 
 result = 0;
 
 for(i = 0; i < 4; i++)           // Convert a maximum of 4 hex digits
 {
 temp = toupper(*ptr++);      // Get the char.  Convert to upper case
 if(isxdigit(temp) == FALSE)  // Is ascii char a hex digit ?
 break;         // Break if not
 temp -= '0';      // Convert the char to binary
 if(temp > 9)
 temp -= 7;
 result <<= 4;      // shift existing value left 1 nybble
 result |= temp;    // Then combine it with new nybble
 }
 
 return(result);
 }
 
 //================================
 void main(void)
 {
 int16 result;
 int8 buffer[] = "1139";   // Hex value
 
 result = axtoul(buffer);
 printf("result = %lu \r", result);
 
 
 while(1);
 }
 | 
 |  | 
	
		|  | 
	
		| pilar 
 
 
 Joined: 30 Jan 2008
 Posts: 197
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Jan 29, 2013 7:16 pm |   |  
				| 
 |  
				|  Thank you.... |  | 
	
		|  | 
	
		| asmboy 
 
 
 Joined: 20 Nov 2007
 Posts: 2128
 Location: albany ny
 
 
			      
 
 | 
			
				|  |  
				|  Posted: Wed Jan 30, 2013 7:39 am |   |  
				| 
 |  
				| not to be a nit picker but if it is really  a string , i would include 
 
 the way you want to handle it, "buffer " does technically NOT contain a "string" at all, unless every instance is limited to 4 bytes by code.
 
 just my 2 cents
 |  | 
	
		|  | 
	
		|  |