diff options
Diffstat (limited to 'src/numconvert.c')
| -rw-r--r-- | src/numconvert.c | 146 |
1 files changed, 97 insertions, 49 deletions
diff --git a/src/numconvert.c b/src/numconvert.c index 174dae5..ff90a08 100644 --- a/src/numconvert.c +++ b/src/numconvert.c | |||
| @@ -112,6 +112,9 @@ | |||
| 112 | // main statement | 112 | // main statement |
| 113 | int main(int argc, char * argv[]); | 113 | int main(int argc, char * argv[]); |
| 114 | 114 | ||
| 115 | // print value | ||
| 116 | void my_print(int32_t opt, uintmax_t byte); | ||
| 117 | |||
| 115 | //displays usage information | 118 | //displays usage information |
| 116 | void my_usage(void); | 119 | void my_usage(void); |
| 117 | 120 | ||
| @@ -134,15 +137,11 @@ int main(int argc, char * argv[]) | |||
| 134 | int base; | 137 | int base; |
| 135 | int optbase; | 138 | int optbase; |
| 136 | int opt_index; | 139 | int opt_index; |
| 137 | char buff[9]; | ||
| 138 | int32_t opt; | 140 | int32_t opt; |
| 139 | intmax_t x; | 141 | intmax_t x; |
| 140 | intmax_t y; | 142 | intmax_t y; |
| 141 | intmax_t z; | ||
| 142 | uintmax_t len; | ||
| 143 | uintmax_t num; | 143 | uintmax_t num; |
| 144 | uintmax_t max; | 144 | char * endptr; |
| 145 | uintmax_t byte; | ||
| 146 | 145 | ||
| 147 | // getopt options | 146 | // getopt options |
| 148 | static char short_opt[] = "AaBbDdhlOoRrsVXx"; | 147 | static char short_opt[] = "AaBbDdhlOoRrsVXx"; |
| @@ -251,37 +250,75 @@ int main(int argc, char * argv[]) | |||
| 251 | 250 | ||
| 252 | for(x = optind; x < argc; x++) | 251 | for(x = optind; x < argc; x++) |
| 253 | { | 252 | { |
| 254 | len = 0; | 253 | // attempts to detect base |
| 255 | optbase = base; | 254 | optbase = base; |
| 256 | if (!(optbase)) | 255 | if (!(optbase)) |
| 257 | { | 256 | { |
| 258 | optbase = 10; | 257 | if (!(strncasecmp("0b", argv[x], (size_t)2))) |
| 259 | if (!(strncmp("00", argv[x], (size_t)2))) | 258 | { |
| 259 | optbase = 2; | ||
| 260 | argv[x][1] = '0'; | ||
| 261 | } | ||
| 262 | else if (!(strncasecmp("00", argv[x], (size_t)2))) | ||
| 260 | optbase = 2; | 263 | optbase = 2; |
| 261 | else if (!(strncmp("0x", argv[x], (size_t)2))) | 264 | else if (!(strncasecmp("0x", argv[x], (size_t)2))) |
| 262 | optbase = 16; | 265 | optbase = 16; |
| 263 | else if (!(strncmp("0", argv[x], (size_t)1))) | 266 | else if (!(strncmp("0", argv[x], (size_t)1))) |
| 264 | optbase = 8; | 267 | optbase = 8; |
| 265 | else | 268 | else |
| 266 | { | 269 | { |
| 267 | optbase = 10; | 270 | for(y = 0; ( ((uint32_t)y) < strlen(argv[x]) && (optbase != -1) ); y++) |
| 268 | for(y = 0; ( ((uint32_t)y) < strlen(argv[x]) && (optbase > 0) ); y++) | 271 | { |
| 272 | if ( (optbase < 10) && ((argv[x][y] >= '0') && (argv[x][y] <= '9')) ) optbase = 10; | ||
| 273 | else if ( (optbase < 16) && ((argv[x][y] >= 'a') && (argv[x][y] <= 'f')) ) optbase = 16; | ||
| 274 | else if ( (optbase < 16) && ((argv[x][y] >= 'a') && (argv[x][y] <= 'F')) ) optbase = 16; | ||
| 275 | else optbase = -1; | ||
| 276 | }; | ||
| 277 | }; | ||
| 278 | }; | ||
| 279 | |||
| 280 | // print value | ||
| 281 | switch(optbase) | ||
| 282 | { | ||
| 283 | case -1: | ||
| 284 | for(y = 0; (y < (int)strlen(argv[x])); y++) | ||
| 285 | my_print(opt, (intmax_t)argv[x][y]); | ||
| 286 | break; | ||
| 287 | |||
| 288 | default: | ||
| 289 | num = strtoumax(argv[x], &endptr, optbase); | ||
| 290 | if (endptr[0] != '\0') | ||
| 291 | { | ||
| 292 | switch(optbase) | ||
| 269 | { | 293 | { |
| 270 | c = argv[x][y]; | 294 | case 2: fprintf(stderr, "%s:%i: invalid binary number\n", PROGRAM_NAME, (int)(x - optind + 1)); return(1); |
| 271 | if ((c >= 'a') && (c <= 'z')) | 295 | case 8: fprintf(stderr, "%s:%i: invalid octal number\n", PROGRAM_NAME, (int)(x - optind + 1)); return(1); |
| 272 | optbase = 16; | 296 | case 10: fprintf(stderr, "%s:%i: invalid decimal number\n", PROGRAM_NAME, (int)(x - optind + 1)); return(1); |
| 273 | else if ((c >= 'A') && (c <= 'Z')) | 297 | default: fprintf(stderr, "%s:%i: invalid hexadecimal number\n", PROGRAM_NAME, (int)(x - optind + 1)); return(1); |
| 274 | optbase = 16; | ||
| 275 | else if ((c < '0') || (c > '9')) | ||
| 276 | optbase = 16; | ||
| 277 | }; | 298 | }; |
| 278 | }; | 299 | }; |
| 300 | my_print(opt, num); | ||
| 301 | break; | ||
| 279 | }; | 302 | }; |
| 280 | max = sizeof(intmax_t); | 303 | }; |
| 281 | if (optbase == -1) | 304 | |
| 282 | num = argv[x][0]; | 305 | return(0); |
| 283 | else | 306 | } |
| 284 | num = strtoumax(argv[x], NULL, optbase); | 307 | |
| 308 | |||
| 309 | // print value | ||
| 310 | void my_print(int32_t opt, uintmax_t num) | ||
| 311 | { | ||
| 312 | char buff[9]; | ||
| 313 | intmax_t y; | ||
| 314 | intmax_t z; | ||
| 315 | uintmax_t len; | ||
| 316 | uintmax_t max; | ||
| 317 | uintmax_t byte; | ||
| 318 | |||
| 319 | max = sizeof(intmax_t); | ||
| 320 | len = 0; | ||
| 321 | |||
| 285 | if (opt & MY_OPT_LIMIT) | 322 | if (opt & MY_OPT_LIMIT) |
| 286 | { | 323 | { |
| 287 | max = 1; | 324 | max = 1; |
| @@ -289,36 +326,47 @@ int main(int argc, char * argv[]) | |||
| 289 | if ( (num >> (y*8)) & 0xFF ) | 326 | if ( (num >> (y*8)) & 0xFF ) |
| 290 | max = (y+1); | 327 | max = (y+1); |
| 291 | }; | 328 | }; |
| 292 | if (opt & MY_OPT_ASCII) len = printf((len ? ((opt & MY_OPT_SPACE) ? ", '%c'" : ",'%c'") : "'%c'"), ((((num & 0xFF) >= 32) && ((num & 0xFF) <= 126)) ? (char)(num & 0xFF) : '.')); | 329 | |
| 293 | if (opt & MY_OPT_BIN) | 330 | // print ASCII value |
| 331 | if (opt & MY_OPT_ASCII) | ||
| 332 | len = printf((len ? ((opt & MY_OPT_SPACE) ? ", '%c'" : ",'%c'") : "'%c'"), ((((num & 0xFF) >= 32) && ((num & 0xFF) <= 126)) ? (char)(num & 0xFF) : '.')); | ||
| 333 | |||
| 334 | // print binary value | ||
| 335 | if (opt & MY_OPT_BIN) | ||
| 336 | { | ||
| 337 | if (len) printf((opt & MY_OPT_SPACE) ? ", " : ","); | ||
| 338 | for(y = 0; ((uint32_t)y) < max; y++) | ||
| 294 | { | 339 | { |
| 295 | if (len) printf((opt & MY_OPT_SPACE) ? ", " : ","); | 340 | if (opt & MY_OPT_LEBYTE) |
| 296 | for(y = 0; ((uint32_t)y) < max; y++) | 341 | byte = num >> (8*y); |
| 297 | { | 342 | else |
| 298 | if (opt & MY_OPT_LEBYTE) | 343 | byte = num >> (8*(max-y-1)); |
| 299 | byte = num >> (8*y); | 344 | if (opt & MY_OPT_LEBIT) |
| 300 | else | 345 | for(z = 0; z < 8; z++) |
| 301 | byte = num >> (8*(max-y-1)); | 346 | buff[z] = (byte & (0x01 << z)) ? '1' : '0'; |
| 302 | if (opt & MY_OPT_LEBIT) | 347 | if (!(opt & MY_OPT_LEBIT)) |
| 303 | for(z = 0; z < 8; z++) | 348 | for(z = 0; z < 8; z++) |
| 304 | buff[z] = (byte & (0x01 << z)) ? '1' : '0'; | 349 | buff[z] = (byte & (0x01 << (7-z))) ? '1' : '0'; |
| 305 | if (!(opt & MY_OPT_LEBIT)) | 350 | buff[z] = '\0'; |
| 306 | for(z = 0; z < 8; z++) | 351 | printf("%s", buff); |
| 307 | buff[z] = (byte & (0x01 << (7-z))) ? '1' : '0'; | 352 | if ((opt & MY_OPT_SPACE) && (((uint32_t)y+1) < max)) |
| 308 | buff[z] = '\0'; | 353 | printf(" "); |
| 309 | printf("%s", buff); | ||
| 310 | if ((opt & MY_OPT_SPACE) && (((uint32_t)y+1) < max)) | ||
| 311 | printf(" "); | ||
| 312 | }; | ||
| 313 | len = 1; | ||
| 314 | }; | 354 | }; |
| 315 | if (opt & MY_OPT_HEX) len = printf((len ? ((opt & MY_OPT_SPACE) ? ", 0x%0*jX" : ",0x%0*jX") : "0x%0*jX"), (unsigned)(max*2), num); | 355 | len = 1; |
| 316 | if (opt & MY_OPT_OCT) len = printf((len ? ((opt & MY_OPT_SPACE) ? ", 0%jo" : ",0%jo") : "0%jo"), num); | ||
| 317 | if (opt & MY_OPT_DEC) len = printf((len ? ((opt & MY_OPT_SPACE) ? ", %ju" : ",%ju") : "%ju"), num); | ||
| 318 | printf("\n"); | ||
| 319 | }; | 356 | }; |
| 320 | 357 | ||
| 321 | return(0); | 358 | // print hexadecimal value |
| 359 | if (opt & MY_OPT_HEX) len = printf((len ? ((opt & MY_OPT_SPACE) ? ", 0x%0*jX" : ",0x%0*jX") : "0x%0*jX"), (unsigned)(max*2), num); | ||
| 360 | |||
| 361 | // print octal value | ||
| 362 | if (opt & MY_OPT_OCT) len = printf((len ? ((opt & MY_OPT_SPACE) ? ", 0%jo" : ",0%jo") : "0%jo"), num); | ||
| 363 | |||
| 364 | // print decimal value | ||
| 365 | if (opt & MY_OPT_DEC) len = printf((len ? ((opt & MY_OPT_SPACE) ? ", %ju" : ",%ju") : "%ju"), num); | ||
| 366 | |||
| 367 | printf("\n"); | ||
| 368 | |||
| 369 | return; | ||
| 322 | } | 370 | } |
| 323 | 371 | ||
| 324 | 372 | ||
