Pre-processing hex string C for number __uint128

Is there any C preprocessing string processing that can be used to extract a substring from a given string?

I want to split a hex string representing the number __uint128 into two hex 64 bit chunks to produce a 128 bit number for a 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 );

      

+3


source to share


1 answer


The C preprocessor cannot decompose tokens into smaller markers, although it can replace them entirely in the special case where they are macro names. So you can't use it to physically separate hex digits that you don't predict in advance.



You can use a preprocessor to convert a hexadecimal numeric string to a C string, and perhaps then wrap that in a conversion function, for example strtoull()

(if that turns out to be appropriate). However, if this function was appropriate, then you could simply use the hex string as is, or insert a suffix into it ULL

.

+2


source







All Articles