From: Helmut Meukel on 28 May 2010 14:02 Garry, thanks for clarifying what you really wanted. Why did you let us guess? How about this (no error handling included for odd length strings,...): Function Hex2Dec(HexString as String) as String Dim l as Long, i as Long, tmp as String l = Len(HexString) For i = 1 to l - 1 Step 2 tmp = tmp & CStr(Val("&H" & Mid(HexString, i, 2))) if i < l - 1 then tmp = tmp & "," next i Hex2Dec = tmp End Function Helmut. "GS" <gesansom(a)netscape.net> schrieb im Newsbeitrag news:htoutg$r7r$1(a)news.eternal-september.org... > Thanks to all for replying! Unfortunately, after diligently trying each, none > of the suggestions are working as expected. Here's what I'm trying to do: > > I have the following string of Hex values: > "0E0E00000505000C20450164A5A5" > > which I need to convert to a delimited string of Dec values. The expected > result is: > "14,14,0,0,5,5,0,12,32,69,1,100,165,165" > > Since the source string of Hex values are paired, a delimited version of this > would be: > "0E,0E,00,00,05,05,00,0C,20,45,01,64,A5,A5" > > I was able to get the result using an Excel WorkSheetFunction provided by the > Analyss Toolpak addin called Hex2Dec(). I would like to duplicate that > function in VB6. > > Thanks in advance, again, for any suggested solutions. > > -- > Garry > > Free usenet access at http://www.eternal-september.org > ClassicVB Users Regroup! comp.lang.basic.visual.misc > >
From: Derek on 28 May 2010 14:24 On May 28, 11:35 am, GS <gesan...(a)netscape.net> wrote: > Thanks to all for replying! Unfortunately, after diligently trying > each, none of the suggestions are working as expected. Here's what I'm > trying to do: > > I have the following string of Hex values: > "0E0E00000505000C20450164A5A5" > > which I need to convert to a delimited string of Dec values. The > expected result is: > "14,14,0,0,5,5,0,12,32,69,1,100,165,165" > > Since the source string of Hex values are paired, a delimited version > of this would be: > "0E,0E,00,00,05,05,00,0C,20,45,01,64,A5,A5" > The following function should do the job in QBASIC, QB4.5, PDS 7, VBDOS or VB1-6. I've tested it using QBASIC. It adds an initial "0" to the input string to ensure an even number of characters but doesn't bother to check that the string is valid hex. So it will accept "HIYA" or whatever without complaint. Cheers Derek -- Begin Code --------------------------------- DECLARE FUNCTION CustomHex2Dec$ (aHexSequence AS STRING) DIM dInput AS STRING DIM dOutput AS STRING LET dInput = "0E0E00000505000C20450164A5A5" LET dOutput = CustomHex2Dec(dInput) + "<" PRINT "'"; dInput; "' -> '"; dOutput; "'" SYSTEM FUNCTION CustomHex2Dec$ (aHexSequence AS STRING) DIM J AS INTEGER DIM dResult AS STRING LET dResult = MID$("0", 1, LEN(aHexSequence) MOD 2) + aHexSequence FOR J = 1 TO LEN(dResult) STEP 2 LET dResult = dResult + "," + LTRIM$(STR$(VAL("&H" + MID$(dResult, J, 2)))) NEXT LET CustomHex2Dec = MID$(dResult, INSTR(dResult, ",") + 1) END FUNCTION -- End Code ---------------------------------------
From: dpb on 28 May 2010 15:07 GS wrote: .... > Since the source string of Hex values are paired, a delimited version of > this would be: > "0E,0E,00,00,05,05,00,0C,20,45,01,64,A5,A5" .... You didn't say that, now did you? See Helmut's function for one solution... The point to be made is unless you describe the problem accurately it's highly unlikely to get a useful response. How was anybody to know from your initial posting you had a composite list of 16-bit hex digits you wanted to parse? --
From: GS on 28 May 2010 15:27 Helmut Meukel presented the following explanation : > Garry, > > thanks for clarifying what you really wanted. > Why did you let us guess? > How about this (no error handling included for odd length strings,...): > > Function Hex2Dec(HexString as String) as String > Dim l as Long, i as Long, tmp as String > l = Len(HexString) > For i = 1 to l - 1 Step 2 > tmp = tmp & CStr(Val("&H" & Mid(HexString, i, 2))) > if i < l - 1 then tmp = tmp & "," > next i > Hex2Dec = tmp > End Function > > Helmut. Thanks so much, Helmut! Sorry for not being more detailed in my OP. I was actually trying to figure out how to combine the Auric_ & Henning suggestions to achieve what you've done here, which is most excellent. What's interesting is how I arrived at the result using the Analysis Toolpak addin in Excel. Here's my VB[A] code: Function SendHexString() As String Const vHexString As Variant = "0E0E00000505000C20450164A5A5" Dim s1 As String, c As Range Dim i As Integer, j As Integer For i = 1 To Len(vHexString) Step 2 s1 = "=hex2dec(""" & Mid(vHexString, i, 2) & """)" j = j + 1 Cells(j, 1).FormulaR1C1 = s1 Next For Each c In ActiveSheet.UsedRange SendHexString = SendHexString & CStr(c.value) Next End Function Amazing similarity, but not how I want to do (way too much work...) it since my VB6 project needs to run stand-alone and not dependant on Excel or its addins. I don't actually need the resulting string to be delimited, but thanks for providing a variation on how to handle the extra comma. (I would have used 'tmp = tmp & "," & CStr(Val("&H" & Mid(HexString, i, 2)))' to construct the string, then used 'Hex2Dec = Mid$(tmp, 2)' to return the value minus the leading comma. <IMO>The function runs a bit faster not having to evaluate the 'If...then'. Not an issue! Here's my final solution: Function Hex2Dec(HexString As String) As String Dim i As Long For i = 1 To l - 1 Step 2 Hex2Dec = Hex2Dec & CStr(Val("&H" & Mid(HexString, i, 2))) Next i End Function Thanks again! -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc
From: GS on 28 May 2010 15:37
dpb explained : > GS wrote: > ... > >> Since the source string of Hex values are paired, a delimited version of >> this would be: >> "0E,0E,00,00,05,05,00,0C,20,45,01,64,A5,A5" > ... > > You didn't say that, now did you? > > See Helmut's function for one solution... > > The point to be made is unless you describe the problem accurately it's > highly unlikely to get a useful response. How was anybody to know from your > initial posting you had a composite list of 16-bit hex digits you wanted to > parse? Point taken, ..and thanks! Sorry for my lack of providing detail. I will be sure to provide more detail in future. I was hoping I would get enough examples to figure it out on my own based on a sample of handling a single Hex val. I realized this was a poor decision after reading the replies. See my reply to Helmut's post for more details, and what details of Helmut's solution I finally decided to go with. regards, -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc |