我有一个程序以下列格式返回数据:
<CFData 0x1001219c0 [0x7fff7027aee0]>{length = 20, capacity = 20, bytes = 0x8deead13b8ae7057f6a629fdaae5e1200bcb8cf5}
Run Code Online (Sandbox Code Playgroud)
我需要提取8deead13b8ae7057f6a629fdaae5e1200bcb8cf5(是的,减去0x).我尝试使用sscanf并传递一些正则表达式,但我对此没有任何线索.
知道怎么做吗?代码片段表示赞赏.
您可以使用strstr()在输入字符串中定位"bytes = 0x"并复制字符串的其余部分(从"bytes = 0x"结尾),除了最后一个字符:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char* s = "<CFData 0x1001219c0 [0x7fff7027aee0]>{length = 20, "
"capacity = 20, "
"bytes = 0x8deead13b8ae7057f6a629fdaae5e1200bcb8cf5}";
char* value = 0;
const char* begin = strstr(s, "bytes = 0x");
if (begin)
{
begin += 10; /* Move past "bytes = 0x" */
value = malloc(strlen(begin)); /* Don't need 1 extra for NULL as not
copy last character from 'begin'. */
if (value)
{
memcpy(value, begin, strlen(begin) - 1);
*(value + strlen(begin) - 1) = 0;
printf("%s\n", value);
free(value);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)