HP Forums

Full Version: (50g) Decimal IP address to binary octets- HP-50G
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
For an IP address such as converts to binary with leading zeros- useful for subnetting.

Place the IP address on the stack:

4: 172
3: 100
2: 254
1: 4

Run the program and the result is:

4: "10101100"
3: "01100100"
2: "11111110"
1: "00000100"

<< → a b c d @put the contents of the stack in variables a b c d
<< 97 100 @character codes for the letters: a b c d
FOR N a R→B @convert to binary
→STR @convert to string
‘a’ STO @store the string in a
SIZE 1 – ‘s’ STO @store the size of the string minus 1 in s
a 3 s SUB @truncate the extraneous characters
‘a ‘ STO @store this new string in ‘a’
a SIZE 8 @get the size of ‘a’ and put 8 on the stack
FOR I “0” a + ‘a’ STO @FOR loop will add the number of missing zeros to make
@this an octet
a TAIL @truncate the remaining extraneous characters
1 N + CHR OBJ→ @add 1 to the counter value and convert to the next variable (b ,c or d)
‘a’ STO NEXT @store this in ‘a’ and repeat
>> >>
Reference URL's