HEX number to item

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

Just wondering if anyone can help me out here.. I am trying to convert a HEX number to an item..

Here is my list:

1 = 0100
2 = 0200
3 = 0400
4 = 0800
5 = 1000
6 = 2000
7 = 4000
8 = 8000
9 = 0001
10 = 0002
11 = 0004
12 = 0008
13 = 0010
14 = 0020
15 = 0040
16 = 0080

The part I can't work out is to match the HEX number to the numbers

Lets say I have a number 40 (HEX) that would mean I have to show 7.
But if I have a number C0 (HEX) that would mean I have to show 7 & 8.

Anyone able to help as I can't work it out?
 
Hello,

I don't think I'm doing this correct but can anyone help me out trying to fix my code so it does work..

Code: Copy
    Sub Display_num() As String
        Dim myData As String
         myData = "c000"
         
      Dim myNumber As Int        
         myNumber = "16"
      
      Dim Display As String 
         Display = ""
      
        Dim intCounter As Int 
        Dim intBit As Int

        If myData = 0 Then Return "display nothing"
        intBit = 1
        For intCounter = 1 To myNumber
            If myData & intBit > 0 Then
                Display = intCounter
                If intCounter < myNumber Then Msgbox (Display, "show")
            End If
            intBit = intBit * 2
        Next

    End Sub

based on my example it should only display 2 message boxs:
Message1 = 7
Message2 = 8
 
Upvote 0
you can use an array or a map.
for e.g
Code: Copy
dim reply(17)
reply(1)="0100"
reply(2)="0200"
and so on :-)
 
Upvote 0
I think that this is what you are looking for:
Code: Copy
Sub Activity_Create(FirstTime As Boolean)
   Log(ConvertToInt("0100"))
   Log(ConvertToInt("0200"))
   Log(ConvertToInt("0080"))
End Sub

Sub ConvertToInt(h As String) As Int
   Dim hex As Int
   hex = Bit.ParseInt(h.SubString2(2, 4) & h.SubString2(0,2), 16)
   Return Logarithm(hex, 2) + 1
End Sub
 
Upvote 0
Hello,

Its not quite what I was after.

If my number is C0 (hex) then the log should show 7 & 8

as 4000 + 8000 = C0 (all added in hex, and the numbers you add are in the list in my first post.)

Now I need a way to convert it back the other way so I know what numbers to display.

C0 = 7 & 8 (4000, 8000)

If I use Erels example is only displays 8 (7 is not shown) when I use the following code:

Code: Copy
Log(ConvertToInt("C000"))

Where it should show 7 & 8 in this example.
 
Upvote 0
Isn't it 40+80=c0 ? I mean, why do you denote it as 4000 and 8000? And if so, when you write 0080, you mean 8000 in hex? when you write 0001, you mean 100 in hex?
 
Upvote 0
Anyway, perhaps this would do it:
Code: Copy
Dim a As String
Dim b As String
Dim c As String 
c=""
a="c0"
b=Bit.ToBinaryString (Bit.ParseInt(a,16))
For k=b.Length -1 To 0 Step-1
If b.CharAt (k)="1" Then c=c & (b.Length -k)
Next
Msgbox(c,"ok")
If for some reason, you wish to write c0 as c000, you should obviously replace
Code: Copy
a="c0"
with
Code: Copy
a="c000"
and then add
Code: Copy
a=a.SubString (2) & a.SubString2(0,2)
 
Last edited:
Upvote 0
Back
Top