简体   繁体   English

将unsigned char *转换为uint64_t

[英]Converting unsigned char* to uint64_t

I have converted uint64_t to unsigned char* using the following: 我已使用以下内容将uint64_t转换为unsigned char*

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

Now I want to do the reverse. 现在我想做相反的事情。 Any idea? 任何想法?

The direct analogue to what you're doing with sprintf(3) would be to use sscanf(3) : sprintf(3)所做的直接类比是使用sscanf(3)

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

But probably strtoull(3) will be easier and better at error handling: 但可能strtoull(3)在错误处理方面会更容易和更好:

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.) (这个回答假设in真正指向的东西,类似于如何out有指向您的示例代码的东西。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM