Skip to content

Instantly share code, notes, and snippets.

@austinschwartz
Created April 27, 2016 23:04
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 austinschwartz/8e3a59eae62ff318692f459fcc0575fa to your computer and use it in GitHub Desktop.
Save austinschwartz/8e3a59eae62ff318692f459fcc0575fa to your computer and use it in GitHub Desktop.
the most beautiful code that ever was
// This code is awful
// IT WORKS THOUGH :D
// High five whoevers reading this!
char *host_addr_to_string(char *addr) {
char *buf = (char*)malloc(22);
uint8_t ipb[4];
uint16_t port;
sscanf(addr, "%03hhu%03hhu%03hhu%03hhu:%05hu", &ipb[3], &ipb[2], &ipb[1], &ipb[0], &port);
sprintf(buf, "%hhu.%hhu.%hhu.%hhu:%03hu", ipb[3], ipb[2], ipb[1], ipb[0], port);
return buf;
}
// This code is also awful
char *addr_to_host_addr(const char *ip, uint16_t port) {
char *buf = (char*)malloc(20);
uint8_t ipb[4];
sscanf(ip, "%03hhu.%03hhu.%03hhu.%03hhu", &ipb[3], &ipb[2], &ipb[1], &ipb[0]);
sprintf(buf, "%03hhu%03hhu%03hhu%03hhu:%05hu", ipb[3], ipb[2], ipb[1], ipb[0], port);
return buf;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment