Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting unsigned char* to uint64_t

Tags:

c

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?

like image 229
Arash Avatar asked Sep 10 '13 00:09

Arash


1 Answers

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.)

like image 114
Carl Norum Avatar answered Sep 30 '22 09:09

Carl Norum