我有以下的文字包括控制字符,请查看图像,当我从记事本++复制这个问题如何从二进制文件读取纯文本?
有什么办法抢只是“HIEUDT”字符串粘贴这些控制字符不显示在这篇文章中?我不知道有控制字符
字符串函数在另一种语言,也许我会使用一个正则表达式开始与\a
性格和0x04
的想法,但在CI没有经验吧
我有以下的文字包括控制字符,请查看图像,当我从记事本++复制这个问题如何从二进制文件读取纯文本?
有什么办法抢只是“HIEUDT”字符串粘贴这些控制字符不显示在这篇文章中?我不知道有控制字符
字符串函数在另一种语言,也许我会使用一个正则表达式开始与\a
性格和0x04
的想法,但在CI没有经验吧
int i=0, j=0;
while ((data[i] != 0x07) && (i<dataLen))
i++;
char *subString = malloc((dataLen-i)+1);
if (subString != NULL)
{
while ((data[i] != 0x14) && (i<dataLen))
subString[j++] = data[i++];
if (i<dataLen)
{
subString[j] = '\0';
printf("%s", subString);
}
else
{
printf("Delimiters not found");
}
free (subString);
}
您可以使用标准的字符串函数这样的:
char *start = memchr(data, '\x07', datalen);
start++;
char *end = memchr(start, '\x14', datalen-(start-data));
int len = end - start;
char str[len+1];
memcpy(str, data, len);
str[len] = '\0';
你当然必须检查是否memchr
不返回每次NULL
。
#include <stdio.h>
#include <stdlib.h>
int extractStr(char *out,const char *in, char Delim1, char Delim2){
int n=0;
while(*in){
if(*in==Delim1){ // first delimiter found?
in++;
while(in[n]){
if(in[n]==Delim2){ // second delimiter found?
if(out) out[n]=0; // null terminate out if provided
return n;
}else{
if(out)
out[n]= in[n]; // copy back to out if provided
}
n++;
}
in+=n;
}
in++;
}
return 0;
}
int main(){
char *buff;
int n;
char str[]="jksdqsqd sqjhd\bresult\x04qsjhgsdsqs";
n=extractStr(NULL,str,'\b',0x04);
if(n){
buff=malloc(n);
n=extractStr(buff,str,'\b',0x04);
printf("'%s'\n",buff);
free(buff);
}else
printf("string not found\n");
return 0;
}
这是二进制格式。它可能是明确的,具有特定的结构。如果是这样,它应该相应地阅读 - 解析。你尝试过吗?正则表达式可能工作,但这不完全正确的方法。 – TNW
'BEL'应该是'0x07','DC4'应该是'0x14'。使用[strok](http://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm)查找分隔符。或者在你的情况下更好,更安全,通过你的阵列寻找BEL,然后收集字符,直到DC4 – LPs
我从套接字消息中得到这个文本,是否有任何建议给我 – Ryo