VB Code:
Public Function USSD_Unpacking$(ByVal DataStr As String)
Dim DataLen As Integer
Dim i%, j%, char$
Dim tmpval As Integer
Dim uss_byte_Tab() As Byte
Dim uss_bitField2$, unpackeduss_bitField$
Dim TempStr$ DataLen = Len(DataStr)
If (DataLen = 0) Then Exit Function
'STEP 1 : building of an octets array
'-> each byte contains the value of each hexa-code of packed string
DataLen = DataLen \ 2
ReDim uss_byte_Tab(0 To DataLen - 1) As Byte
j = 1
For i = 0 To DataLen - 1
char$ = Mid$(DataStr, j, 2)
uss_byte_Tab(i) = CByte("&h" & char$)
j = j + 2
Next i
'STEP 2 : building of the bit train (7-bit codes)
uss_bitField2$ = ""
For i = 0 To DataLen - 1
For j = 0 To 7
tmpval = 2 ^ j
If (CStr(uss_byte_Tab(i) And CByte(tmpval)) = tmpval) Then
uss_bitField2$ = uss_bitField2$ & "1"
Else
uss_bitField2$ = uss_bitField2$ & "0"
End If
Next j
Next i
'STEP 3 : unpacking of string
unpackeduss_bitField$ = ""
j = 0
TempStr = ""
For i = 1 To Len(uss_bitField2$)
TempStr = Mid$(uss_bitField2$, i, 1) & TempStr
'If (i Mod 7 = 0) Then
If (Len(TempStr) = 7) Then
'add bit 7 (forced to value '0') after having gathered bits 6..0
unpackeduss_bitField$ = unpackeduss_bitField$ & "0" & TempStr
j = j + 1
TempStr = ""
End If
Next i
'completion of last octet (... normally useless)
tmpval = Len(TempStr)
Select Case tmpval
Case 0
'nothing to do
Case Else
'fill with sequence of '0'
For i = tmpval To 7
TempStr = "0" & TempStr
Next i
unpackeduss_bitField$ = unpackeduss_bitField$ & TempStr
j = j + 1
End Select
'nota: i octets unpacked into j characters
'case of special filling sequence "0001101"
If ((j * 7) Mod 8 = 1) Then
TempStr = Right$(unpackeduss_bitField$, 8)
If (Left$(TempStr, 7) = "0001101") Then
unpackeduss_bitField$ = Left$(unpackeduss_bitField$, Len(unpackeduss_bitField$) - 8) & "0000000" & Right$(TempStr, 1)
End If
End If
'STEP 4 : generation of the ASCII string
TempStr = ""
For i = 1 To Len(unpackeduss_bitField$) Step 8
tmpval = 0
char$ = Mid$(unpackeduss_bitField$, i, 8)
For j = 0 To 7
If (Mid$(char$, 8 - j, 1) = "1") Then tmpval = tmpval + (2 ^ j)
Next j
TempStr = TempStr & Chr$(tmpval)
Next i
USSD_Unpacking$ = TempStr$End Function
Public Function USSD_Unpacking$(ByVal DataStr As String)
Dim DataLen As Integer
Dim i%, j%, char$
Dim tmpval As Integer
Dim uss_byte_Tab() As Byte
Dim uss_bitField2$, unpackeduss_bitField$
Dim TempStr$ DataLen = Len(DataStr)
If (DataLen = 0) Then Exit Function
'STEP 1 : building of an octets array
'-> each byte contains the value of each hexa-code of packed string
DataLen = DataLen \ 2
ReDim uss_byte_Tab(0 To DataLen - 1) As Byte
j = 1
For i = 0 To DataLen - 1
char$ = Mid$(DataStr, j, 2)
uss_byte_Tab(i) = CByte("&h" & char$)
j = j + 2
Next i
'STEP 2 : building of the bit train (7-bit codes)
uss_bitField2$ = ""
For i = 0 To DataLen - 1
For j = 0 To 7
tmpval = 2 ^ j
If (CStr(uss_byte_Tab(i) And CByte(tmpval)) = tmpval) Then
uss_bitField2$ = uss_bitField2$ & "1"
Else
uss_bitField2$ = uss_bitField2$ & "0"
End If
Next j
Next i
'STEP 3 : unpacking of string
unpackeduss_bitField$ = ""
j = 0
TempStr = ""
For i = 1 To Len(uss_bitField2$)
TempStr = Mid$(uss_bitField2$, i, 1) & TempStr
'If (i Mod 7 = 0) Then
If (Len(TempStr) = 7) Then
'add bit 7 (forced to value '0') after having gathered bits 6..0
unpackeduss_bitField$ = unpackeduss_bitField$ & "0" & TempStr
j = j + 1
TempStr = ""
End If
Next i
'completion of last octet (... normally useless)
tmpval = Len(TempStr)
Select Case tmpval
Case 0
'nothing to do
Case Else
'fill with sequence of '0'
For i = tmpval To 7
TempStr = "0" & TempStr
Next i
unpackeduss_bitField$ = unpackeduss_bitField$ & TempStr
j = j + 1
End Select
'nota: i octets unpacked into j characters
'case of special filling sequence "0001101"
If ((j * 7) Mod 8 = 1) Then
TempStr = Right$(unpackeduss_bitField$, 8)
If (Left$(TempStr, 7) = "0001101") Then
unpackeduss_bitField$ = Left$(unpackeduss_bitField$, Len(unpackeduss_bitField$) - 8) & "0000000" & Right$(TempStr, 1)
End If
End If
'STEP 4 : generation of the ASCII string
TempStr = ""
For i = 1 To Len(unpackeduss_bitField$) Step 8
tmpval = 0
char$ = Mid$(unpackeduss_bitField$, i, 8)
For j = 0 To 7
If (Mid$(char$, 8 - j, 1) = "1") Then tmpval = tmpval + (2 ^ j)
Next j
TempStr = TempStr & Chr$(tmpval)
Next i
USSD_Unpacking$ = TempStr$End Function
public static String USSDUnpacking(String dataString){
String tempStr = "", uss_bitField2 = "", unpackeduss_bitField = "";
int dataLenth;
int tmpVal;
byte[] uss_byte_Tab;
dataLenth = dataString.length();
if(dataLenth == 0)
return null;
//STEP 1 : building of an octets array
//-> each byte contains the value of each hexa-code of packed string
dataLenth = dataLenth/ 2;
uss_byte_Tab = new byte[dataLenth - 1];
int j = 0;
for (int i = 0; i< dataLenth - 1; i++){
String chara = dataString.substring(j, j+2);
Integer in = Integer.decode("0x"+ chara);
uss_byte_Tab[i] = in.byteValue();
j = j + 2;
}
//'STEP 2 : building of the bit train (7-bit codes)
for (int i = 0; i <dataLenth-1; i++){
for (int l = 0; l < 7; l++){
tmpVal = (int)Math.pow(2,l);
if((uss_byte_Tab[i] & tmpVal)==tmpVal){
uss_bitField2 = uss_bitField2 +"1";
}else{
uss_bitField2 = uss_bitField2 +"0";
}
}
}
//STEP 3 : unpacking of string
j = 0;
for(int i = 0; i < uss_bitField2.length(); i++){
tempStr = uss_bitField2.substring(i, i+1) + tempStr;
// If (i Mod 7 = 0) Then
if (tempStr.length() == 7){
// add bit 7 (forced to value '0') after having gathered bits 6..0
unpackeduss_bitField = unpackeduss_bitField + "0" + tempStr;
j = j + 1;
tempStr = "";
}
}
System.out.println("unpackeduss_bitField :: " + unpackeduss_bitField);
// 'completion of last octet (... normally useless)
tmpVal = tempStr.length();
switch(tmpVal){
case 0:
// nothing;
break;
default:
// fill with sequence of '0'
for(int i =tmpVal; i < 7; i++){
tempStr = "0" +tempStr;
}
unpackeduss_bitField = unpackeduss_bitField + tempStr;
j = j + 1;
}
//nota: i octets unpacked into j characters
//case of special filling sequence "0001101"
if ((j * 7)% 8 == 1){
tempStr = unpackeduss_bitField.substring(unpackeduss_bitField.length()-9);
if(tempStr.substring(0, 6)=="0001101"){
unpackeduss_bitField = unpackeduss_bitField.substring(0, unpackeduss_bitField.length()-9)+
"0000000" + tempStr.substring(tempStr.length()-1);
System.out.println("unpackeduss_bitField :: " + unpackeduss_bitField);
}
}
//STEP 4 : generation of the ASCII string
tempStr = "";
for(int i= 0; i < unpackeduss_bitField.length(); i+=8){
tmpVal = 0;
String chara = unpackeduss_bitField.substring(i,i+8);
for(int l = 0; l < 8; l++){
if(chara.substring(7-l , 8-l).equals("1")){
tmpVal = tmpVal+(int)Math.pow(2,l);
}
}
char dataChar = (char)tmpVal;
String dataStr = new Character(dataChar).toString();
tempStr = tempStr + dataStr;
} System.out.println("uss Sequence ::" + tempStr);
return tempStr;
}小女子 vb 小白,希望各位耐心指点 thx!!
Option ExplicitPrivate Function Unpack(ByRef src() As Long, ByRef dst() As Long, ByVal inputlen As Long) As Long
Dim a As Long
Dim b As Long
Dim k As Long
a = 0
b = 0
Do
If a >= ((inputlen \ 7) * 8 + (inputlen Mod 8)) Then
Exit Do
End If
k = a Mod 8
If 7 = k Then
b = b - 1
dst(a) = src(b) \ 2
Else
dst(a) = src(b) * (2 ^ k)
If b > 0 Then
dst(a) = dst(a) Or (src(b - 1) \ (2 ^ (8 - k)))
End If
dst(a) = dst(a) And &H7F
End If
a = a + 1
b = b + 1
Loop While 1
Unpack = a
End FunctionPrivate Function UnpackStr(ByVal s As String) As String
Dim src(0 To 140) As Long
Dim dst(0 To 160) As Long
Dim i As Long
For i = 1 To Len(s) Step 2
src(Int(i / 2)) = Val("&h" & Mid(s, i, 2))
Next i
Dim l As Long
l = Unpack(src, dst, Len(s) / 2)
Dim ss As String
ReDim a(0 To l - 1) As Byte
For i = 0 To l - 1
a(i) = dst(i) And &HFF
Next i
ss = StrConv(a, vbUnicode)
UnpackStr = ss
End FunctionPrivate Sub Command1_Click()
Debug.Print UnpackStr("E8329BFD6681EE")
End Sub俺手头有急事,不能奉陪了。