有个想法作为参考 1)把他们读入内存 把文本文件内容读取TextBox: Dim TempFile As Long Dim LoadBytes() As ByteTempFile=FreeFile Open 文件名 For Binary As #TempFile Redim LoadBytes(1 To Lof(TempFile)) As Byte Get #TempFile,,LoadBytes Close TempFileText1.Text=StrConv(LoadBytes,vbUniCode)2)转换成byte数组 Option ExplicitPrivate Sub Command1_Click() Dim a As String Dim b() As Byte a = "法拉卡拉法阿阿萨阿啊啊啊啊啊啊啊;" Dim i As Integer i = 1 Dim j As Integer j = -1 Dim l As Long
Do While i <= Len(a) l = Asc(Mid(a, i, 1)) i = i + 1 If l < 0 Then l = l + 65536 j = j + 2 ReDim Preserve b(j) b(j - 1) = (l \ 256) Mod 256 b(j) = l Mod 256 Else j = j + 1 ReDim Preserve b(j) b(j) = l End If Loop For i = 0 To UBound(b) Debug.Print b(i) Next iEnd Sub 3)根据公式转换 这是徐景周的c代码 void Big2GB::BigtoGB() { CalLineNumber(); if(m_text=="\n") return; for(int Line=0;Line<GetLineNumber();Line++) { BYTE* SourceSTR; SourceSTR=(BYTE* )((LPCTSTR)strLine[Line]); int size=strLine[Line].GetLength(); if(size) { BYTE* DestSTR=new BYTE[size+1]; unsigned i=0; do { // is English 是英文字符 if(SourceSTR[i]<0xA1||(SourceSTR[i+1]<0x40)) { DestSTR[i]=SourceSTR[i]; i++; } else //是BIG5的汉字码 { DestSTR[i+1]=HIBYTE(pBIGTable[(SourceSTR[i]-0xA1)*0xBF+SourceSTR[i+1]-0x40]); DestSTR[i]=LOBYTE(pBIGTable[(SourceSTR[i]-0xA1)*0xBF+SourceSTR[i+1]-0x40]); i+=2; } } while(i<strlen((char*)(SourceSTR))); DestSTR[i]='\0'; strLine[Line]=DestSTR; delete DestSTR; } } }void Big2GB::GBtoBig() { CalLineNumber(); if(m_text=="\n") return; for(int Line=0;Line<GetLineNumber();Line++) { BYTE* SourceSTR; SourceSTR=(BYTE* )((LPCTSTR)strLine[Line]); int size=strLine[Line].GetLength(); if(size) { BYTE* DestSTR=new BYTE[size+1]; unsigned i=0; do { // is English 是英文字符 if(SourceSTR[i]<0xA1||(SourceSTR[i+1]<0xA1)) { DestSTR[i]=SourceSTR[i]; i++; } else if(SourceSTR[i]>0xA1&&SourceSTR[i]<0xB0) //是GB2312的汉字码 { DestSTR[i+1]=HIBYTE(pGBTable[(SourceSTR[i]-0xA1)*0x5E+SourceSTR[i+1]-0xA1]); DestSTR[i]=LOBYTE(pGBTable[(SourceSTR[i]-0xA1)*0x5E+SourceSTR[i+1]-0xA1]); i+=2; } else { DestSTR[i+1]=HIBYTE(pGBTable[(SourceSTR[i]-0xA7)*0x5E+SourceSTR[i+1]-0xA1]); DestSTR[i]=LOBYTE(pGBTable[(SourceSTR[i]-0xA7)*0x5E+SourceSTR[i+1]-0xA1]); i+=2; } } while(i<strlen((char*)(SourceSTR))); DestSTR[i]='\0'; strLine[Line]=DestSTR; delete DestSTR; } }} 4)把byte数组转换成字符串 Dim strC as string Dim intI as long Open file fot binary as #1 ReDim bytbuf(1 To LOF(1)) As Byte Get #1, , bytbuf() Close #1 '将byte数组转化成string strC=bytbuf() pring strC ’将byte数组放为0xC3的形式。 For intI = LBound(bytbuf) To UBound(bytbuf) If Len(Hex(bytbuf(intI))) = 1 Then bytbuf(intI) = "0x0" & Hex(bytbuf(intI)) Else bytbuf(intI) = "0x" & Hex(bytbuf(intI)) End If DoEvents Next intI 5)写入文件 把TextBox内容写入文本文件: Dim TempFile As Long Dim SaveBytes() As ByteSaveBytes=StrConv(Text1.Text,vbFromUniCode)TempFile=FreeFile Open 文件名 For Binary As #TempFile Put #TempFile,,SaveBytes Close TempFile
Function BigToGB(BigStr As String) As String Dim I As Long, Y As Long Dim BigByte() As Byte Dim GbByte() As Byte
If BigStr = "" Then BigToGB = "" Exit Function End If
BigByte = StrConv(BigStr, vbFromUnicode) Y = UBound(BigByte) ReDim GbByte(0 To Y) For I = 0 To Y If I = Y Then GbByte(I) = BigByte(I) Exit For End If If BigByte(I) < &HA1 Or BigByte(I + 1) < &H40 Then GbByte(I) = BigByte(I) Else GbByte(I) = PBigType(BigByte(I), BigByte(I + 1)).loChar GbByte(I + 1) = PBigType(BigByte(I), BigByte(I + 1)).hiChar I = I + 1 End If Next I BigToGB = StrConv(GbByte, vbUnicode) Erase GbByte End Function'//GB=>Big Function GBToBig(GBStr As String) As String Dim I As Long, Y As Long Dim GbByte() As Byte Dim BigByte() As Byte
If GBStr = "" Then GBToBig = "" Exit Function End If
GbByte = StrConv(GBStr, vbFromUnicode) Y = UBound(GbByte) ReDim BigByte(0 To Y)
For I = 0 To Y If I = Y Then BigByte(I) = GbByte(I) Exit For End If If GbByte(I) < &HA1 Or GbByte(I + 1) < &HA1 Then BigByte(I) = GbByte(I) Else If GbByte(I) < &HB0 And GbByte(I + 1) >= &HA1 Then BigByte(I) = PGbType(GbByte(I) + 6, GbByte(I + 1)).loChar BigByte(I + 1) = PGbType(GbByte(I) + 6, GbByte(I + 1)).hiChar Else BigByte(I) = PGbType(GbByte(I), GbByte(I + 1)).loChar BigByte(I + 1) = PGbType(GbByte(I), GbByte(I + 1)).hiChar End If I = I + 1 End If Next I GBToBig = StrConv(BigByte, vbUnicode) Erase BigByte End Function
我急需gb 与 big5 的转换代码
我急需gb 与 big5 的转换代码
我的mail:[email protected]
1)把他们读入内存
把文本文件内容读取TextBox:
Dim TempFile As Long
Dim LoadBytes() As ByteTempFile=FreeFile
Open 文件名 For Binary As #TempFile
Redim LoadBytes(1 To Lof(TempFile)) As Byte
Get #TempFile,,LoadBytes
Close TempFileText1.Text=StrConv(LoadBytes,vbUniCode)2)转换成byte数组
Option ExplicitPrivate Sub Command1_Click()
Dim a As String
Dim b() As Byte
a = "法拉卡拉法阿阿萨阿啊啊啊啊啊啊啊;"
Dim i As Integer
i = 1
Dim j As Integer
j = -1
Dim l As Long
Do While i <= Len(a)
l = Asc(Mid(a, i, 1))
i = i + 1
If l < 0 Then
l = l + 65536
j = j + 2
ReDim Preserve b(j)
b(j - 1) = (l \ 256) Mod 256
b(j) = l Mod 256
Else
j = j + 1
ReDim Preserve b(j)
b(j) = l
End If
Loop
For i = 0 To UBound(b)
Debug.Print b(i)
Next iEnd Sub
3)根据公式转换
这是徐景周的c代码
void Big2GB::BigtoGB()
{
CalLineNumber(); if(m_text=="\n")
return;
for(int Line=0;Line<GetLineNumber();Line++)
{
BYTE* SourceSTR;
SourceSTR=(BYTE* )((LPCTSTR)strLine[Line]);
int size=strLine[Line].GetLength();
if(size)
{
BYTE* DestSTR=new BYTE[size+1];
unsigned i=0;
do
{
// is English 是英文字符
if(SourceSTR[i]<0xA1||(SourceSTR[i+1]<0x40))
{
DestSTR[i]=SourceSTR[i];
i++;
}
else //是BIG5的汉字码
{
DestSTR[i+1]=HIBYTE(pBIGTable[(SourceSTR[i]-0xA1)*0xBF+SourceSTR[i+1]-0x40]);
DestSTR[i]=LOBYTE(pBIGTable[(SourceSTR[i]-0xA1)*0xBF+SourceSTR[i+1]-0x40]);
i+=2;
}
}
while(i<strlen((char*)(SourceSTR)));
DestSTR[i]='\0'; strLine[Line]=DestSTR;
delete DestSTR;
}
}
}void Big2GB::GBtoBig()
{
CalLineNumber(); if(m_text=="\n")
return;
for(int Line=0;Line<GetLineNumber();Line++)
{
BYTE* SourceSTR;
SourceSTR=(BYTE* )((LPCTSTR)strLine[Line]);
int size=strLine[Line].GetLength();
if(size)
{
BYTE* DestSTR=new BYTE[size+1];
unsigned i=0;
do
{
// is English 是英文字符
if(SourceSTR[i]<0xA1||(SourceSTR[i+1]<0xA1))
{
DestSTR[i]=SourceSTR[i];
i++;
}
else if(SourceSTR[i]>0xA1&&SourceSTR[i]<0xB0) //是GB2312的汉字码
{
DestSTR[i+1]=HIBYTE(pGBTable[(SourceSTR[i]-0xA1)*0x5E+SourceSTR[i+1]-0xA1]);
DestSTR[i]=LOBYTE(pGBTable[(SourceSTR[i]-0xA1)*0x5E+SourceSTR[i+1]-0xA1]);
i+=2;
}
else
{
DestSTR[i+1]=HIBYTE(pGBTable[(SourceSTR[i]-0xA7)*0x5E+SourceSTR[i+1]-0xA1]);
DestSTR[i]=LOBYTE(pGBTable[(SourceSTR[i]-0xA7)*0x5E+SourceSTR[i+1]-0xA1]);
i+=2;
}
}
while(i<strlen((char*)(SourceSTR)));
DestSTR[i]='\0'; strLine[Line]=DestSTR;
delete DestSTR;
}
}}
4)把byte数组转换成字符串
Dim strC as string
Dim intI as long
Open file fot binary as #1
ReDim bytbuf(1 To LOF(1)) As Byte
Get #1, , bytbuf()
Close #1
'将byte数组转化成string
strC=bytbuf()
pring strC
’将byte数组放为0xC3的形式。
For intI = LBound(bytbuf) To UBound(bytbuf)
If Len(Hex(bytbuf(intI))) = 1 Then
bytbuf(intI) = "0x0" & Hex(bytbuf(intI))
Else
bytbuf(intI) = "0x" & Hex(bytbuf(intI))
End If
DoEvents
Next intI
5)写入文件
把TextBox内容写入文本文件:
Dim TempFile As Long
Dim SaveBytes() As ByteSaveBytes=StrConv(Text1.Text,vbFromUniCode)TempFile=FreeFile
Open 文件名 For Binary As #TempFile
Put #TempFile,,SaveBytes
Close TempFile
Dim I As Long, Y As Long
Dim BigByte() As Byte
Dim GbByte() As Byte
If BigStr = "" Then
BigToGB = ""
Exit Function
End If
BigByte = StrConv(BigStr, vbFromUnicode)
Y = UBound(BigByte)
ReDim GbByte(0 To Y)
For I = 0 To Y
If I = Y Then
GbByte(I) = BigByte(I)
Exit For
End If
If BigByte(I) < &HA1 Or BigByte(I + 1) < &H40 Then
GbByte(I) = BigByte(I)
Else
GbByte(I) = PBigType(BigByte(I), BigByte(I + 1)).loChar
GbByte(I + 1) = PBigType(BigByte(I), BigByte(I + 1)).hiChar
I = I + 1
End If
Next I
BigToGB = StrConv(GbByte, vbUnicode)
Erase GbByte
End Function'//GB=>Big
Function GBToBig(GBStr As String) As String
Dim I As Long, Y As Long
Dim GbByte() As Byte
Dim BigByte() As Byte
If GBStr = "" Then
GBToBig = ""
Exit Function
End If
GbByte = StrConv(GBStr, vbFromUnicode)
Y = UBound(GbByte)
ReDim BigByte(0 To Y)
For I = 0 To Y
If I = Y Then
BigByte(I) = GbByte(I)
Exit For
End If
If GbByte(I) < &HA1 Or GbByte(I + 1) < &HA1 Then
BigByte(I) = GbByte(I)
Else
If GbByte(I) < &HB0 And GbByte(I + 1) >= &HA1 Then
BigByte(I) = PGbType(GbByte(I) + 6, GbByte(I + 1)).loChar
BigByte(I + 1) = PGbType(GbByte(I) + 6, GbByte(I + 1)).hiChar
Else
BigByte(I) = PGbType(GbByte(I), GbByte(I + 1)).loChar
BigByte(I + 1) = PGbType(GbByte(I), GbByte(I + 1)).hiChar
End If
I = I + 1
End If
Next I
GBToBig = StrConv(BigByte, vbUnicode)
Erase BigByte
End Function
非常感谢jennyvenus的帮助还有一个问题:怎么给分啊?