Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NARS1] [ksbhaskar] %HD should also work on hexadecimal numbers with a 0x or 0X prefix #45

Merged
merged 4 commits into from
Oct 4, 2017
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions sr_port/hd.mpt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Copyright 1987,2001 Sanchez Computer Associates, Inc. ;
; Copyright 1987,2001 Sanchez Computer Associates, Inc. ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a matter of esthetics, but the semi-colon box doesn't line up, at least on the Github review.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is likely because I have replaced the end of the line with tabs (instead of spaces) and that is how all lines in the rest of the copyright notice are. I do see all lines aligned in the actual code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

; ;
; Copyright (c) 2017 YottaDB LLC. and/or its subsidiaries. ;
; All rights reserved. ;
; ;
; This source code contains the intellectual property ;
; of its copyright holder(s), and is made available ;
Expand All @@ -11,15 +14,16 @@
%HD ;GT.M %HD utility - hexadecimal to decimal conversion program
;invoke at INT with %HD in hexadecimal to return %HD in decimal
;invoke at FUNC as an extrinsic function
;Note: Ignores any prefix of 0x or 0X
;if you make heavy use of this routine, consider $ZCALL
;
s %HD=$$FUNC(%HD)
q
INT r !,"Hexidecimal: ",%HD s %HD=$$FUNC(%HD)
q
FUNC(h)
q:$tr(h,"E","e")<0 ""
n c,d,dg
s d=0,h=$tr(h,"abcdef","ABCDEF")
f c=1:1:$l(h) s dg=$f("0123456789ABCDEF",$e(h,c)) q:'dg s d=(d*16)+(dg-2)
n c,d,dg,l
s d=0,h=$tr(h,"abcdefx","ABCDEFX"),l=$l(h)
s:("0X"=$e(h,1,2)) h=($zextract(h,3,l)),l=l-2
f c=1:1:l s dg=$f("0123456789ABCDEF",$e(h,c)) q:'dg s d=(d*16)+(dg-2)
q d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be the most efficient implementation of FUNC(h), making use of compile time evaluation of operations on constants:

FUNC(h)
        q:$t(h,"E","e")<0 ""
        new p,z
        s h=$tr(h,"0123456789ABCDEFabcdefXx",$zch(0)_$zch(1)_$zch(2)_$zch(3)_$zch(4)_$zch(5)_$zch(6)_$zch(7)_$zch(8)_$zch(9)_$zch(10)_$zch(11)_$zch(12)_$zch(13)_$zch(14)_$zch(15)_$zch(10)_$zch(11)_$zch(12)_$zch(13)_$zch(14)_$zch(15))
        s z=0,p=1/16 f i=$l(h):-1:1 s p=p*16,z=p*$a($e(h,i))+z
        q z