ValHex() got broken -- Curious why

Just starting out? Need help? Post your questions and find answers here.
Randy Walker
Addict
Addict
Posts: 1009
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

ValHex() got broken -- Curious why

Post 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
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Mr.L
Enthusiast
Enthusiast
Posts: 146
Joined: Sun Oct 09, 2011 7:39 am

Re: ValHex() got broken -- Curious why

Post 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")
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ValHex() got broken -- Curious why

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

Code: Select all

Debug Val("$" + Hex$)
Randy Walker
Addict
Addict
Posts: 1009
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: ValHex() got broken -- Curious why

Post 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. :oops:

yes I already tested and learned val() is functional as noted in my OP above.

THANKS infratec!!!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Axolotl
Addict
Addict
Posts: 819
Joined: Wed Dec 31, 2008 3:36 pm

Re: ValHex() got broken -- Curious why

Post 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. :oops:

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 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Randy Walker
Addict
Addict
Posts: 1009
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: ValHex() got broken -- Curious why

Post 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. :oops:

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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
mk-soft
Always Here
Always Here
Posts: 6226
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ValHex() got broken -- Curious why

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
miso
Enthusiast
Enthusiast
Posts: 459
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: ValHex() got broken -- Curious why

Post by miso »

Also toggles #true / #False states:

Code: Select all

x = 1-x
User avatar
idle
Always Here
Always Here
Posts: 5864
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: ValHex() got broken -- Curious why

Post by idle »

if you ever need the values larger than 64bits you can use this.
viewtopic.php?t=83378
infratec
Always Here
Always Here
Posts: 7598
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ValHex() got broken -- Curious why

Post 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
miso
Enthusiast
Enthusiast
Posts: 459
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: ValHex() got broken -- Curious why

Post by miso »

A binary version:
Nice, thanks. I update my future 'toggle' codes with that one ;)
Randy Walker
Addict
Addict
Posts: 1009
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: ValHex() got broken -- Curious why

Post 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!!!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Post Reply