现在的位置: 首页 > 综合 > 正文

根据转换后的数据流错误位置判断源文件错误位置

2013年01月31日 ⁄ 综合 ⁄ 共 6348字 ⁄ 字号 评论关闭

前几天在论坛上问了个问题,跟贴得不少,没有一个人回答问题,我宁愿相信,是他们没弄懂需求。这年头搞技术的人,都有点酸酸的味道,又不是专利,为什么不能多点共享精神呢?

需求如下

做一个信号处理的东西。信号格式如下
信号   信号名{
0000120"string"   #   comment  
002D0"string"   #   comment  
006e0"string"   #   comment  
}
转换规则:去掉信号,去掉信号名,去掉注释(#到换行),"   "(字符串)内字符串转换为16进制,
经过转换,信号变为"0100000000000000001076000000C6868696E00"类似的格式。
现在要实现一个功能,给出转换后的位置(比如第2位),如何查找转换前(原信号)的位置。

问题地址如下

http://topic.csdn.net/u/20071128/16/6d5bb2e1-83de-4f20-a60d-e99f9c9c87b2.html

代码如下:

 


    
/**
     * get length before data<br>
     * 
     * 
@param String
     *            data
     * 
@return length
     * 
@throws N/A
     * 
@author tianrenliang
     * 
@since V0.1(2007/11/28)
     
*/
    
private int getLengthBeforeData(String strdata) {
        
int iLengthBeforeData = 0;
        
int ibeforeStartTagLength = 0;
        String strsubString 
= null;
        
if (strdata.indexOf(strStartTag) != -1) {
            strsubString 
= 
                strdata.substring(
0, strdata.indexOf(strLineSeparator));
        }
        ibeforeStartTagLength 
= strsubString.length();
        
int iseparatorLength = strLineSeparator.length();
        
// length is before line separator and separator length
        iLengthBeforeData = ibeforeStartTagLength + iseparatorLength;
        
return iLengthBeforeData;
    }

    /**
     * get rid of start tag and end tag <br>
     * 
@param avp data
     * 
@return content
     * 
@throws N/A
     * 
@author tianrenliang
     * 
@since V0.1(2007/11/28)
     
*/
    
private String getContent(String strContent)
    {
        
        
int iLengthBeforeData = getLengthBeforeData(strDecodeData);
        String content 
= null;
        
if(null == strContent )
        {
            
return null;
        }
        
//get the string between line change tag
        if(strContent.indexOf(strStartTag)!=-1&&
                strContent.indexOf(strEndTag)
!=-1)
        {
            content 
= strContent.substring(
                    iLengthBeforeData,
                        strContent.indexOf(strEndTag));
            content 
= content.replace(strStartTag, "");
        }
        
else
        {
            
return null;
        }
        
return content;
    }
    
    
/**
     * convert a string to hex string <br>
     * 
@param String data
     * 
@return hexString
     * 
@throws N/A
     * 
@author tianrenliang
     * 
@since V0.1(2007/11/28)
     
*/
    
private String stringToHexString(String strPart) {
        String hexString 
= "";
        
for (int i = 0; i < strPart.length(); i++) {
            
int ch = (int) strPart.charAt(i);
            String strHex 
= Integer.toHexString(ch);
            hexString 
= hexString + strHex;
        }
        
return hexString;
    }

    /**
     * get the error position in file  <br>
     * 
@param int iErrorPosition
     * 
@return iPosition
     * 
@throws N/A
     * 
@author tianrenliang
     * 
@since V0.1(2007/11/28)
     
*/
    
public int getErrorPosition(int iErrorPosition){
        String strDecodeData 
= getDecodeDataInfo();
        String strData 
= getNullFormatData(strDecodeData);
        
int iDataErrorPosition = setErrorPosition(iErrorPosition);
        String strContent 
= getContent(strDecodeData);
        
int iLengthBeforeData = getLengthBeforeData(strDecodeData);
        
int iPosition = 0;
        
int iOtherLength = 0;
        
int iStringLength = 0;
        String strDataBeforeErrorPosition 
= 
            strData.substring(
0,iDataErrorPosition);
        
int i = 0;
        
int iStringPosition = 0;
        
int iInString =0;
        
while(i<strDataBeforeErrorPosition.length())
        {
            
//if ErrorData in this position equals to content in this position
            if(strDataBeforeErrorPosition.charAt(i)
                    
==strContent.charAt(i+iOtherLength))
            {
                i
++;
            }
            
//count the length of space part
            else if(strContent.charAt(i+iOtherLength)==chSpaceTag)
            {
                iOtherLength
++;
            }
            
//count the length of comment part
            else if(strContent.charAt(i+iOtherLength)==chrCommentTag)
            {
                
while(strContent.charAt(i+iOtherLength)!=' ')
                {
                    iOtherLength 
++;
                }
                iOtherLength 
++;
            }
            
//count the length of string part
            else if(strContent.charAt(i+iOtherLength)==stringTag)
            {
                iInString 
= i;
                
int iLength = 0;
                
int iStartPosition = 0;
                
int iEndPosition = 0;
                iOtherLength
++;
                iStartPosition 
= iInString+iOtherLength;
                
//get the length of string
                while(strContent.charAt(iInString+iOtherLength+iLength)!=
                    stringTag)
                {
                    iLength
++;
                }
                iEndPosition 
= iStartPosition+iLength;
                String string 
= strContent.substring(iStartPosition,
                                                            iEndPosition);
                string 
= stringToHexString(string);//convert this part to hex
                iLength = string.length();
                
//compare hex string part with strDataBeforeErrorPosition
                while(iStringPosition<string.length()
                        
&&iInString<strDataBeforeErrorPosition.length())
                {
                    
if(string.charAt(iStringPosition)==
                            strDataBeforeErrorPosition.charAt(iInString))
                    {
                        iInString
++;
                        iStringPosition
++;//if equal,compare next position
                        iLength--;
                    }
                    
else
                    {
                        iInString
++;//else compare string's next position
                    }
                }
                iStringLength 
= iStringPosition/2;//convert length to byte
                if(iLength>0)//if end position in string
                {
                    iOtherLength 
= iOtherLength-iStringPosition+iStringLength;
                }
                
else//if end position not in string ,need to add'"' length 1
                {
                    iOtherLength 
= iOtherLength-iStringPosition+iStringLength+1;
                }
                i 
= i+iStringPosition;
            }
        }
        
//because index is from 0,so position need to minus 1 from length.
        iPosition =i+iOtherLength+iLengthBeforeData;

        return iPosition;
    }
    

 

唯一的问题是,写了个大函数,远远超过50行。有空的时候把它拆了。

抱歉!评论已关闭.