ezzetabi Posted December 27, 2004 Share Posted December 27, 2004 (edited) expandcollapse popup#region;_Dec2Bin($sNum), _Dec2Oct($sNum), _Dec2Hex($sNum), _Bin2Dec($sNum), _Oct2Dec($sNum), _Hex2Dec($sNum) Func _Dec2Bin($sNum) $sNum = StringReplace($sNum, ' ', '') Local $aParts[3], $iDigit, $sOutPut, $bNeg = 0 If StringLeft($sNum, 1) = '-' Then $bNeg = 1 $sNum = StringTrimLeft($sNum, 1) EndIf If StringInStr($sNum, '.') Then $aParts = StringSplit($sNum, '.') $aParts[1] = Int($aParts[1]) $aParts[2] = $aParts[2] / 10 ^ StringLen($aParts[2]) Else $aParts[1] = Int($sNum) $aParts[2] = 0 EndIf While $aParts[1] <> 0 $iDigit = Mod($aParts[1], 2) $sOutPut = $iDigit & $sOutPut $aParts[1] = Int($aParts[1] / 2) Wend If $aParts[2] <> 0 Then $sOutPut = $sOutPut & '.' While $aParts[2] <> 0 $aParts[2] = $aParts[2] * 2 $iDigit = Int($aParts[2]) $aParts[2] = $aParts[2] - $iDigit $sOutPut = $sOutPut & $iDigit Wend EndIf If $bNeg = 1 Then $sOutPut = '-' & $sOutPut Return $sOutPut EndFunc ;==>_Dec2Bin Func _Dec2Oct($sNum) $sNum = StringReplace($sNum, ' ', '') Local $aParts[3], $iDigit, $sOutPut, $bNeg = 0 If StringLeft($sNum, 1) = '-' Then $bNeg = 1 $sNum = StringTrimLeft($sNum, 1) EndIf If StringInStr($sNum, '.') Then $aParts = StringSplit($sNum, '.') $aParts[1] = Int($aParts[1]) $aParts[2] = $aParts[2] / 10 ^ StringLen($aParts[2]) Else $aParts[1] = Int($sNum) $aParts[2] = 0 EndIf While $aParts[1] <> 0 $iDigit = Mod($aParts[1], 8) $sOutPut = $iDigit & $sOutPut $aParts[1] = Int($aParts[1] / 8) Wend If $aParts[2] <> 0 Then $sOutPut = $sOutPut & '.' While $aParts[2] <> 0 $aParts[2] = $aParts[2] * 8 $iDigit = Int($aParts[2]) $aParts[2] = $aParts[2] - $iDigit $sOutPut = $sOutPut & $iDigit Wend EndIf If $bNeg = 1 Then $sOutPut = '-' & $sOutPut Return $sOutPut EndFunc ;==>_Dec2Oct Func _Dec2Hex($sNum) $sNum = StringReplace($sNum, ' ', '') Local $aParts[3], $iDigit, $sOutPut, $bNeg = 0 If StringLeft($sNum, 1) = '-' Then $bNeg = 1 $sNum = StringTrimLeft($sNum, 1) EndIf If StringInStr($sNum, '.') Then $aParts = StringSplit($sNum, '.') $aParts[1] = Int($aParts[1]) $aParts[2] = $aParts[2] / 10 ^ StringLen($aParts[2]) Else $aParts[1] = Int($sNum) $aParts[2] = 0 EndIf While $aParts[1] <> 0 $iDigit = Mod($aParts[1], 16) If $iDigit >= 10 Then $iDigit = Chr(55 + $iDigit);65 is A EndIf $sOutPut = $iDigit & $sOutPut $aParts[1] = Int($aParts[1] / 16) Wend If $aParts[2] <> 0 Then $sOutPut = $sOutPut & '.' While $aParts[2] <> 0 $aParts[2] = $aParts[2] * 16 $iDigit = Int($aParts[2]) $aParts[2] = $aParts[2] - $iDigit If $iDigit >= 10 Then $iDigit = Chr(55 + $iDigit);65 is A EndIf $sOutPut = $sOutPut & $iDigit Wend EndIf If $bNeg = 1 Then $sOutPut = '-' & $sOutPut Return $sOutPut EndFunc ;==>_Dec2Hex Func _Bin2Dec($sNum) $sNum = StringReplace($sNum, ' ', '') Local $iSep, $fOutPut = 0, $iDigit, $c, $iL, $bNeg = 0 If StringLeft($sNum, 1) = '-' Then $bNeg = 1 $sNum = StringTrimLeft($sNum, 1) EndIf If Not _Only($sNum, '0|1|.') Then Return 0 EndIf $iSep = StringInStr($sNum, '.') If $iSep <> 0 Then $sNum = StringReplace($sNum, '.', '') $iSep = (StringLen($sNum) - $iSep + 1) * - 1 EndIf $iL = StringLen($sNum) For $c = 1 To StringLen($sNum) $iDigit = StringMid($sNum, $iL - $c + 1, 1) $iDigit = Int($iDigit) $fOutPut = $fOutPut + $iDigit * 2^ ($iSep + $c - 1) Next If $bNeg = 1 Then $fOutPut = $fOutPut * - 1 Return $fOutPut EndFunc ;==>_Bin2Dec Func _Oct2Dec($sNum) $sNum = StringReplace($sNum, ' ', '') Local $iSep, $fOutPut = 0, $iDigit, $c, $iL, $bNeg = 0 If StringLeft($sNum, 1) = '-' Then $bNeg = 1 $sNum = StringTrimLeft($sNum, 1) EndIf If Not _Only($sNum, '0|1|2|3|4|5|6|7|.') Then Return 0 EndIf $iSep = StringInStr($sNum, '.') If $iSep <> 0 Then $sNum = StringReplace($sNum, '.', '') $iSep = (StringLen($sNum) - $iSep + 1) * - 1 EndIf $iL = StringLen($sNum) For $c = 1 To StringLen($sNum) $iDigit = StringMid($sNum, $iL - $c + 1, 1) $iDigit = Int($iDigit) $fOutPut = $fOutPut + $iDigit * 8^ ($iSep + $c - 1) Next If $bNeg = 1 Then $fOutPut = $fOutPut * - 1 Return $fOutPut EndFunc ;==>_Oct2Dec Func _Hex2Dec($sNum) $sNum = StringReplace($sNum, ' ', '') Local $iSep, $fOutPut = 0, $iDigit, $c, $iL, $bNeg = 0 If StringLeft($sNum, 1) = '-' Then $bNeg = 1 $sNum = StringTrimLeft($sNum, 1) EndIf If Not _Only($sNum, '0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|.') Then Return 0 EndIf $iSep = StringInStr($sNum, '.') If $iSep <> 0 Then $sNum = StringReplace($sNum, '.', '') $iSep = (StringLen($sNum) - $iSep + 1) * - 1 EndIf $iL = StringLen($sNum) For $c = 1 To StringLen($sNum) $iDigit = StringMid($sNum, $iL - $c + 1, 1) Select Case $iDigit = 'a' $iDigit = 10 Case $iDigit = 'b' $iDigit = 11 Case $iDigit = 'c' $iDigit = 12 Case $iDigit = 'd' $iDigit = 13 Case $iDigit = 'e' $iDigit = 14 Case $iDigit = 'f' $iDigit = 15 Case Else $iDigit = Int($iDigit) EndSelect $fOutPut = $fOutPut + $iDigit * 16^ ($iSep + $c - 1) Next If $bNeg = 1 Then $fOutPut = $fOutPut * - 1 Return $fOutPut EndFunc ;==>_Hex2Dec Func _Only($string, $aItems) $aItems = StringSplit($aItems, '|') Local $bOk, $c, $c2, $char For $c = 1 To StringLen($string) $char = StringMid($string, $c, 1) $bOk = 0 For $c2 = 1 To $aItems[0] If $aItems[$c2] = $char Then $bOk = 1 ExitLoop EndIf Next If $bOk = 0 Then Return 0 EndIf Next Return 1 EndFunc ;==>_Only #EndRegion Fixed a bug about negative numbers. Edited December 28, 2004 by ezzetabi Link to comment Share on other sites More sharing options...
Josbe Posted December 27, 2004 Share Posted December 27, 2004 Some months ago I needed some of these functions. (But, I had write it) Now, I found here others(2) that could serve me. Thxs. AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta Link to comment Share on other sites More sharing options...
ezzetabi Posted December 28, 2004 Author Share Posted December 28, 2004 I was thinking about creating a any to any (actually 2-36) base converter. What do you think? Is a good idea? Link to comment Share on other sites More sharing options...
Guest Guidosoft Posted December 28, 2004 Share Posted December 28, 2004 I already made one but I lost it. It handled infinite bases to infinite bases. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now