kaszub4u
kaszub4u

Reputation: 853

Pre-process C hexadecimal string for __uint128 number

Is there a C pre-processor string manipulation that could be used to extract substring from given string ?

I want to divide hexadecimal string representing __uint128 number into two hexadecimal 64bit chunks in order to produce 128bit number for given type.

As in pseudocode:

#include <inttypes.h>
#include <ctype.h>

#define UINT128_C(X)   // extraxt hi (0x == 2) + (ffffffffffffffff == 16) == 18
                       // extract lo (ffffffffffffffff == 16) 
                       // prepend lo with string "0x"                     == 18
                       // (((uint128_t)(hi) << 64) | (uint128_t)(lo))

typedef __uint128_t     uint128_t;

uint128_t x;

x = UINT128_C( 0xffffffffffffffffffffffffffffffff );

Upvotes: 4

Views: 426

Answers (1)

John Bollinger
John Bollinger

Reputation: 181199

The C preprocessor cannot decompose tokens into smaller tokens, though it can replace them altogether in the special case that they are macro names. Thus, you cannot use it to physically split hexadecimal digit strings that you do not predict in advance.

You can use the preprocessor to convert the hexadecimal digit string into a C string, and perhaps then to wrap that in a conversion function, such as strtoull() (if that happened to be appropriate). If that function in particular were suitable, however, then you could also just use the the hex string as-is, or by pasting a ULL suffix onto it.

Upvotes: 3

Related Questions