Posts: 3
Threads: 1
Joined: Jun 2013
I recently became the owner of HP-16c and I want make a program to convert hexadecimal number to decimal by a special rule.
I use the following formulas in Excel:
- Remove in the hexadecimal number the last two digits from left:
=LEFT(A2,(LEN(A2)-2))
- Converts HEX to DEC:
=HEX2DEC(B2)
- Remove first digits starting with the “1“ for such a rule
=IF(LEFT(C2,4)="1000",RIGHT(C2,1),
(IF(LEFT(C2,3)="100",RIGHT(C2,2),
(IF(LEFT(C2,2)="10",RIGHT(C2,3),
(IF(LEFT(C2,1)="1",RIGHT(C2,4),
"???")))))))
For example:
HEX DEC
285801 328
271101 1
3e7F03 5999
3f7F03 6255
ae01 174
f02 15
3dA01 ???
3f3e02 6190
2b6799 1111
But I have difficulty trying to program HP-16c begin at first step-how to remove two digit? Shifting Bits, or...?
Posts: 325
Threads: 18
Joined: Jul 2006
That part is easy. Simply shift (not rotate) right eight bits. Converting from hex to decimal is also easy.
Stripping off leading ones from a decimal number is somewhat harder. Divide by 1000, see if the quotient is 1, divide by 100, see if the quotient is 1, etc.
Posts: 735
Threads: 34
Joined: May 2007
Or maybe:
- Subtract 1000
- Leave if it is between 0 and 1000
- Add 1000
- Subtract 100
- Leave if it is between 0 and 100
- Add 100
(...)
This is the program that illustrates the algorithm:
#!/usr/bin/python
def remove(n):
k = 1000
while k > 0:
n -= k
if 0 <= n < k:
break
n += k
k /= 10
return n
for n in (0, 1, 3, 13, 45, 114, 144, 456, 1456, 1018, 3255):
print "%10d -> %10d" % (n, remove(n))
0 -> 0
1 -> 0
3 -> 3
13 -> 3
45 -> 45
114 -> 14
144 -> 44
456 -> 456
1456 -> 456
1018 -> 18
3255 -> 3255
This is the program for the HP-16C:
001 - 43,22, A LBL A
002 - 1 1
003 - 0 0
004 - 0 0
005 - 10 /
006 - 24 DEC
007 - 1 1
008 - 0 0
009 - 0 0
010 - 0 0
011 - 0 0
012 - 43,22, 0 LBL 0
013 - 30 -
014 - 43 2 x<0
015 - 22 1 GTO 1
016 - 43 36 LSTx
017 - 43 1 x<=y
018 - 22 2 GTO 2
019 - 22 3 GTO 3
020 - 43,22, 1 LBL 1
021 - 43 36 LSTx
022 - 43,22, 2 LBL 2
023 - 40 +
024 - 43 36 LSTx
025 - 1 1
026 - 0 0
027 - 10 /
028 - 43 30 x>0
029 - 22 0 GTO 0
030 - 43,22, 3 LBL 3
031 - 33 Rv
032 - 43 21 RTN
My table doesn't agree in all cases:
INPUT OUTPUT
285801 h 328 d
271101 h 1 d
3E7F03 h 5999 d
3F7F03 h 6255 d
AE01 h 74 d
F02 h 5 d
3DA01 h 986 d
3F3E02 h 6190 d
2B6799 h 1111 d
Nevertheless I hope this is what you were looking for.
Kind regards
Thomas
Edited: 3 June 2013, 2:58 p.m.
Posts: 735
Threads: 34
Joined: May 2007
Quote:
Stripping off leading ones from a decimal number
As the examples ae01 and f02 indicate that's not what Leonid wants. But then I wonder, what's happening with this formula in Excel with say 100037: is it just 7 as IF(LEFT(C2,4)="1000",RIGHT(C2,1) indicates? But then this isn't the description:
Quote:
Remove first digits starting with the “1“ for such a rule
Or is the length of the decimal numbers somehow limited to 5 digits? Do we just have to remove the first digit if the decimal number exceeds 9999? This program produces the same results as your table with the exception of 3DA01 h for which it returns 986 d:
001 - 43,22, A LBL A
002 - 1 1
003 - 0 0
004 - 0 0
005 - 10 /
006 - 24 DEC
007 - 1 1
008 - 0 0
009 - 0 0
010 - 0 0
011 - 0 0
012 - 42 9 RMD
013 - 43 21 RTN
However this will disagree with Excel for 6423 h as the result is 100 d and not 0 (or rather "00").
Now I'm curious what this program is supposed to do. And then I guess the most difficult part will be to produce three question marks.
Cheers
Thomas
Edited: 3 June 2013, 4:06 p.m.
Posts: 325
Threads: 18
Joined: Jul 2006
Quote:
that's not what Leonid wants
I stand corrected. If the leading digit is one then the number of immediately following zeroes determine the number of digits of the result.
Thomas, as for the discrepancies in your previous table: for ae01, strip the two rightmost digits to get ae, convert to decimal to get 174, which starts with "1" so retain the rightmost four digits 0174 or 174. For f02, strip to get f, convert to get 15, which again starts with "1" so the rightmost four digits gives 0015 or 15. In neither case does the "1" get removed.
Posts: 735
Threads: 34
Joined: May 2007
I think I understand the Excel formula and I know that both of my programs don't give the same result in all cases. But I still wonder why 3E800 h, shifted and converted to 1000 d should result in 0 d while A00 h, shifted and converted to 10 d rests as it is. Does that make sense to you?
Kind regards
Thomas
Edited: 3 June 2013, 4:37 p.m.
Posts: 325
Threads: 18
Joined: Jul 2006
Quote:
But I still wonder why 3E800 h, shifted and converted to 1000 d should result in 0 d while A00 h, shifted and converted to 10 d rests as it is. Does that make sense to you?
1000, because it begins "1000", is reduced to the rightmost digit to give "0". 10, because it begins "10", is "reduced" to the rightmost three digits to give "010" or "10".
It makes sense according to the rules. I don't see the sense or reason behind the rules, however. Perhaps Leonid could explain what this is all in pursuit of.
Posts: 3
Threads: 1
Joined: Jun 2013
Thank you, short program is good solution for me
(Sorry I have not completely described, but you have correctly guessed conditions. This is a practical problem, and not all the possible numbers will be used in it)
By the way, for causes "???" (3dA01). HP-16c has something visual, like Flag 9 in HP-15c?
Posts: 735
Threads: 34
Joined: May 2007
Quote:
short program is good solution for me
I'm just glad you don't need the equivalent of the Excel-function because that would have been difficult to implement.
I'm not aware that you could make the HP-16C blink as the HP-15C but you can still set flags 4 or 5 to display either the
carry- or the out-of-range-annunciator.
This program sets the G-annunciator for 3DA01 h:
001 - 43,22, A LBL A
002 - 1 1
003 - 0 0
004 - 0 0
005 - 10 /
006 - 24 DEC
007 - 1 1
008 - 0 0
009 - 0 0
010 - 0 0
011 - 0 0
012 - 43 3 x>y
013 - 22 3 GTO 3
014 - 42 9 RMD
015 - 43 21 RTN
016 - 43,22, 0 LBL 0
017 - 30 -
018 - 43 2 x<0
019 - 22 1 GTO 1
020 - 43 36 LSTx
021 - 43 1 x<=y
022 - 22 2 GTO 2
023 - 40 +
024 - 43 21 RTN
025 - 43,22, 1 LBL 1
026 - 43 36 LSTx
027 - 43,22, 2 LBL 2
028 - 40 +
029 - 43 36 LSTx
030 - 43,22, 3 LBL 3
031 - 1 1
032 - 0 0
033 - 10 /
034 - 43 30 x>0
035 - 22 0 GTO 0
036 - 43, 4, 5 SF 5
037 - 33 Rv
038 - 43 21 RTN
Cheers
Thomas
Edited: 3 June 2013, 10:22 p.m.
Posts: 528
Threads: 40
Joined: Dec 2008
Shift right 8 bits, then subtract 10,000(decimal). I haven't used a 16C so I don't know the details of programming it, but that's the idea.
Dave
|