| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| pebbert9 
 
 
 Joined: 31 Dec 2010
 Posts: 39
 
 
 
			    
 
 | 
			
				| itoa and negative numbers |  
				|  Posted: Tue Sep 14, 2021 8:54 pm |   |  
				| 
 |  
				| Hello, 
 I am having trouble sending negative numbers out to a serial port. Does anyone see the issue with this test code?
 I need to use an int48 and it works fine for positive numbers. I am using a PIC33CK32MP502 and compiler 5.105.
 
 
  	  | Code: |  	  | char strTest[20] = {0};
 
 signed int48 Test = -100000;
 
 itoa(Test, 10, strTest);
 
 fprintf(rs485, "%s\r\n",strTest);
 | 
 
 It is sending -4294967295 to the serial port.
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Sep 15, 2021 2:07 am |   |  
				| 
 |  
				| OK. Do you _'have'_ to use int48???.
 There have been issues with the arithmetic for this type for just about
 every compiler release for ages...
 I suspect you will find it'll work OK, with int32, or int64.
 Report it to CCS, but if you can as a 'get out of the problem' solution,
 change to int64. If you need  an int48 result, do the conversion using
 int64 only. So:
 
  	  | Code: |  	  | itoa((signed int64)Test, 10, strTest);
 
 | 
 |  | 
	
		|  | 
	
		| pebbert9 
 
 
 Joined: 31 Dec 2010
 Posts: 39
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Wed Sep 15, 2021 5:39 am |   |  
				| 
 |  
				| Thank you! 
 Converting to int64 worked.
 |  | 
	
		|  | 
	
		|  |