Advertisement
2002VB Miscellaneous #17982

Conversion between Dec, Bin and Hex

This module contain function that are used to convert between decimal, binary and hexadecimal.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
'**************************************************************
'*             Best Tools             *
'*             Conversion             *
'*         v2.1 (Improved performance)        *
'*              for VB              *
'*                              *
'*This module contain a lot of subs and functions for basic  *
'*conversion between Hexadecimal, Binary and decimal.     *
'**************************************************************
Option Explicit
Public Function Bin2Dec(ByVal sBin As String) As Long
 Dim i As Integer
  
 For i = 1 To Len(sBin)
  Bin2Dec = Bin2Dec + CLng(CInt(Mid(sBin, Len(sBin) - i + 1, 1)) * 2 ^ (i - 1))
 Next i
End Function
Public Function Bin2Hex(ByVal sBin As String) As String
 Dim i As Integer
 Dim nDec As Long
 sBin = String(4 - Len(sBin) Mod 4, "0") & sBin 'Add zero to complete Byte
 For i = 1 To Len(sBin)
  nDec = nDec + CInt(Mid(sBin, Len(sBin) - i + 1, 1)) * 2 ^ (i - 1)
 Next i
 Bin2Hex = Hex(nDec)
 If Len(Bin2Hex) Mod 2 = 1 Then Bin2Hex = "0" & Bin2Hex
End Function
Public Function Dec2Bin(ByVal nDec As Integer) As String
 'This function is the same then Hex2Bin, but it has been copied to speed up process
 Dim i As Integer
 Dim j As Integer
 Dim sHex As String
 Const HexChar As String = "0123456789ABCDEF"
 
 sHex = Hex(nDec) 'That the only part that is different
 For i = 1 To Len(sHex)
  nDec = InStr(1, HexChar, Mid(sHex, i, 1)) - 1
  For j = 3 To 0 Step -1
   Dec2Bin = Dec2Bin & nDec \ 2 ^ j
   nDec = nDec Mod 2 ^ j
  Next j
 Next i
 'Remove the first unused 0
 i = InStr(1, Dec2Bin, "1")
 If i <> 0 Then Dec2Bin = Mid(Dec2Bin, i)
End Function
Public Function Hex2Bin(ByVal sHex As String) As String
 Dim i As Integer
 Dim j As Integer
 Dim nDec As Long
 Const HexChar As String = "0123456789ABCDEF"
 
 For i = 1 To Len(sHex)
  nDec = InStr(1, HexChar, Mid(sHex, i, 1)) - 1
  For j = 3 To 0 Step -1
   Hex2Bin = Hex2Bin & nDec \ 2 ^ j
   nDec = nDec Mod 2 ^ j
  Next j
 Next i
 'Remove the first unused 0
 i = InStr(1, Hex2Bin, "1")
 If i <> 0 Then Hex2Bin = Mid(Hex2Bin, i)
End Function
Public Function Hex2Dec(ByVal sHex As String) As Long
 Dim i As Integer
 Dim nDec As Long
 Const HexChar As String = "0123456789ABCDEF"
 
 For i = Len(sHex) To 1 Step -1
  nDec = nDec + (InStr(1, HexChar, Mid(sHex, i, 1)) - 1) * 16 ^ (Len(sHex) - i)
 Next i
 Hex2Dec = CStr(nDec)
End Function
Public Function HiWord(ByVal DWord As Long) As Long
 HiWord = (DWord \ 65536) And &HFFFF
End Function
Public Function LoWord(ByVal DWord As Long) As Long
 LoWord = DWord And &HFFFF
End Function
Public Function DWord(ByVal HiWord As Long, ByVal LoWord As Long) As Long
 DWord = ((LoWord And 65536) Or ((HiWord And 65536) * 65536))
End Function
NuclearMedia 2.0 is a media SDK that can play most media files including m3u playlists (the winamp playlist). It uses Windows API except NuclearM3U.h. This API is only available on Windows 98/NT and up. NuclearM3U.h can be run on any OS but not tested. There is a class that connects all the engines together so you don’t have to. It is very easy to use, but the code is pretty advanced. This can be used in any type of program console, win32, ect. Most (all that I found) have been fixed if you see any contact me.
<br>
Write comments and vote!!!
<br>
<a href = http://www.lostsidedead-software.com/codes/NuclearMedia2_0.zip>DownLoad</a>
<H2><Center>Updates</Center></H2>
I am working on NuclearMediaSDK 2.1 this version will have more video funtions. This will come out along with a gui for NuclearMedia. I was at this high school today and they only had borland i got NuclearMedia to work on that all you have to do is comment out <br>
#pragma comment(lib, "winmm")<br>
the next version will be comming out soon.
Original Comments (3)
Recovered from Wayback Machine