|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Excel decimal to binary (32 bit) conversion
Hi guys. I'm trying to write an Excel function that will take a 32 bit integer and calculate its binary equivalence, however I have very little experience with VisualBasic and was wondering if somebody could give me an idea of how I'd go about doing this.
Thanks. |
|
#2
|
|||
|
|||
|
I've not tried something like this, but one way that comes to mind (untried) is to use the hex function on the number and parse the resulting hex characters in the string, and build a new string with the binary equivalent of the hex characters.
|
|
#3
|
|||
|
|||
|
That's exactly what I wound up doing, and I have this really long string of Excel commands daisy-chained together which will give me what I want, and it works.
But you have to change two numbers within that very long chain of commands depending on which bit of the 32 bit integer you want to look at. Right now I've opened up the VB editor in Excel and have written the following code in a new module: Code:
Function BitState(Value As Long, Bit As Integer)
Dim Y As Integer
Dim Z As Integer
If ((Bit >= 0) And (Bit <= 3)) Then
Y = 1
Z = (Bit Mod 4) + 1
End If
If ((Bit >= 4) And (Bit <= 7)) Then
Y = 2
Z = (Bit Mod 4) + 1
End If
If ((Bit >= 8) And (Bit <= 11)) Then
Y = 3
Z = (Bit Mod 4) + 1
End If
If ((Bit >= 12) And (Bit <= 15)) Then
Y = 4
Z = (Bit Mod 4) + 1
End If
If ((Bit >= 16) And (Bit <= 19)) Then
Y = 5
Z = (Bit Mod 4) + 1
End If
If ((Bit >= 20) And (Bit <= 23)) Then
Y = 6
Z = (Bit Mod 4) + 1
End If
If ((Bit >= 24) And (Bit <= 27)) Then
Y = 7
Z = (Bit Mod 4) + 1
End If
If ((Bit >= 28) And (Bit <= 31)) Then
Y = 8
Z = (Bit Mod 4) + 1
End If
BitState = Left(Right(HEX2BIN(Left(Right(DEC2HEX(Value, 8), Y)), 4), Z))
End Function
But when I try to compile it, I get this: "Compile Error: Sub or Function Undefined" and it highlights the "DEC2HEX" function. I thought you could use Excel standard macros within my function. So I'm stuck here. I don't know what to do. Last edited by m0dr0cker : October 9th, 2003 at 02:53 PM. |
|
#4
|
|||
|
|||
|
Try to Use hex function to replace the DEC2HEX function..
|
|
#5
|
|||
|
|||
|
I can't find any help on the hex function in Excel.
|
|
#6
|
|||
|
|||
|
In Excel 2000, open the Help table of contents (I'm not sure how you get there if the annoying little clippy is in the way
![]() Find the next-to-last chapter "Programming Information" Open the "Visual Basic Language Reference" sub-chapter. Open Functions, H-L and find the documentation on the HEX function. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Excel decimal to binary (32 bit) conversion |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|