Unexpected behaviour from strtoul

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Unexpected behaviour from strtoul

355 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by philippschrader on Wed Dec 24 17:25:28 MST 2014
Hi,

I'm currently experiencing some strange behaviour with `strtoul'.

I've got the following code running on the M0 of an LPC4357 part:
NEW_CLI_STATE(convert_cmd)
{
  for (int i = 1; i < argc; i++) {                              
    unsigned int num = (unsigned int)strtoul(argv, NULL, 16);
    unsigned int i_new = (unsigned int)i;                       
    cli.Printf("%s: %u: %u\n", argv, i_new, num);            
  }
}


the `cli' class above is simply a wrapper around USART3.

Now when I actually execute the code, I get this output:
$ convert 1 2 3 4 5 6 7 
                        
1: 1: 0                 
2: 2: 0                 
3: 3: 0                 
4: 4: 4                 
5: 5: 0                 
6: 6: 0                 
7: 7: 0                 
                           
$ convert 2 3 4         
                        
2: 1: 0                 
3: 2: 0                 
4: 3: 4                 
                          
$ convert 8 9 142       
                        
8: 1: 0                 
9: 2: 0                 
142: 3: 0               
                             
$ convert 12 34 56 78 90
                        
12: 1: 0                
34: 2: 4                
56: 3: 0                
78: 4: 0                
90: 5: 0                
                            
$                       


Any idea why my call to `strtoul' is not giving me the results I'm expecting?

I'm using LPCXpresso 7.3.

Thanks,
0 Kudos
5 Replies

349 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by philippschrader on Thu Jan 08 14:36:45 MST 2015
Sorry for the delay, I'm currently debugging another issue related to multi-core linking.

Once I get back to this, I'll post a mini-project to see if anyone else has any ideas.
0 Kudos

349 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Tue Jan 06 15:00:01 MST 2015
Post an example project, so we help you debug it.
0 Kudos

349 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by philippschrader on Tue Jan 06 14:23:26 MST 2015
I should also mention that when stepping through the code in a debug session I see that the values returned by the "strtoul" calls are the ones being printed out. So I'm pretty certain that there is something about the function itself that's messing with me.
0 Kudos

349 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by philippschrader on Tue Jan 06 13:25:54 MST 2015
I get the same behaviour when I use unsigned long instead of unsigned int.

I've also noticed that the "isprint" function returns weird numbers like this as well. When I create a similar command to the one above but called isprint instead and printing the result I get true or false returned randomly.

Is it possible that these functions aren't being linked properly?
0 Kudos

349 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Thu Dec 25 09:35:18 MST 2014
Try displaying the unsigned long directly, instead of converting to unsigned int.
0 Kudos