Last active
November 28, 2023 22:52
-
-
Save shymega/b8aa651b4782aea37131f97a1ce7fea1 to your computer and use it in GitHub Desktop.
hexmemcalc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
/** | |
* The MIT License (MIT) | |
* | |
* Copyright © 2022 Dom Rodriguez (shymega) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the “Software”), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
void print_help(char **argv) { | |
fprintf(stdout, "%s <start_address> <end_address>\n", argv[0]); | |
} | |
int main(int argc, char **argv) { | |
if (argc <= 1) { | |
fprintf(stdout, "Not enough arguments!\n"); | |
print_help(argv); | |
return EXIT_FAILURE; | |
} | |
if (argv[1] == NULL) { | |
fprintf(stderr, "Memory start hex address NULL!\n"); | |
return EXIT_FAILURE; | |
} | |
if (argv[2] == NULL) { | |
fprintf(stderr, "Memory end hex address: NULL!\n"); | |
return EXIT_FAILURE; | |
} | |
int MEM_BEGIN = strtol(argv[1], NULL, 16); | |
int MEM_END = strtol(argv[2], NULL, 16); | |
if (MEM_BEGIN == 0) { | |
fprintf(stderr, "String hex parsing failed [MEM_BEGIN]\n"); | |
return EXIT_FAILURE; | |
} else if (MEM_END == 0) { | |
fprintf(stderr, "String hex parsing failed [MEM_END]\n"); | |
return EXIT_FAILURE; | |
} | |
int difference_bytes = (MEM_END - MEM_BEGIN) * sizeof(int); | |
double difference_kb = difference_bytes / 1024.00; | |
double difference_mb = difference_kb / 1024.00; | |
if (difference_kb == 0.00) { | |
return EXIT_SUCCESS; | |
} else if (difference_mb == 0.00) { | |
return EXIT_SUCCESS; | |
} | |
fprintf(stdout, "kb: %.2lf\n", difference_bytes / 1024.00); | |
fprintf(stdout, "mb: %.2lf\n", difference_bytes / 1024.00 / 1024.00); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment