两个十六进制相加怎么运算?
如本标题。
读取出来~VB6会很贴心的帮你转成10进位~再相加就好了~
写回到档案会比较麻烦~因为牵扯到的MSB和LSB的的问题~这问题用VB的比较麻烦~要慢慢算~若用C就用移位互相做And~记得是这样~
[ 本帖最后由 wube 于 2011-6-8 12:40 编辑 ]
Option Explicit Dim TitleTemp() As Byte, FilePath As String, FileName As String Dim fl As Long, Status As Boolean Private Sub cmdCommand1_Click() Dim FileNum As Integer, fl As Long On Error GoTo ErrorHandling FileNum = FreeFile: FilePath = "": txtText1.Text = "": txtText1.ToolTipText = "" ProgressBar1.Value = 0 Dialog1.ShowOpen FilePath = Dialog1.FileName If FilePath <> "" Then fl = FileLen(FilePath) txtText1.Text = FilePath txtText1.ToolTipText = txtText1.Text Status = LoadFile(fl) If Status = True Then MsgBox "LoadFile OK !" End If cmdCommand1.Enabled = False cmdCommand2.Enabled = True Else FilePath = "": txtText1.Text = "": txtText1.ToolTipText = "" End If Exit Sub ErrorHandling: Status = False ': MsgBox "Error !" Call ErrorWriteBuff(FileName, 0, "cmdCommand1_Click", Err.Number, Err.Description, "系统讯息") Resume Next End Sub Private Sub cmdCommand2_Click() Dim S1 As Currency, S2 As Currency, f As Currency, t1 As Currency '计时用 Dim FileNum As Integer, i As Long, NewFileName As String, ErrFileName As String, Path As String On Error GoTo ErrorHandling NewFileName = "": ErrFileName = "": cmdCommand1.Enabled = False StatusBar1.Panels(1).Text = "Transform Process ..." Call QueryPerformanceFrequency(f) '计时 Call QueryPerformanceCounter(S1) '计时 FileName = Trim(Mid$(FilePath, InStrRev(FilePath, "\") + 1, Len(FilePath) - InStrRev(FilePath, "\"))) Path = Trim(Mid$(FilePath, 1, Len(FilePath) - Len(FileName))) NewFileName = "New_" & FileName ErrFileName = "Err_" & FileName Status = Transform(Path, NewFileName, ErrFileName) Call QueryPerformanceCounter(S2) '计时 t1 = (S2 - S1) / f '计时 If Status = True Then MsgBox "Finish !" & vbCrLf & "Spend Time : " & fl \ t1 & " (Line/Second)" End If cmdCommand1.Enabled = True cmdCommand2.Enabled = False Exit Sub ErrorHandling: Status = False ': MsgBox "Error !" Call ErrorWriteBuff(FileName, 0, "cmdCommand2_Click", Err.Number, Err.Description, "系统讯息") Resume Next End Sub Private Sub Form_Initialize() ErrorCount = 0: ReDim ErrorData(ErrorCount): ReDim TitleTemp(0): fl = 0: Status = False: FilePath = "" End Sub Private Sub Form_Load() If App.PrevInstance Then '避免程式执行两次以上 Call MsgBox("This program has been executed", vbCritical, "Warning") Unload Me End If frmMain.Caption = frmMain.Caption & " Ver " & App.Major & "." & App.Minor & "." & App.Revision & ".1" SetCurrentDirectory App.Path cmdCommand1.Enabled = True cmdCommand2.Enabled = False ProgressBar1.Max = 100 ProgressBar1.Min = 0 ProgressBar1.Value = 0 End Sub Private Function LoadFile(fl As Long) As Boolean Dim FileNum As Integer On Error GoTo ErrorHandling FileNum = FreeFile: FileName = "" ReDim TitleTemp(fl - 1) Open FilePath For Binary As #FileNum '1秒 抓档案全部资料 Get #FileNum, 1, TitleTemp() Close #FileNum LoadFile = True Exit Function ErrorHandling: LoadFile = False ': MsgBox "Error !" Call ErrorWriteBuff(FileName, 0, "LoadFile", Err.Number, Err.Description, "系统讯息") Resume Next End Function Private Function Transform(Path As String, NewFileName As String, ErrFileName As String) As Boolean Dim i As Long, DataString() As Byte On Error GoTo ErrorHandling '重点开始---------------------------------------------------------------- For i = 0 To UBound(TitleTemp) '???????????????????????????????????????????????????????????????????? Call DataStringProcess(TitleTemp(), i) '???????????????????????????????????????????????????????????????????? If i Mod 1000 = 0 Then If i <> 0 Then ProgressBar1.Value = (i / fl) * 100 ElseIf fl - i < 100 Then ProgressBar1.Value = 100 End If Next i '------------------------------------------------------------------------ Status = WriteFile(Path, NewFileName, ErrFileName, DataString()) If Status <> True Then MsgBox "WriteFile Error !": Exit Function Transform = True Exit Function ErrorHandling: Transform = False ': MsgBox "Error !" Call ErrorWriteBuff(FileName, i, "Transform", Err.Number, Err.Description, "系统讯息") Resume Next End Function Private Function WriteFile(Path As String, NewFileName As String, ErrFileName As String, DataString() As Byte) As Boolean Dim FileNum As Integer, i As Long On Error GoTo ErrorHandling FileNum = FreeFile 'Write New Binary File Open Path & NewFileName For Binary Access Write As #FileNum For i = 0 To UBound(DataString) DoEvents Put #FileNum, , CByte(Val(DataString(i))) 'Binary Next i Close #FileNum If UBound(ErrorData) > 0 Then FileNum = FreeFile 'Write Error Messages Open Path & ErrFileName For Output As #FileNum For i = 0 To UBound(ErrorData) DoEvents Print #FileNum, ErrorData(i) 'String Next i Close #FileNum End If WriteFile = True Exit Function ErrorHandling: WriteFile = False ': MsgBox "Error !" Call ErrorWriteBuff(FileName, 0, "WriteFile", Err.Number, Err.Description, "系统讯息") Resume Next End Function Private Function DataStringProcess(TitleTemp() As Byte, i As Long) As Boolean On Error GoTo ErrorHandling 'Binary 处理分两种 '1. 单纯一个16进位码对应一个Ascii Code '2. 不单纯有特定格式之N Bytes 16进位码组成一个数字或字串~而其中又分~ '1.LSB系列 - 低位元至高位元由左排至右 '2.MSB系列 - 低位元至高位元由右排至左 DataStringProcess = True Exit Function ErrorHandling: DataStringProcess = False ': MsgBox "Error !" Call ErrorWriteBuff(FileName, 0, "WriteFile", Err.Number, Err.Description, "系统讯息") Resume Next End Function
Option Explicit Public Declare Function QueryPerformanceCounter Lib "kernel32" (x As Currency) As Boolean Public Declare Function QueryPerformanceFrequency Lib "kernel32" (x As Currency) As Boolean Public Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long Public ErrorCount As Long Public ErrorData() As String Public Function ErrorWriteBuff(FileName As String, lines As Long, FunctionName As String, code As Integer, Description As String, Remarks As String) As Boolean If Description = "" Then Description = "Null" ReDim Preserve ErrorData(ErrorCount) ErrorData(ErrorCount) = FileName & ":" & Format(lines, "00000000") & " " & FunctionName & " " & "code :" & code & " Description :" & Description & ":" & Remarks ErrorCount = ErrorCount + 1 End Function Public Function IEEE754(i As Currency, TitleTemp() As Byte, b As String) As String '十进位转((二进位(自己写的))浮点数转换) Dim binary() As Integer, ii As Integer, jj As Integer, temp As Integer, power As Integer Dim number As Double, Result_T As Currency Dim tempstring As String, a As String Erase binary ReDim binary(31) temp = 0 power = 0 number = 0 tempstring = "" b = "" Result_T = ((CCur(TitleTemp(i)) * 1) + (CCur(TitleTemp(i + 1)) * 256) + (CCur(TitleTemp(i + 2)) * 65536) + (CCur(TitleTemp(i + 3)) * 16777216)) '-------------------------------------------------------------------十进位转二进位(自己写的) For ii = 0 To 3 temp = TitleTemp(i + ii) For jj = 0 To 7 If temp < 2 And temp >= 0 Then If temp = 1 Then binary(31 - jj - (ii * 8)) = 1 temp = temp - 1 Else binary(31 - jj - (ii * 8)) = 0 End If ElseIf temp >= 2 Then binary(31 - jj - (ii * 8)) = temp Mod 2 temp = Fix(temp / 2) Else binary(31 - jj - (ii * 8)) = 0 End If Next jj Next ii '-------------------------------------------------------------------(32bits)二进位转浮点数 For ii = 0 To 31 a = a & binary(ii) Next ii If binary(0) = 0 Then b = "+" Else b = "-" End If tempstring = "" For ii = 1 To 8 tempstring = tempstring & binary(ii) Next ii number = Dec(tempstring) power = number - 127 tempstring = "" For ii = 9 To 31 tempstring = tempstring & binary(ii) Next ii tempstring = "1." & tempstring number = Dec(tempstring) number = number * (2 ^ power) b = b & CStr(number) '------------------------------------------------------------------- End Function Public Function Bin(ByVal n As Double, ByVal m As Long) As String '十进位转二进位(网路抓的)-暂无用处 Dim i As Long, dot As Long, iP As Long, fP As Double Dim prefix As String, BinInt As String, BinFloat As String If Left(n, 1) = "-" Then prefix = "-": n = Mid(n, 2) dot = InStr(n, ".") If dot <> 0 Then iP = Left(n, dot - 1): fP = Mid(n, dot) Else iP = n Do BinInt = (iP Mod 2) & BinInt iP = iP \ 2 Loop Until iP = 0 BinInt = prefix & BinInt If dot = 0 Then Bin = BinInt: Exit Function For i = 1 To m fP = fP * 2 fP = (fP - Int(fP)) + (Int(fP) Mod 2) BinFloat = BinFloat & Int(fP) If fP = 1 Then Exit For Next Bin = BinInt & "." & BinFloat End Function Public Function Dec(ByVal n As String) As Double '二进位转十进位 Dim i As Long, j As Long, dot As Long, prefix As Long prefix = Sgn(n) If prefix = -1 Then n = Mid(n, 2) dot = InStr(n, ".") If dot = 0 Then dot = Len(n) - 1 Else n = Left(n, dot - 1) & Mid(n, dot + 1) dot = dot - 2 End If For i = dot To dot - Len(n) + 1 Step -1 j = j + 1 If Mid(n, j, 1) <> 0 Then Dec = Dec + 2 ^ i Next Dec = Dec * prefix End Function Public Function StringToHex(K_start As Integer, K_end As Integer, j_start As Long, Title() As String, All() As String, end_j As Integer) Dim k As Integer, m As Integer Dim j As Long On Error Resume Next j = j_start For k = K_start To K_end m = 1 If Title(k) <> "" Then If Asc(Title(k)) <> 0 Then If Len(Title(k)) <> 0 Then If Title(k) = "2.5.9_8 " Then All(j) = Len(Title(k)) + 3 For j = j + 1 To (j + 1) + Len(CStr(Title(k))) - 1 All(j) = Asc(Mid(CStr(Title(k)), m, 1)) m = m + 1 Next j Else All(j) = Len(Trim(Title(k))) For j = j + 1 To (j + 1) + Len(Trim(Title(k))) - 1 All(j) = Asc(Mid(CStr(Title(k)), m, 1)) m = m + 1 Next j End If End If Else All(j) = 0 j = j + 1 End If Else All(j) = 0 j = j + 1 End If Next k end_j = j End Function Public Function DecToHex(Array_start As Integer, Source As Currency, ByRef Result() As String, How_Byte As Integer) Dim TempTotalCount1 As Integer, TempTotalCount2 As Integer On Error Resume Next If How_Byte = 4 Then If Fix(CCur(Source) / 256) >= 1 Then If Fix(CCur(Source) / 65536) >= 1 Then If Fix(CCur(Source) / 16777216) >= 1 Then Result(Array_start + 3) = CByte(Fix(CCur(Source) / 16777216)) '''最高位数字(最右) TempTotalCount1 = Result(Array_start + 3) Result(Array_start + 2) = CByte(Fix(CCur(Source) / 65536) - CCur(TempTotalCount1) * 256) '''次高位数字(右) TempTotalCount2 = Result(Array_start + 2) Result(Array_start + 1) = CByte(Fix(CCur(Source) / 256) - CCur((TempTotalCount1) * 65536) - (CCur(TempTotalCount2) * 256)) '''次次高位数字(右) Result(Array_start) = CByte(CCur(Source) - CCur(256 * (Fix(CCur(Source) / 256)))) ' Result(Array_start) = CByte(CCur(Source) Mod 256) '''最低位数字(最左) Else Result(Array_start + 3) = 0 Result(Array_start + 2) = CByte(Fix(CCur(Source) / 65536)) TempTotalCount2 = Result(Array_start + 2) Result(Array_start + 1) = CByte(Fix(CCur(Source) / 256) - (CCur(TempTotalCount2) * 256)) Result(Array_start) = CByte(CCur(Source) - CCur(256 * (Fix(CCur(Source) / 256)))) End If Else Result(Array_start + 3) = 0 Result(Array_start + 2) = 0 Result(Array_start + 1) = CByte(Fix(CCur(Source) / 256)) Result(Array_start) = CByte(CCur(Source) - CCur(256 * (Fix(CCur(Source) / 256)))) End If Else Result(Array_start + 3) = 0 Result(Array_start + 2) = 0 Result(Array_start + 1) = 0 Result(Array_start) = CByte(CCur(Source)) End If ElseIf How_Byte = 2 Then If Fix(CCur(Source) / 256) >= 1 Then Result(Array_start + 1) = CByte(Fix(CCur(Source) / 256)) Result(Array_start) = CByte(CCur(Source) - CCur(256 * (Fix(CCur(Source) / 256)))) Else Result(Array_start + 1) = 0 Result(Array_start) = CByte(CCur(Source)) End If ElseIf How_Byte = 1 Then Result(Array_start) = CByte(CCur(Source)) End If End Function
//------------------------------------------------------------------------------------------------------------------// int DecToHex(int Array_start, unsigned int Source, char *Result, int How_Byte) { int TempTotalCount1=0, TempTotalCount2=0; if (How_Byte == 4) { if ((Source / 256) >= 1) { if ((Source / 65536) >= 1) { if ((Source / 16777216) >= 1) { Result[Array_start + 3] = (Source / 16777216); //最高位数字(最右) TempTotalCount1 = Result[Array_start + 3]; Result[Array_start + 2] = ((Source / 65536) - (TempTotalCount1 * 256)); //次高位数字(右) TempTotalCount2 = Result[Array_start + 2]; Result[Array_start + 1] = ((Source / 256) - (TempTotalCount1 * 65536) - (TempTotalCount2 * 256)); //次次高位数字(右) Result[Array_start] = (Source - (256 * (Source / 256))); Result[Array_start] = (Source % 256); //最低位数字(最左) } else { Result[Array_start + 3] = 0; Result[Array_start + 2] = (Source / 65536); TempTotalCount2 = Result[Array_start + 2]; Result[Array_start + 1] = ((Source / 256) - (TempTotalCount2 * 256)); Result[Array_start] = (Source - (256 * (Source / 256))); } } else { Result[Array_start + 3] = 0; Result[Array_start + 2] = 0; Result[Array_start + 1] = (Source / 256); Result[Array_start] = (Source - (256 * (Source / 256))); } } else { Result[Array_start] = Source; Result[Array_start + 1] = 0; Result[Array_start + 2] = 0; Result[Array_start + 3] = 0; } } else if (How_Byte == 2) { if ((Source / 256) >= 1) { Result[Array_start + 1] = (Source / 256); Result[Array_start] = (Source - (256 * (Source / 256))); } else { Result[Array_start + 1] = 0; Result[Array_start] = Source; } } else if (How_Byte == 1) { Result[Array_start] = Source; } Array_start++; return Array_start; } //------------------------------------------------------------------------------------------------------------------// int StringToHex(int K_start, int K_end, int j_start, int flag, char **part1, char *All) { unsigned int m,j,n,S_len=0; int k; char *temp; j = j_start; //All的开始位址 k=K_start; //part1的开始位址 for (k = K_start;k<=K_end;k++) //part1的目前位址 { m = 0; temp=part1[k]; if (strcmp(temp," ") != 0) { if(flag!=1) { S_len = strlen(temp); All[j] = S_len; for (n=j+1;n<=(j+1)+S_len-1;n++) { All[n] = (int)temp[m]; m = m + 1; } j=j+S_len+1; } else { All[j] = (int)temp[0]; j = j + 1; } } else { All[j] = 0; j = j + 1; } } j_start=j; return j; } //------------------------------------------------------------------------------------------------------------------// int sum(int start,int end,char *TitleTemp) { unsigned int mode; int Number,Number1,Number2,Number3,Number4; Number=0,Number1=0,Number2=0,Number3=0,Number4=0; mode=end-start+1; if(mode==2) { int Number1=(long)TitleTemp[end]*256; Number1=Number1&0x0000ffff; int Number2=(long)TitleTemp[end-1]*1; Number2=Number2&0x000000ff; Number=Number1+Number2; return Number; } else if(mode==4) { int Number1=(long)TitleTemp[end]*16777216; Number1=Number1&0xffffffff; int Number2=(long)TitleTemp[end-1]*65536; Number2=Number2&0x00ffffff; int Number3=(long)TitleTemp[end-2]*256; Number3=Number3&0x0000ffff; int Number4=(long)TitleTemp[end-3]*1; Number4=Number4&0x000000ff; int Number=Number1+Number2+Number3+Number4; return Number; } return 0; } //------------------------------------------------------------------------------------------------------------------// /*********************************************************************************** * Function: ADC_Calc; * Description: ;IEEE754 浮点数转为半浮点数 * Input: 待转换的数,float; * Output: 两位元组半浮点数; * Return: 整形指标类型; * Note: none; ************************************************************************************/ unsigned int FloatToHalf(float value) { unsigned char i,sdata[4]; unsigned int ret; unsigned long tdata; void *p; p=&value; for(i=0;i<4;i++) { sdata[i]=*((unsigned char *)p+i); } tdata=((unsigned long)sdata[3]<<24)+((unsigned long)sdata[2]<<16)+(unsigned int)(sdata[1]<<8)+sdata[0]; //zero if((sdata[0]==0)&&(sdata[1]==0)&&(sdata[2]==0)&&(sdata[3]==0)) return 0; if((sdata[0]==0x80)&&(sdata[1]==0)&&(sdata[2]==0)&&(sdata[3]==0)) return 0x8000; //infinity if((sdata[0]==0x7f)&&(sdata[1]==0x80)&&(sdata[2]==0)&&(sdata[3]==0)) return 0x7c00; if((sdata[0]==0xff)&&(sdata[1]==0x80)&&(sdata[2]==0)&&(sdata[3]==0)) return 0xfc00; //outrange if(value>65000) return 0x7bff; if(value<-65000) return 0xfbff; //normal ret=(((tdata>>16)&0x8000)|((((tdata&0x7f800000)-0x38000000)>>13)&0x7C00)|((tdata>>13)&0x3ff)); return ret; } /*********************************************************************************** * Function: HalfToFloat; * Description: ;IEEE754 半浮点数(hfloat)转为浮点数(float) * Input: 两位元组半浮点数; * Output: float; * Return: float; * Note: none; ************************************************************************************/ float HalfToFloat(unsigned int hfloat) { unsigned long tdata; float ret; void *p; //zero if(hfloat==0) { return 0; } if(hfloat==0x8000) { p=&ret; *(unsigned char *)p =0x80; *((unsigned char *)p+1) =0x00; *((unsigned char *)p+2) =0x00; *((unsigned char *)p+3) =0x00; return ret; } //infinity if(hfloat==0x7c00) { p=&ret; *(unsigned char *)p =0x7f; *((unsigned char *)p+1) =0x80; *((unsigned char *)p+2) =0x00; *((unsigned char *)p+3) =0x00; return ret; } if(hfloat==0xfc00) { p=&ret; *(unsigned char *)p =0xff; *((unsigned char *)p+1) =0x80; *((unsigned char *)p+2) =0x00; *((unsigned char *)p+3) =0x00; return ret; } //normal tdata=(((hfloat&0x8000)<<16)|(((hfloat&0x7c00)+0x1c000)<<13)|((hfloat&0x3ff)<<13)); p=&ret; *(unsigned char *)p =tdata>>24; *((unsigned char *)p+1) =tdata>>16; *((unsigned char *)p+2) =tdata>>8; *((unsigned char *)p+3) =tdata; return ret; }