Link to home
Start Free TrialLog in
Avatar of T0masz
T0masz

asked on

embedded visual basic HextoStr function

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
Avatar of Mark_FreeSoftware
Mark_FreeSoftware
Flag of Netherlands image


can you explain a little more, since the Hex() vb function does return a string....
Avatar of GrahamSkan
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
Mark,

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

Mike

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)
Avatar of T0masz
T0masz

ASKER

I want to convert hex values to integers example hextoint('ABCD') would give me 43981

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
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
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
Function HexToInt(sHexVal as String) as Integer
HexToInt = Cint("&h" & sHexVal)
End Function
Avatar of T0masz

ASKER

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

which version of vb are you using?
Avatar of T0masz

ASKER

embedded visual basic 3.0

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

i don't know enough about that version, sorry
What value would you expect from "abcd"?
Hex$(-21555) returns "ABCD", so -21555 would seem to be correct.
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.
@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
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?
Avatar of T0masz

ASKER

ABCD is 43981
A is 10 etc
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
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
one sidenote: my function not only gives 43981 for "ABCD", it also gives 2882400001 for "ABCDEF01"
Avatar of T0masz

ASKER

GrahamSkan: Val invalid variable
AmigoJack: Invalid character at Left$
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?
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of T0masz

ASKER

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
Thanks Tom, I'm glad we finally got there.