C++: How do I store a 256 bit number, and how do I convert it to hex?

后端 未结 3 1361
轻奢々
轻奢々 2021-01-28 09:10

ints only go to 32 bits, longs to 64bits... so.. what do you do when you are working with a much larger number?

Also, how easy would it be to switch between the binary

3条回答
  •  伪装坚强ぢ
    2021-01-28 09:39

    Use an array. For example:

    // Declare a structure containing an array of 4 64-bit integers
    struct uint256_t
    {
        uint64_t bits[4];
    };
    
    // Then, to convert to hex:
    uint256_t x;
    char hexstring[65];  // needs to be at least 64 hex digits + 1 for the null terminator
    sprintf(hexstring, "%016llx%016llx%016llx%016llx", x.bits[0], x.bits[1], x.bits[2], x.bits[3]);
    

提交回复
热议问题