Solved

embedded visual basic HextoStr function

Posted on 2006-06-10
26
1,074 Views
Last Modified: 2013-11-25
I tried to rewrite the one I use in reg visual studio but that isnt going well, anybody has one that works? For some reason I cant seem to pass a return variables.

Tom
0
Comment
Question by:T0masz
  • 7
  • 6
  • 5
  • +4
26 Comments
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16880111

can you explain a little more, since the Hex() vb function does return a string....
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16880122
What exactly do you want it to do?
This will convert a string of hex characters a string of text characters.

Option Explicit


Sub CallHexToStr()
    MsgBox HexToStr("61626a")
End Sub

Function HexToStr(strHex As String) As String
    Dim i As Integer
   
    For i = 1 To Len(strHex) - 1 Step 2
        HexToStr = HexToStr & Chr$(Val("&h" & Mid$(strHex, i, 2)))
    Next i
End Function
0
 
LVL 4

Expert Comment

by:mikem
ID: 16880236
Mark,

Could you post the existing code, this will help us to understand the problem..

Mike
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16880269

there is the default hex function from vb

if you use it like this:

dim tmpStr as string

tmpStr=hex(1234)

you have a string in tmpStr representing 1234 (hexadecimal)
0
 

Author Comment

by:T0masz
ID: 16881028
I want to convert hex values to integers example hextoint('ABCD') would give me 43981
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16881117

try this one:


Private Function HextoDec(HexNum As String) As Long
    'converts a hexadecimal value to a decimal value
    'You can use the characters a-f but also A-F (in capitals)
    'for example: label1.caption = HextoDec("Ab789Ff")
    'returns as the labels caption: 179800575
    Dim xx As Long, yy As Long
    For xx = 1 To Len(HexNum)
        If Asc(Mid(HexNum, xx, 1)) < 48 Then GoTo HexError
        If Asc(Mid(HexNum, xx, 1)) > 57 And Asc(Mid(HexNum, xx, 1)) < 65 Then GoTo HexError
        If Asc(Mid(HexNum, xx, 1)) > 70 And Asc(Mid(HexNum, xx, 1)) < 97 Then GoTo HexError
        If Asc(Mid(HexNum, xx, 1)) > 102 Then GoTo HexError
    Next xx

    HextoDec = Val("&h" & HexNum)
   
    Exit Function
HexError:
    Exit Function
End Function
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 16881125
Public Function hextodec(ByVal hex As String) As Double
  Dim f As Double
  Dim r As Double
  Dim b As String
 
  f = 1
  r = 0
 
  While Len(hex) > 0
    b = Mid(hex, Len(hex), 1)
    Select Case b
      Case "0" To "9"
           r = r + f * Val(b)
      Case Else
           r = r + f * (10 + Asc(b) - Asc("A"))
    End Select
   
    f = f * 16
    hex = Left(hex, Len(hex) - 1)
  Wend
 
  hextodec = r
End Function
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 16881128
Function ConvertHexToDecimal(HString)
    accum = 0
    mult = 0
    For i = Len(HString) To 1 Step -1
        ch = Mid(HString, i, 1)
        Select Case ch
            Case "A": nmb = 10
            Case "B": nmb = 11
            Case "C": nmb = 12
            Case "D": nmb = 13
            Case "E": nmb = 14
            Case "F": nmb = 15
            'Case ch <= "9" And ch >= "0": nmb = Val(ch)
            Case Else
                If IsNumeric(ch) Then nmb = Val(ch) Else nmb = 0
                'nmb = 0
        End Select
       
        accum = accum + (nmb * (16 ^ mult))
        mult = mult + 1
    Next i
    ConvertHexToDecimal = accum
           
End Function
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 16881131
Function HexToInt(sHexVal as String) as Integer
HexToInt = Cint("&h" & sHexVal)
End Function
0
 

Author Comment

by:T0masz
ID: 16881283
Pretty much every function has gives me an error or doesnt give me the right value.

Mark_FreeSoftware : If Asc(Mid(HexNum, xx, 1)) < 48 Then GoTo HexError -<expected statement
 
JackOfPH: Case "0" To "9" -<expected statement
in the ConvertHexToDecimal Im getting undefined variables so I put Dim accu and etc but then im getting type mismatched '[undefined]'
and the last function returns -21555 for abcd
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16881293

which version of vb are you using?
0
 

Author Comment

by:T0masz
ID: 16881295
embedded visual basic 3.0
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16881335

you should have posted that in the first place,
because that is antique

i don't know enough about that version, sorry
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16881466
What value would you expect from "abcd"?
Hex$(-21555) returns "ABCD", so -21555 would seem to be correct.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16881626
How can you get 43981 from "abcd"?
Perhaps you can get give us some other examples, e.g. "FFFF", "7FFF". I would expect -1 and 32767 respectively.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16881821
@GrahamSkan...

Decimal (Base 10):
43981 --> (4 * 10^4) + (3 * 10^3) + (9 * 10 ^2) + (8 * 10^1) + (1 * 10^0)
          --> (4 * 10000) + (3 * 1000) + (9 * 100) + (8 * 10) + (1 * 1)
          --> 40000 + 3000 + 900 + 80 + 1
          --> 43981


Hexadecimal (Base 16):
0 --> 9, A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

ABCD --> (10 * 16^3) + (11 * 16^2) + (12 * 16^1) + (13 * 16^0)
         --> (10 * 4096) + (11 * 256) + (12 * 16) + (13 * 1)
         --> 40960 + 2816 + 192 + 13
ABCD --> 43981
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16882089
OK, T0masz

It seems that we are having trouble with the explanatory language, (English).  Are we talking about  0,1,2,3,4,5,6,7,8,9,A,B,C,D representing Hex half-byte characters, or about any text string, e.g, "WXY"?  Incidentally, in that string ("WXY") which would be illegal as a representation of what I would call a hex string, the numbers of characters is deliberately odd to demonstrate that they are definately not Hex pairs?
0
 

Author Comment

by:T0masz
ID: 16882368
ABCD is 43981
A is 10 etc
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16883776
So you want the Hex string to be considered unsigned.

Try this:
Sub CallUnsHexToLong()
    MsgBox UnsHexToLong("ABCD")
End Sub

Function UnsHexToLong(strHex As String) As Long 'needs to be a Long to prevent Overflow error.
    Dim strMakePositive As String
    strMakePositive = "&h1" & String(Len(strHex), "0")
    UnsHexToLong = Val("&h1" & strHex) - Val(strMakePositive)
End Function
0
 
LVL 12

Expert Comment

by:AmigoJack
ID: 16883781
modification of JackOfPH:

Function HexToInt(sHexVal As String) As Double
  While Len$(sHexVal) < 8
    sHexVal = "0" & sHexVal
  Wend
  HexToInt = CDbl("&h" & Left$(sHexVal, 4)) * 65536 + CDbl("&h" & Right$(sHexVal, 4))
End Function

this is a trick to also display up to 8 hex digits as positive numbers. T0masz, visual basic has little support for unsigned integers. normally, a range of an integer is divided into positive and negative ranges. the function here however deals with simply treating it partly as a floating point value. please note that it wont work for more than 8 hex digits - thats simply the maximum
0
 
LVL 12

Expert Comment

by:AmigoJack
ID: 16883791
one sidenote: my function not only gives 43981 for "ABCD", it also gives 2882400001 for "ABCDEF01"
0
 

Author Comment

by:T0masz
ID: 16899821
GrahamSkan: Val invalid variable
AmigoJack: Invalid character at Left$
0
 
LVL 12

Expert Comment

by:AmigoJack
ID: 16900354
dang, your visual basic seems to be WAY too old, nobody of us here knows anything about that. if it says "invalid character at left$" and means the & in front of it, simply lookup the help on some basics like "concatenating strings". you didnt complain about this with previous posts which also take use of this.

please say: what does your system look like?
- your windows version
- your <microsoft anything> office version (because you said "embedded" it seems to be included in another product)

and cant you change it to something a bit more actual?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 16900585
That context is OK for VB1 to VB6. I have used them all, and still have the VB3 diskettes, though they might not be readable any more. I suspect that it the 'embedded' bit that is giving us the problem.

If it were VB3, I would be very surprised that Val is not recognised as a function. I would also surprised that Left$ is complained about.

However, if we assumed that the code is more VBScript than VB3, that would make sense.

Leave the $ sign off the Left$() function and just use Left(), and use Clng in my code.

This is modifed for VBScript

MsgBox UnsHexToLong("ABCD")

Function UnsHexToLong(strHex)
    strMakePositive = "&h1" & String(Len(strHex), "0")
    UnsHexToLong = Clng("&h1" & strHex) - Clng(strMakePositive)
End Function
0
 

Author Comment

by:T0masz
ID: 16903633
embedded vb 3 comes in its own package.
http://www.microsoft.com/downloads/details.aspx?FamilyID=f663bf48-31ee-4cbe-aac5-0affd5fb27dd&DisplayLang=en

win2k,
GrahamSkan: that works great, I only had to add a dim for strMakepositive(ebv3 requires that u declare the variables beforehand)

Tom
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16904546
Thanks Tom, I'm glad we finally got there.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now