Created
November 26, 2023 20:54
-
-
Save azat/14a515fc561eca404d9242dee257256f to your computer and use it in GitHub Desktop.
This file contains 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 <limits.h> | |
#include <stddef.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char **argv) | |
{ | |
const char * arg = argv[1]; | |
uint64_t in_host = strtoll(arg, NULL, 16); | |
uint64_t in_network = __bswap_64(in_host); | |
uint64_t out_host = 0; | |
unsigned char * data; | |
data = (unsigned char *)&in_network; | |
for (ssize_t i = sizeof(uint64_t) * (CHAR_BIT - 1), pos = 0; i >= 0; i -= CHAR_BIT) { | |
/* out_host |= (uint64_t)(data[pos++] & 0xff) << i; */ | |
/* out_host |= (uint64_t)((unsigned char)data[pos++]) << i; */ | |
printf("data[i] = %x\n", data[pos]); | |
out_host |= (uint64_t)data[pos++] << i; // bug | |
printf("out_host = 0x%lx\n", out_host); | |
} | |
uint64_t out_network; | |
data = (unsigned char *)&out_network; | |
for (ssize_t i = sizeof(uint64_t) * (CHAR_BIT - 1), pos = 0; i >= 0; i -= CHAR_BIT) { | |
data[pos++] = out_host >> i & 0xFFu; | |
} | |
printf("arg=%s, in_host=0x%lx, in_network=0x%lx, out_host=0x%lx, out_network=0x%lx\n", arg, in_host, in_network, out_host, out_network); | |
return 0; | |
} |
Author
azat
commented
Nov 26, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment