From: GS on
Oops! Seem to have lost some code somehow. Final solution is:

Function Hex2Dec(HexString As String) As String
Dim i As Long
For i = 1 To Len(HexString) Step 2
Hex2Dec = Hex2Dec & CStr(Val("&H" & Mid(HexString, i, 2)))
Next i
End Function

Error handling is done in the caller before passing the HexString var.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


From: Mike Williams on
"GS" <gesansom(a)netscape.net> wrote in message
news:htp75e$69q$1(a)news.eternal-september.org...
> Oops! Seem to have lost some code somehow. Final solution is: Function
> Hex2Dec(HexString As String) As String
> Dim i As Long
> For i = 1 To Len(HexString) Step 2
> Hex2Dec = Hex2Dec & CStr(Val("&H" & Mid(HexString, i, 2)))
> Next i
> End Function

Are you certain that's exactly what you want? It doesn't look right to me.
The reason I'm saying that is because given the output string it is actually
impossible to reliably discover what the original input hex string was, even
if you know the exact length of the original input hex string, and
especially if you do not, which is ringing alarm bells here. Are you
absolutely sure it is what you want?

Mike



From: GS on
Mike Williams laid this down on his screen :
> "GS" <gesansom(a)netscape.net> wrote in message
> news:htp75e$69q$1(a)news.eternal-september.org...
>> Oops! Seem to have lost some code somehow. Final solution is: Function
>> Hex2Dec(HexString As String) As String
>> Dim i As Long
>> For i = 1 To Len(HexString) Step 2
>> Hex2Dec = Hex2Dec & CStr(Val("&H" & Mid(HexString, i, 2)))
>> Next i
>> End Function
>
> Are you certain that's exactly what you want? It doesn't look right to me.
> The reason I'm saying that is because given the output string it is actually
> impossible to reliably discover what the original input hex string was, even
> if you know the exact length of the original input hex string, and especially
> if you do not, which is ringing alarm bells here. Are you absolutely sure it
> is what you want?
>
> Mike

Hi Mike,
Thanks for stepping in! Helmut's solution exactly reproduces what the
Excel addin does, and so for this purpose it's what I want.

However, I understand your point. It's imperitive that the output
string is able to be converted back to the input string. I did some
testing and discovered what you suggest is exactly the case. Problem
lies with zeros. Seems that converting the result back to hex vals
returns this:
"EE0000371171A5BA141105"

I'm thinking that I should retain the delimited values and dump it into
an array using Split(). This would, at least, preserve the individual
values return by the Hex2Dec function. Problem is, as you say, how to
determine what the original string was from the resulting string. For
example, returning Hex(14) returns "E" and not "0E".

Any suggestions you have would be greatly appreciated.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


From: Derek on
On May 28, 4:13 pm, GS <gesan...(a)netscape.net> wrote:
> I'm thinking that I should retain the delimited values and dump it into
> an array using Split(). This would, at least, preserve the individual
> values return by the Hex2Dec function. Problem is, as you say, how to
> determine what the original string was from the resulting string. For
> example, returning Hex(14) returns "E" and not "0E".
>
> Any suggestions you have would be greatly appreciated.
>

Returning RIGHT$(HEX$(256 + 14), 2) would return "0E"

Cheers

Derek
From: GS on
Derek submitted this idea :
> On May 28, 4:13�pm, GS <gesan...(a)netscape.net> wrote:
>> I'm thinking that I should retain the delimited values and dump it into
>> an array using Split(). This would, at least, preserve the individual
>> values return by the Hex2Dec function. Problem is, as you say, how to
>> determine what the original string was from the resulting string. For
>> example, returning Hex(14) returns "E" and not "0E".
>>
>> Any suggestions you have would be greatly appreciated.
>>
>
> Returning RIGHT$(HEX$(256 + 14), 2) would return "0E"
>
> Cheers
>
> Derek

Thanks Derek! This works great! Sorry if I ignored your original reply
but it seemed too daunting to get involved with converting it to VB6
syntax. Helmut's solution was an easier option and so I went with a
modified version of it. Here's the resulting functions that I came up
with using both your's and Helmut's suggestions:

Function Hex2Dec(HexString As String) As String
' Builds a delimited DecString from a source HexString
Dim i As Long
For i = 1 To Len(HexString) Step 2
Hex2Dec = Hex2Dec & "," & CStr(Val("&H" & Mid(HexString, i, 2)))
Next i
Hex2Dec = Mid(Hex2Dec, 2)
End Function
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HexString "0E0E00000505000C20450164A5A5"
Returns DecString: "14,14,0,0,5,5,0,12,32,69,1,100,165,165"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Function Dec2Hex(DecString As String) As String
' Restores a delimited DecString to its original HexString
Dim i As Integer, v As Variant
v = Split(DecString, ",")
For i = LBound(v) To UBound(v)
Dec2Hex = Dec2Hex & Right(Hex(256 + CLng(v(i))), 2)
Next
End Function
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DecString "14,14,0,0,5,5,0,12,32,69,1,100,165,165"
Returns HexString "0E0E00000505000C20450164A5A5"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Your input is very much appreciated!

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc