fork download
  1. /* strtoul example */
  2. #include <stdio.h> /* printf, NULL */
  3. #include <stdlib.h> /* strtoul */
  4. #include <errno.h>
  5.  
  6. signed int GetIdentifier(const char* idString)
  7. {
  8. char *str_end;
  9. int id = -1;
  10. errno = 0;
  11. id = strtoul(idString, &str_end, 16);
  12. printf("Id (%s) - str_end %s - errno %d.\n", idString, str_end, errno);
  13. if ( *str_end != '\0' || (errno == ERANGE))
  14. {
  15. printf("Error while converting Id (%s) - str_end %s - errno %d.\n", idString, str_end, errno);
  16. return -1;
  17. }
  18.  
  19. // Return error if converted Id is more than 29-bit
  20. if(id > 0x1FFFFFFF)
  21. {
  22. printf("Error: Id (%s) should fit on 29 bits (maximum value: 0x1FFFFFFF).\n", idString);
  23. return -1;
  24. }
  25. printf("sucess\n");
  26. return id;
  27. }
  28.  
  29.  
  30. int main ()
  31. {
  32. GetIdentifier("inv");
  33. GetIdentifier("0x210000000");
  34. GetIdentifier("0x10000000");
  35.  
  36. return 0;
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
Success #stdin #stdout 0s 2728KB
stdin
Standard input is empty
stdout
Id (inv) - str_end inv - errno 0.
Error while converting Id (inv) - str_end inv - errno 0.
Id (0x210000000) - str_end  - errno 34.
Error while converting Id (0x210000000) - str_end  - errno 34.
Id (0x10000000) - str_end  - errno 0.
sucess