Bits Tool V1 一个自制 BIT | BIN | HEX 转换小工具

用 VC6 开发一个 Win 平台小工具,用来计算 bit 相关的二进制、十六进制和十进制之间的转换。最主要的功能是,可以快速知道哪个 bit 是 0 还是 1。尽管进制转化,Win7计算器可以很好地完成,但是特别有时候需要知道第几个位的状态,如果你的眼力心算能力好,有可能从一堆 0 和 1 中瞄出来,不然只好老老实实地数吧。所以这个软件的最大的用处是把每个位都单独标出来。

Bits_Tool

虽说不要造轮子,可是我想要自己的轮子,所以这是我的第一个轮子。

版本更新历史

 06.29.14 Added: 增加整数符号功能 
 06.23.14 Added: 增加十进制转化 
 07.03.13 Changed: 将资源和对象处理一般化,消息映射使用ON_CONTROL_RANGE
 06.12.13 Changed: 将进制转换封装为CHBDconv类
 06.03.13 Added: 使用CLable类,设置CStatic控件背景颜色
 06.01.13 Changed: 更改了ICON图标
 05.31.13 Added : 增加非HEX输入提示
 05.30.13 Changed : 取消BIN无输入的提示
 05.30.13 Changed : 显示字符串消去左边的0
 发布最初版本 V 1.0 (03.14.13)
 - 十六进制、二进制相互转化;同时将各个位的状态显示出来
 - 勾选各个位的状态;同时将结果显示为二进制和十六进制
 - 取反
 - 复位

用法和开发背景

用法很简单,在 HEX 或者 BIN 或者 DEC 输入框里输入制值,从Bit 0 到 31都会把为 1 的位标识出来,便于快速查找位的含义;勾选相应的 Bits,会显示相应的 各种进制的数值,一目了然。

在有些机器人品牌的系统参数设置中,系统变量是整型,每个 Bit 表示不同的功能,其状态 0 或 1 表示其功能关闭或者打开。如果缺乏原生的可视化工具,无疑数位数和设置位数是很痛苦的一项 dirty work,所以这种应用是 Bits Tool 开发的主要背景。

Bits_Tool_Usage

算法

算法主要是十六进制和二进制的互转,LT 参考了网上一个 VB 格式的算法HEXTOBIN(出处暂时找不到了,等找到再补上 ),然后用 VC 改了改,拿来主义,呵呵。这个小工具,大部分时间其实是花在了界面上。界面操作 CSTATIC 控件背景颜色使用了 CLABEL 类(Extended Use of CStatic Class – CLabel 1.6)。

十进制和二进制的转化,参考 http://baike.baidu.com/view/1426817.htm ,二十进制和十六进制之间的转化,则是通过二进制作为中间过渡。关于转化 算法,封装到一个 CHBDconv 类中,以方便调用。

class CHBDconv  
{
public:
	bool Hex2Bin(CString sHex_input, CString &sBin_Output);
	bool Bin2Hex(CString sBin_Input, CString &sHex_Output);
	bool Dec2Bin(CString sDec_Input, CString &sBin_Output, bool bSigned = true);
	bool Bin2Dec(CString sBin_Input, CString &sDec_Output, bool bSigned = true);
	//下面函数是组合而来
	bool Hex2Dec(CString sHex_Input, CString &sDec_Output, bool bSigned = true);
	bool Dec2Hex(CString sDec_Input, CString &sHex_Output, bool bSigned = true);
	//

};

 

下面是原VB的转换算法

Private Function HexToBin(ByVal InputData As String) As String
    On Error GoTo ErrMsg
    Const ProcName = "HexToBin"

    Dim ii        As Integer
    Dim BinOut    As String
    Dim intLen    As Integer

    InputData = UCase(InputData)
    intLen = Len(InputData)
    For ii = 1 To intLen
        If IsNumeric(Mid(InputData, ii, 1)) Then
            GoTo NumOk
        ElseIf Mid(InputData, ii, 1) = "A" Then
            GoTo NumOk
        ElseIf Mid(InputData, ii, 1) = "B" Then
            GoTo NumOk
        ElseIf Mid(InputData, ii, 1) = "C" Then
            GoTo NumOk
        ElseIf Mid(InputData, ii, 1) = "D" Then
            GoTo NumOk
        ElseIf Mid(InputData, ii, 1) = "E" Then
            GoTo NumOk
        ElseIf Mid(InputData, ii, 1) = "F" Then
            GoTo NumOk
        Else
'            MsgBox "Number   given   is   not   in   Hex   format", vbCritical
            MsgBox "输入的数据不是十六进制的数据,请检查...", vbExclamation
            GoTo ErrExit
        End If
NumOk:
    Next ii

    BinOut = ""
    For ii = 1 To intLen
        If Mid(InputData, ii, 1) = "0" Then
            BinOut = BinOut & "0000"
        ElseIf Mid(InputData, ii, 1) = "1" Then
            BinOut = BinOut & "0001"
        ElseIf Mid(InputData, ii, 1) = "2" Then
            BinOut = BinOut & "0010"
        ElseIf Mid(InputData, ii, 1) = "3" Then
            BinOut = BinOut & "0011"
        ElseIf Mid(InputData, ii, 1) = "4" Then
            BinOut = BinOut & "0100"
        ElseIf Mid(InputData, ii, 1) = "5" Then
            BinOut = BinOut & "0101"
        ElseIf Mid(InputData, ii, 1) = "6" Then
            BinOut = BinOut & "0110"
        ElseIf Mid(InputData, ii, 1) = "7" Then
            BinOut = BinOut & "0111"
        ElseIf Mid(InputData, ii, 1) = "8" Then
            BinOut = BinOut & "1000"
        ElseIf Mid(InputData, ii, 1) = "9" Then
            BinOut = BinOut & "1001"
        ElseIf Mid(InputData, ii, 1) = "A" Then
            BinOut = BinOut & "1010"
        ElseIf Mid(InputData, ii, 1) = "B" Then
            BinOut = BinOut & "1011"
        ElseIf Mid(InputData, ii, 1) = "C" Then
            BinOut = BinOut & "1100"
        ElseIf Mid(InputData, ii, 1) = "D" Then
            BinOut = BinOut & "1101"
        ElseIf Mid(InputData, ii, 1) = "E" Then
            BinOut = BinOut & "1110"
        ElseIf Mid(InputData, ii, 1) = "F" Then
            BinOut = BinOut & "1111"
        Else
            MsgBox "数据不在转换范围之内,请检查...", vbExclamation
        End If
    Next ii
    HexToBin = BinOut

ErrExit:
    On Error GoTo 0
    Exit Function

ErrMsg:
    MsgBox Err.Number & " " & Err.Description
    Resume ErrExit
  End Function

 

Private Function BinToHex(ByVal InputData As String) As String
    On Error GoTo ErrMsg
    Const ProcName = "BinToHex"

    Dim ii          As Integer
    Dim intLen      As Integer
    Dim chkBin      As String
    Dim NumBlocks   As Integer
    Dim FullBin     As String
    Dim HexOut      As String
    Dim TempBin     As String
    Dim TempHex     As String

    intLen = Len(InputData)
    For ii = 1 To intLen
        chkBin = Mid(InputData, ii, 1)
        If chkBin <> "0" And chkBin <> "1" Then
            MsgBox "不是正常的二进制数据,请检查...", vbExclamation
            GoTo ErrExit
        End If
    Next ii
    FullBin = InputData
  ''If   the   value   is   less   than   4   in   length,   build   it   up.
    If intLen < 4 Then
        If intLen = 3 Then
            FullBin = "0" & FullBin
        ElseIf intLen = 2 Then
            FullBin = "00" & FullBin
        ElseIf intLen = 1 Then
            FullBin = "000" & FullBin
        ElseIf intLen = 0 Then
            MsgBox "Nothing   Given..", vbCritical
            MsgBox "没有数据可以进行转换...", vbExclamation
            GoTo ErrExit
        End If
        NumBlocks = 1
        GoTo DoBlocks
    End If

    If intLen = 4 Then
        NumBlocks = 1
        GoTo DoBlocks
    End If

    If intLen > 4 Then

        Dim TempHold    As Currency
        Dim TempDiv     As Currency
        Dim AfterDot    As Integer
        Dim Pos         As Integer

        TempHold = Len(InputData)
        TempDiv = (TempHold / 4)

        ''     Works   by   seeing   whats   after   the   deciomal   place
        Pos = InStr(1, CStr(TempDiv), ".")
        If Pos = 0 Then
            ''Divided   by   4   perfectly
            NumBlocks = TempDiv
            GoTo DoBlocks
        End If

        AfterDot = Mid(CStr(TempDiv), (Pos + 1))

        If AfterDot = 25 Then
            FullBin = "000" & FullBin
            NumBlocks = (Len(FullBin) / 4)
        ElseIf AfterDot = 5 Then
            FullBin = "00" & FullBin
            NumBlocks = (Len(FullBin) / 4)
        ElseIf AfterDot = 75 Then
            FullBin = "0" & FullBin
            NumBlocks = (Len(FullBin) / 4)
        Else
'            MsgBox "Big   Time   Screw   up   happened,   WAHHHHHHHHHHH", vbInformation
            GoTo ErrExit
        End If
        GoTo DoBlocks
    End If

  ''     The   rest   will   process   the   now   built   up   number
  ''
DoBlocks:

    HexOut = ""
    For ii = 1 To Len(FullBin) Step 4
        TempBin = Mid(FullBin, ii, 4)
        If TempBin = "0000" Then
            HexOut = HexOut & "0"
        ElseIf TempBin = "0001" Then
            HexOut = HexOut & "1"
        ElseIf TempBin = "0010" Then
            HexOut = HexOut & "2"
        ElseIf TempBin = "0011" Then
            HexOut = HexOut & "3"
        ElseIf TempBin = "0100" Then
            HexOut = HexOut & "4"
        ElseIf TempBin = "0101" Then
            HexOut = HexOut & "5"
        ElseIf TempBin = "0110" Then
            HexOut = HexOut & "6"
        ElseIf TempBin = "0111" Then
            HexOut = HexOut & "7"
        ElseIf TempBin = "1000" Then
            HexOut = HexOut & "8"
        ElseIf TempBin = "1001" Then
            HexOut = HexOut & "9"
        ElseIf TempBin = "1010" Then
            HexOut = HexOut & "A"
        ElseIf TempBin = "1011" Then
            HexOut = HexOut & "B"
        ElseIf TempBin = "1100" Then
            HexOut = HexOut & "C"
        ElseIf TempBin = "1101" Then
            HexOut = HexOut & "D"
        ElseIf TempBin = "1110" Then
            HexOut = HexOut & "E"
        ElseIf TempBin = "1111" Then
            HexOut = HexOut & "F"
        End If
    Next ii

    BinToHex = HexOut

ErrExit:
    On Error GoTo 0
    Exit Function

ErrMsg:
    MsgBox Err.Number & " " & Err.Description
    Resume ErrExit
End Function

扩展阅读

关键字:HEX2BIN, BIN2HEX, 十六进制, 二进制, bit, bin, hex

2 thoughts on “Bits Tool V1 一个自制 BIT | BIN | HEX 转换小工具

  1. 有幸拜读阁下的源码作品。但是发现一些编码规范上的问题,比如阁下为了修改for循环中重复定义变量的问题,使用了不规范的变量命名。从工业编码的角度看,好的符号名称是很能体现出设计者的匠心的。

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.