• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1102

# 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
0
T0masz
• 7
• 6
• 5
• +4
1 Solution

Commented:

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

RetiredCommented:
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

Commented:
Mark,

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

Mike
0

Commented:

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 Commented:
I want to convert hex values to integers example hextoint('ABCD') would give me 43981
0

Commented:

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

Commented:
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

Commented:
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

Commented:
Function HexToInt(sHexVal as String) as Integer
HexToInt = Cint("&h" & sHexVal)
End Function
0

Author Commented:
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

Commented:

which version of vb are you using?
0

Author Commented:
embedded visual basic 3.0
0

Commented:

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

i don't know enough about that version, sorry
0

RetiredCommented:
What value would you expect from "abcd"?
Hex\$(-21555) returns "ABCD", so -21555 would seem to be correct.
0

RetiredCommented:
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

Middle School Assistant TeacherCommented:
@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

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

RetiredCommented:
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 Commented:
ABCD is 43981
A is 10 etc
0

RetiredCommented:
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

Commented:
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

Commented:
one sidenote: my function not only gives 43981 for "ABCD", it also gives 2882400001 for "ABCDEF01"
0

Author Commented:
GrahamSkan: Val invalid variable
AmigoJack: Invalid character at Left\$
0

Commented:
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.

- 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

RetiredCommented:
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 Commented:
embedded vb 3 comes in its own package.

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

Tom
0

RetiredCommented:
Thanks Tom, I'm glad we finally got there.
0

## Featured Post

• 7
• 6
• 5
• +4
Tackle projects and never again get stuck behind a technical roadblock.