简体   繁体   English

C 将 char* 转换为 uint8_t []

[英]C Convert char* to uint8_t []

I'm working on a program that receives an encrypted string.我正在开发一个接收加密字符串的程序。 For this I'm using tiny-AES-c library.为此,我使用的是tiny-AES-c库。

The encrypted string I'm receiving is char* .我收到的加密字符串是char* In order to use the string with the function AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length) I will need to convert the string to uint8_t [] .为了将字符串与 function AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length)一起使用,我需要将字符串转换为uint8_t []

This is how the tiny-AES-c library likes the format, as seen in their test.c :这就是tiny-AES-c库喜欢这种格式的方式,如他们的test.c 所示

uint8_t in[]  = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a };

I've been told this can be achieved using strtoul .我被告知这可以使用strtoul来实现。 I've researched this function quite a bit but can't seem to find any questions/documentation/examples related to the issue I'm having.我已经对此 function 进行了相当多的研究,但似乎找不到与我遇到的问题相关的任何问题/文档/示例。

My question being, how can I convert the contents of a char* to an uint8_t [] array, in the format like the snippet above?我的问题是,如何将char*的内容转换为uint8_t []数组,格式类似于上面的代码片段?

This could be as simple as:这可能很简单:

char* buf = "test";
size_t length = 5;

AES_CBC_decrypt_buffer(ctx, (uint8_t*) buf,  (uint32_t) length)

uint8_t and char are largely the same thing for binary data, same as size_t and uint32_t are compatible for values under 2^32.对于二进制数据, uint8_tchar在很大程度上是相同的,因为size_tuint32_t与 2^32 以下的值兼容。

C99 code that uses the integer types will use things like uint8_t , but older C code, or code with no C99 dependency, will use char .使用integer 类型的 C99 代码将使用uint8_t之类的内容,但较旧的 C 代码或不依赖 C99 的代码将使用char They basically mean the same thing.它们基本上是同一个意思。

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

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