Convert Python hex2base16 (INT Function) to PB

Just starting out? Need help? Post your questions and find answers here.
zikitrake
Addict
Addict
Posts: 833
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Convert Python hex2base16 (INT Function) to PB

Post by zikitrake »

My python code:

Code: Select all

feature_id = 'a30d7f68fdfd53e6'
cid = str(int(feature_id, 16))
print(cid)
Result: 11749187091794056166

View online: https://onecompiler.com/python/3yptfxn82


I tried with this code (@idle and @wilbert) but the result is negative:
viewtopic.php?p=546681#p546681

Code: Select all

Procedure.q ConvertToDecimal(*n.Character, base) 
  Protected d.u, r.q
  Repeat
    d = *n\c - 48
    If d > 9
      d | 32 : If d > 48 : d - 39 : EndIf
    EndIf
    If d >= base : Break : EndIf
    r = (r * base) + d
    *n + SizeOf(Character)
  ForEver
  ProcedureReturn r 
EndProcedure 

h$ = "a30d7f68fdfd53e6"
n = ConvertToDecimal(@h$,16)
Debug n
Result: -6697556981915495450 in PBx64

Any idea? Thank you in advance!
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Convert Python hex2base16 (INT Function) to PB

Post by Marc56us »

(Removed by author : The answer of STARGÅTE below is better)
Last edited by Marc56us on Wed Nov 23, 2022 3:10 pm, edited 6 times in total.
User avatar
STARGÅTE
Addict
Addict
Posts: 2063
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Convert Python hex2base16 (INT Function) to PB

Post by STARGÅTE »

PB gives the signed result. Use StrU() for the unsigned result:

Code: Select all

h$ = "a30d7f68fdfd53e6"
n = ConvertToDecimal(@h$,16)
Debug StrU(n)
You can also use Val() to convert the hex-string:

Code: Select all

h$ = "a30d7f68fdfd53e6"
n = Val("$"+h$)
Debug StrU(n)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
zikitrake
Addict
Addict
Posts: 833
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Re: Convert Python hex2base16 (INT Function) to PB

Post by zikitrake »

Thank you! @Stargäte and @Marc56us
User avatar
skywalk
Addict
Addict
Posts: 3960
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Convert Python hex2base16 (INT Function) to PB

Post by skywalk »

Some Macro's I use.

Code: Select all

Macro BinI(i_int, nBits=16)
  "%" + RSet(Bin(i_int), nBits, "0")
EndMacro
Macro HexI(i_int, nBits=16)
  "$" + RSet(Hex(i_int), nBits, "0")
EndMacro
h$ = "$" + "A30D7F68FDFD53E6"
I = Val(h$)
Debug StrU(I,#PB_Quad)
Debug HexI(I,16)
Debug BinI(I,64)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
zikitrake
Addict
Addict
Posts: 833
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Re: Convert Python hex2base16 (INT Function) to PB

Post by zikitrake »

Thank you @skywalk. Added to my pb templates :!:
juergenkulow
Enthusiast
Enthusiast
Posts: 540
Joined: Wed Sep 25, 2019 10:18 am

Re: Convert Python hex2base16 (INT Function) to PB

Post by juergenkulow »

The largest quad number is 9223372036854775807 and 11749187091794056166 is unfortunately larger.
Lizard
Decimal.pbi
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Convert Python hex2base16 (INT Function) to PB

Post by Olli »

juergenkulow wrote: Thu Nov 24, 2022 9:33 am The largest quad number is 9223372036854775807 and 11749187091794056166 is unfortunately larger.
Lizard
Decimal.pbi
yes, the largest signed quad is near 8 exaunits.
And the largest unsigned quad is near 16 exaunits.

Very good tip I forgot with StrU().

Thank you
Post Reply