Page 1 of 1
ValHex() got broken -- Curious why
Posted: Tue Oct 08, 2024 9:16 pm
by Randy Walker
I understand Val() is able to replace the once necessary ValHex() procedure, but I am curious why every hex value I send to that procedure returns -19 as the result. I could not find the post where I got that valhex procedure but can find same procedure here in the PB forums under name Hex2Dec / How did it get so broken it always returns -19?
Code: Select all
Procedure.l ValHex(n.s)
Structure OneByte
a.B
EndStructure
*t.OneByte = @n.s
Result.l = 0
While *t\a <> 0
If *t\a >= '0' And *t\a <= '9'
Result = (Result << 4) + (*t\a - 48)
ElseIf *t\a >= 'A' And *t\a <= 'F'
Result = (Result << 4) + (*t\a - 55)
ElseIf *t\a >= 'a' And *t\a <= 'f'
Result = (Result << 4) + (*t\a - 87)
Else
Result = (Result << 4) + (*t\a - 55)
EndIf
*t + 1
Wend
ProcedureReturn Result
EndProcedure
Re: ValHex() got broken -- Curious why
Posted: Tue Oct 08, 2024 9:26 pm
by Mr.L
try it this way
Code: Select all
Procedure.q ValHex(n.s)
*t.Character = @n.s
Result.q = 0
While *t\c <> 0
If *t\c >= '0' And *t\c <= '9'
Result = (Result << 4) + (*t\c - 48)
ElseIf *t\c >= 'A' And *t\c <= 'F'
Result = (Result << 4) + (*t\c - 55)
ElseIf *t\c >= 'a' And *t\c <= 'f'
Result = (Result << 4) + (*t\c - 87)
Else
Result = (Result << 4) + (*t\c - 55)
EndIf
*t + SizeOf(Character)
Wend
ProcedureReturn Result
EndProcedure
Debug ValHex("12345ABCDEF")
Debug Val("$12345ABCDEF")
Re: ValHex() got broken -- Curious why
Posted: Tue Oct 08, 2024 9:39 pm
by infratec
Since a long time, strings are stored in unicode inside of PB.
Your code is for strings stored in ASCII format.
If i is not time critical, use the way Mr.L showed already:
Re: ValHex() got broken -- Curious why
Posted: Tue Oct 08, 2024 9:58 pm
by Randy Walker
infratec wrote: Tue Oct 08, 2024 9:39 pm
Since a long time, strings are stored in unicode inside of PB.
Your code is for strings stored in ASCII format.
Ok, that explains it, I guess. Unicode is still a bit of a mystery to me.
yes I already tested and learned val() is functional as noted in my OP above.
THANKS infratec!!!
Re: ValHex() got broken -- Curious why
Posted: Wed Oct 09, 2024 10:48 am
by Axolotl
Hi Randy,
Randy Walker wrote: Tue Oct 08, 2024 9:58 pm
Ok, that explains it, I guess. Unicode is still a bit of a mystery to me.
You can do something like this inside the procedure ValHex() to see the memory usage....
Code: Select all
ShowMemoryViewer(@n, StringByteLength(n))
Just another minor hint:
I would never define a structure within a procedure. Even if the compiler is fine with that.
Why?
This approach would suggest that the structure is local.
But it is not!
Therefore my recommendation: structure definitions belong at the beginning of the file (or at least the code.)
Code: Select all
; inserted after the procedure to demonstrate what has just been written.
Define tt.OneByte
tt\a = 254
Debug tt\a
Re: ValHex() got broken -- Curious why
Posted: Wed Oct 09, 2024 8:42 pm
by Randy Walker
Axolotl wrote: Wed Oct 09, 2024 10:48 am
Hi Randy,
Randy Walker wrote: Tue Oct 08, 2024 9:58 pm
Ok, that explains it, I guess. Unicode is still a bit of a mystery to me.
I would never define a structure within a procedure. Even if the compiler is fine with that.
I didn't put it there. It came that way when I only copied the code from one of the posts here in the forums. I thought it was a little strange to see the structure inside the procedure but it worked so I went with it as it was. Structures, Types and callbacks are all huge mysteries. Looking at threads now and that's got my brain majorly torqued. Anything more than a straight for next loop is a challenge. I feel really lucky when I get a Win API to work. My good old GFA Basic allowed me to do x = Not x to toggle a variable and now i'm lost because that doesn't work here.
Re: ValHex() got broken -- Curious why
Posted: Wed Oct 09, 2024 9:19 pm
by mk-soft
My good old GFA Basic allowed me to do x = Not x to toggle a variable and now i'm lost because that doesn't work here.
Code: Select all
Define x
x = #True
If x = #True
Debug "True"
Else
Debug "False"
EndIf
x = Bool(Not x)
If x = #True
Debug "True"
Else
Debug "False"
EndIf
x = Bool(Not x)
If x = #True
Debug "True"
Else
Debug "False"
EndIf
Re: ValHex() got broken -- Curious why
Posted: Wed Oct 09, 2024 9:45 pm
by miso
Also toggles #true / #False states:
Re: ValHex() got broken -- Curious why
Posted: Wed Oct 09, 2024 9:47 pm
by idle
if you ever need the values larger than 64bits you can use this.
viewtopic.php?t=83378
Re: ValHex() got broken -- Curious why
Posted: Wed Oct 09, 2024 10:09 pm
by infratec
Toggling
A binary version:
Code: Select all
x = #False
Debug x
x ! 1
Debug x
x ! 1
Debug x
This should be faster then substracting or call the Bool() procedure.
As Macro:
Code: Select all
Macro Toggle(x)
x ! 1
EndMacro
x = #False
Debug x
Toggle(x)
Debug x
Toggle(x)
Debug x
Re: ValHex() got broken -- Curious why
Posted: Wed Oct 09, 2024 10:36 pm
by miso
A binary version:
Nice, thanks. I update my future 'toggle' codes with that one

Re: ValHex() got broken -- Curious why
Posted: Thu Oct 10, 2024 4:28 am
by Randy Walker
mk-soft wrote: Wed Oct 09, 2024 9:19 pm
My good old GFA Basic allowed me to do x = Not x to toggle a variable and now i'm lost because that doesn't work here.
Code: Select all
x = Bool(Not x)
If x = #True
Debug "True"
Else
Debug "False"
EndIf
OK:
Code: Select all
Result = Bool(<boolean expression>)
That one was hard to find -- buried deep in the help file.
Thanks for that Tip mk-soft!!!