Skip to content

Instantly share code, notes, and snippets.

@shymega
Last active November 28, 2023 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shymega/b8aa651b4782aea37131f97a1ce7fea1 to your computer and use it in GitHub Desktop.
Save shymega/b8aa651b4782aea37131f97a1ce7fea1 to your computer and use it in GitHub Desktop.
hexmemcalc
#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