二进制内容的替换,有什么办法比较高效?
本帖最后由 bcrun 于 2013-08-04 12:56:47 编辑
Private Sub Form_Load()
Dim s As String
s = "中国abctt22"
Dim B() As Byte
B = StrConv(s, vbFromUnicode)
B = Replace(B, StrConv("abc", vbFromUnicode), StrConv("ddd", vbFromUnicode))
Dim s2 As String
s2 = StrConv(B, vbUnicode)
MsgBox s2
End Sub
Dim s As String, s2 As String或
s = "中国abctt22"
Dim B() As Byte
B = StrConv(Replace(s, "abc", "ddd"), vbFromUnicode)
s2 = StrConv(B, vbUnicode)
MsgBox s2
Dim s As String, s2 As String, i As Integer
s = "中国abctt22"
Dim B() As Byte
B = StrConv(s, vbFromUnicode)
For i = LBound(B) To UBound(B) - 2
If B(i) = Asc("a") And B(i + 1) = Asc("b") And B(i + 2) = Asc("c") Then
B(i) = Asc("d")
B(i + 1) = Asc("d")
B(i + 2) = Asc("d")
i = i + 2
End If
Next i
s2 = StrConv(B, vbUnicode)
MsgBox s2
LPWSTR ReplaceW(LPWSTR lpExpression,//
LPWSTR lpFind,//
LPWSTR lpReplacewith)//
{
DWORDi = 0;
DWORDj = 0;
DWORDELenB = 0;
DWORDFLenB = 0;
DWORDRLenB = 0;
DWORDisExec = 0;
LPWSTRretString;
ELenB = wcslen(lpExpression);
FLenB = wcslen(lpFind);
RLenB = wcslen(lpReplacewith);
retString = (LPWSTR)SKCreateMemory(ELenB*sizeof(WCHAR)+sizeof(WCHAR));
CopyStringW(retString, lpExpression, ELenB);
while((int)retString[i]!=0){
isExec = 1;
for(j=0;j<FLenB;j++){
if((WCHAR)retString[i+j]!=(WCHAR)lpFind[j]){
isExec = 0;
break;
}
}
if(isExec==1){
if(FLenB>RLenB){
CopyStringW(retString + i, lpReplacewith, RLenB);
CopyStringW(retString + (i + RLenB), retString + (i + FLenB), ELenB - (i + FLenB));
ELenB = ELenB - (FLenB - RLenB);
retString[ELenB] = 0;
retString = (LPWSTR)ZoomTextBufferW(retString, ELenB);
}else if(FLenB<RLenB){
ELenB = ELenB + (RLenB - FLenB);
retString = (LPWSTR)ZoomTextBufferW(retString, ELenB);
CopyStringRW(retString + (i + RLenB), retString + (i + FLenB), ELenB - (i + RLenB));
CopyStringW(retString + i, lpReplacewith, RLenB);
}else{
CopyStringW(retString + i, lpReplacewith, RLenB);
}
}
i++;
}
return NULL;
}