fork download
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6. {
  7. printf("strtok + strtoul\n");
  8.  
  9. char str[] = "24:6F:28:38:D1:A4";
  10.  
  11. size_t const addressLength = 6;
  12. uint8_t broadcastAddress[addressLength];
  13. size_t count = 0;
  14. char * p = strtok(str, ":");
  15.  
  16. while (p != NULL && count < addressLength)
  17. {
  18. broadcastAddress[count++] = (uint8_t)strtoul(p, NULL, 16);
  19. p = strtok(NULL, ":");
  20. }
  21.  
  22. for (size_t i = 0; i < count; i++)
  23. {
  24. printf("%d\n", broadcastAddress[i]);
  25. }
  26. }
  27.  
  28. {
  29. printf("\nstrtoul seul\n");
  30.  
  31. char str[] = "24:6F:28:38:D1:A4";
  32.  
  33. size_t const addressLength = 6;
  34. uint8_t broadcastAddress[addressLength];
  35. size_t count = 0;
  36. char * p = str;
  37.  
  38. while (count < addressLength)
  39. {
  40. broadcastAddress[count++] = (uint8_t)strtoul(p, &p, 16);
  41. p++;
  42. }
  43.  
  44. for (size_t i = 0; i < count; i++)
  45. {
  46. printf("%d\n", broadcastAddress[i]);
  47. }
  48. }
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 4236KB
stdin
Standard input is empty
stdout
strtok + strtoul
36
111
40
56
209
164

strtoul seul
36
111
40
56
209
164