Arash
Arash

Reputation: 609

Converting unsigned char* to uint64_t

I have converted uint64_t to unsigned char* using the following:

uint64_t in;
unsigned char *out;
sprintf(out,"%" PRIu64, in);

Now I want to do the reverse. Any idea?

Upvotes: 7

Views: 10934

Answers (1)

Carl Norum
Carl Norum

Reputation: 225252

The direct analogue to what you're doing with sprintf(3) would be to use sscanf(3):

unsigned char *in;
uint64_t out;
sscanf(in, "%" SCNu64, &out);

But probably strtoull(3) will be easier and better at error handling:

out = strtoull(in, NULL, 0);

(This answer assumes in really points to something, analogous to how out has to point to something in your example code.)

Upvotes: 10

Related Questions