2016-01-18 68 views
1

我有以下的文字包括控制字符,请查看图像,当我从记事本++复制这个问题如何从二进制文件读取纯文本?

enter image description here

有什么办法抢只是“HIEUDT”字符串粘贴这些控制字符不显示在这篇文章中?我不知道有控制字符

字符串函数在另一种语言,也许我会使用一个正则表达式开始与\a性格和0x04的想法,但在CI没有经验吧

+1

这是二进制格式。它可能是明确的,具有特定的结构。如果是这样,它应该相应地阅读 - 解析。你尝试过吗?正则表达式可能工作,但这不完全正确的方法。 – TNW

+0

'BEL'应该是'0x07','DC4'应该是'0x14'。使用[strok](http://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm)查找分隔符。或者在你的情况下更好,更安全,通过你的阵列寻找BEL,然后收集字符,直到DC4 – LPs

+0

我从套接字消息中得到这个文本,是否有任何建议给我 – Ryo

回答

1
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); 
    } 
3

您可以使用标准的字符串函数这样的:

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

1
#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; 
}