Skip to content

Instantly share code, notes, and snippets.

@mdmitry1
Last active December 19, 2022 21:07
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 mdmitry1/9415c6fec608ab6aa0ea6b1ba20ed06f to your computer and use it in GitHub Desktop.
Save mdmitry1/9415c6fec608ab6aa0ea6b1ba20ed06f to your computer and use it in GitHub Desktop.
Decimal, hexadecimal and binary bidirectional convertors
Compilation note: strip should be taken from /usr/bin
which strip
/usr/bin/strip
ls -ltr /c/msys64/usr/bin/strip /usr/bin/strip | sed "s/$USER/<username>/"
-rwxr-xr-x 2 <username> None 1257025 Oct 18 23:37 /usr/bin/strip
-rwxr-xr-x 2 <username> None 1257025 Oct 18 23:37 /c/msys64/usr/bin/strip
Installation instructions: https://packages.msys2.org/package/binutils?repo=msys&variant=x86_64
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(int argc, char**argv) {
if(argc < 2) {
auto s = string(*argv);
auto exe_name = s.substr(s.find_last_of("/\\") + 1);
cout << endl << "Usage: " << exe_name << " <n>" << endl << endl;
return 1;
}
auto whitespace = string(" \t");
if( 0 == strlen(argv[1]) ) {
cout << endl << "ERROR: conversion failed - empty argument" << endl << endl;
return 1;
} else {
auto arg=string(argv[1]);
if( arg.find_first_not_of(whitespace) == string::npos ) {
cout << endl << "ERROR: conversion failed - argument contains only whitespaces" << endl << endl;
return 1;
}
}
errno=0;
char *pEnd;
auto result = strtoull(argv[1],&pEnd,2);
if( errno !=0 ) {
cout << endl << "ERROR: conversion failed - " << strerror(errno) << endl << endl;
return 1;
}
if( strlen(pEnd) > 0 ) {
auto sEnd=string(pEnd);
if( sEnd.find_first_not_of(whitespace) != string::npos ) {
cout << endl << "ERROR: conversion failed - wrong input format" << endl << endl;
return 1;
}
}
cout << result << endl;
return 0;
}
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(int argc, char**argv) {
if(argc < 2) {
auto s = string(*argv);
auto exe_name = s.substr(s.find_last_of("/\\") + 1);
cout << endl << "Usage: " << exe_name << " <n>" << endl << endl;
return 1;
}
auto whitespace = string(" \t");
if( 0 == strlen(argv[1]) ) {
cout << endl << "ERROR: conversion failed - empty argument" << endl << endl;
return 1;
} else {
auto arg=string(argv[1]);
if( arg.find_first_not_of(whitespace) == string::npos ) {
cout << endl << "ERROR: conversion failed - argument contains only whitespaces" << endl << endl;
return 1;
}
}
errno=0;
char *pEnd;
auto result = strtoull(argv[1],&pEnd,2);
if( errno !=0 ) {
cout << endl << "ERROR: conversion failed - " << strerror(errno) << endl << endl;
return 1;
}
if( strlen(pEnd) > 0 ) {
auto sEnd=string(pEnd);
if( sEnd.find_first_not_of(whitespace) != string::npos ) {
cout << endl << "ERROR: conversion failed - wrong input format" << endl << endl;
return 1;
}
}
cout << hex << result << endl;
return 0;
}
#include <iostream>
#include <bitset>
#include <string>
#include <cstdlib>
#include <cstring>
#include <climits>
using namespace std;
int main(int argc, char**argv) {
if(argc < 2) {
auto s = string(*argv);
auto exe_name = s.substr(s.find_last_of("/\\") + 1);
cout << endl << "Usage: " << exe_name << " <n>" << endl << endl;
return 1;
}
auto whitespace = string(" \t");
if( 0 == strlen(argv[1]) ) {
cout << endl << "ERROR: conversion failed - empty argument" << endl << endl;
return 1;
} else {
auto arg=string(argv[1]);
if( arg.find_first_not_of(whitespace) == string::npos ) {
cout << endl << "ERROR: conversion failed - argument contains only whitespaces" << endl << endl;
return 1;
}
}
errno=0;
char *pEnd;
auto n = strtoull(argv[1],&pEnd,10);
if( errno !=0 ) {
cout << endl << "ERROR: conversion failed - " << strerror(errno) << endl << endl;
return 1;
}
if( strlen(pEnd) > 0 ) {
auto sEnd=string(pEnd);
if( sEnd.find_first_not_of(whitespace) != string::npos ) {
cout << endl << "ERROR: conversion failed - wrong input format" << endl << endl;
return 1;
}
}
if(0 == n) {
cout << n << endl;
} else {
auto binary = bitset<sizeof(long long unsigned int)*CHAR_BIT>(n).to_string();
cout<< binary.erase(0, binary.find_first_not_of('0')) << endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(int argc, char**argv) {
if(argc < 2) {
auto s = string(*argv);
auto exe_name = s.substr(s.find_last_of("/\\") + 1);
cout << endl << "Usage: " << exe_name << " <n>" << endl << endl;
return 1;
}
auto whitespace = string(" \t");
if( 0 == strlen(argv[1]) ) {
cout << endl << "ERROR: conversion failed - empty argument" << endl << endl;
return 1;
} else {
auto arg=string(argv[1]);
if( arg.find_first_not_of(whitespace) == string::npos ) {
cout << endl << "ERROR: conversion failed - argument contains only whitespaces" << endl << endl;
return 1;
}
}
errno=0;
char *pEnd;
auto result = strtoull(argv[1],&pEnd,10);
if( errno !=0 ) {
cout << endl << "ERROR: conversion failed - " << strerror(errno) << endl << endl;
return 1;
}
if( strlen(pEnd) > 0 ) {
auto sEnd=string(pEnd);
if( sEnd.find_first_not_of(whitespace) != string::npos ) {
cout << endl << "ERROR: conversion failed - wrong input format" << endl << endl;
return 1;
}
}
cout << hex << result << endl;
return 0;
}
#include <iostream>
#include <bitset>
#include <string>
#include <cstdlib>
#include <cstring>
#include <climits>
using namespace std;
int main(int argc, char**argv) {
if(argc < 2) {
auto s = string(*argv);
auto exe_name = s.substr(s.find_last_of("/\\") + 1);
cout << endl << "Usage: " << exe_name << " <n>" << endl << endl;
return 1;
}
auto whitespace = string(" \t");
if( 0 == strlen(argv[1]) ) {
cout << endl << "ERROR: conversion failed - empty argument" << endl << endl;
return 1;
} else {
auto arg=string(argv[1]);
if( arg.find_first_not_of(whitespace) == string::npos ) {
cout << endl << "ERROR: conversion failed - argument contains only whitespaces" << endl << endl;
return 1;
}
}
errno=0;
char *pEnd;
auto n = strtoull(argv[1],&pEnd,16);
if( errno !=0 ) {
cout << endl << "ERROR: conversion failed - " << strerror(errno) << endl << endl;
return 1;
}
if( strlen(pEnd) > 0 ) {
auto sEnd=string(pEnd);
if( sEnd.find_first_not_of(whitespace) != string::npos ) {
cout << endl << "ERROR: conversion failed - wrong input format" << endl << endl;
return 1;
}
}
if(0 == n) {
cout << n << endl;
} else {
auto binary = bitset<sizeof(long long unsigned int)*CHAR_BIT>(n).to_string();
cout<< binary.erase(0, binary.find_first_not_of('0')) << endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(int argc, char**argv) {
if(argc < 2) {
auto s = string(*argv);
auto exe_name = s.substr(s.find_last_of("/\\") + 1);
cout << endl << "Usage: " << exe_name << " <n>" << endl << endl;
return 1;
}
auto whitespace = string(" \t");
if( 0 == strlen(argv[1]) ) {
cout << endl << "ERROR: conversion failed - empty argument" << endl << endl;
return 1;
} else {
auto arg=string(argv[1]);
if( arg.find_first_not_of(whitespace) == string::npos ) {
cout << endl << "ERROR: conversion failed - argument contains only whitespaces" << endl << endl;
return 1;
}
}
errno=0;
char *pEnd;
auto result = strtoull(argv[1],&pEnd,16);
if( errno !=0 ) {
cout << endl << "ERROR: conversion failed - " << strerror(errno) << endl << endl;
return 1;
}
if( strlen(pEnd) > 0 ) {
auto sEnd=string(pEnd);
if( sEnd.find_first_not_of(whitespace) != string::npos ) {
cout << endl << "ERROR: conversion failed - wrong input format" << endl << endl;
return 1;
}
}
cout << result << endl;
return 0;
}
MODULES=bin2dec dec2bin bin2hex hex2bin dec2hex hex2dec
%: %.cpp
g++ -O2 -o $@ $<
strip $@
all: $(MODULES)
clean:
-rm -rf $(MODULES)
MODULES=bin2dec dec2bin bin2hex hex2bin dec2hex hex2dec
%: %.cpp
g++ -std=c++20 -O2 -o $@ $<
strip $@
all: $(MODULES)
clean:
-rm -rf $(MODULES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment