Different IP address representation variants
Posted: Fri Jun 10, 2016 11:44 pm
The following shows variations of how IP address can be represented. They working in browsers and not only, many utils and other stuff recognizes such forms correctly (for example, windows ping utility)
Might be useful to pass primitive filters, if you going to post link to something which is blocked by ISP or some site^^
PS. As side-effect this example shows that PB lacks unsigned variables and Octal() function
Might be useful to pass primitive filters, if you going to post link to something which is blocked by ISP or some site^^
PS. As side-effect this example shows that PB lacks unsigned variables and Octal() function

Code: Select all
EnableExplicit
Structure IP4Adress
StructureUnion
IP.q ; integer representation of IP. quad because of lack of unsigned long :/
IP_byte.a [4] ; separated bytes of IP4 adress
EndStructureUnion
EndStructure
; converting decimal to octal. source: https://rosettacode.org/wiki/Count_in_octal#PureBasic
; RETURN: string representing number in octal system
Procedure.s octal(n.q)
Static Dim digits(20)
Protected i, j, result.s
For i = 0 To 20
digits(i) = n % 8
n / 8
If n < 1
For j = i To 0 Step -1
result + Str(digits(j))
Next
Break
EndIf
Next
ProcedureReturn result
EndProcedure
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Define A.IP4Adress
Define S$
; some google IP - 216.58.214.206
; in memory it is stored in reversed mode
a\IP_byte[3] = 216
a\IP_byte[2] = 58
a\IP_byte[1] = 214
a\IP_byte[0] = 206
; dump variations to _test.html file
CreateFile(0, "_test.html", #PB_UTF8)
WriteStringN(0, "<html>")
WriteStringN(0, "<strong>#IP address variants:</strong><br><br>")
WriteStringN(0, ~"<a href=\"http://0x" + Hex(a\IP) + ~"\">HEX form</a><br>")
WriteStringN(0, ~"<a href=\"http://" + "0x" + Hex(a\IP_byte[3]) +
".0x" + Hex(a\IP_byte[2]) +
".0x" + Hex(a\IP_byte[1]) +
".0x" + Hex(a\IP_byte[0]) + ~"\">HEX form #2</a><br>")
WriteStringN(0, ~"<a href=\"http://0" + octal(a\IP) + ~"\">Octal form</a><br>")
WriteStringN(0, ~"<a href=\"http://" + "0" + octal(a\IP_byte[3]) +
".0" + octal(a\IP_byte[2]) +
".0" + octal(a\IP_byte[1]) +
".0" + octal(a\IP_byte[0]) + ~"\">Octal form #2</a><br>")
; also they can be mixed, like dec.dec.hex.oct
WriteStringN(0, ~"<a href=\"http://" + a\IP + ~"\">DEC form</a><br>")
WriteStringN(0, ~"<br><a href=\"http://" + a\IP_byte[3] + "." + a\IP_byte[2] + "." + a\IP_byte[1] + "." + a\IP_byte[0] + ~"\">[regular IP string]</a>")
WriteStringN(0, "<br><br><br>*see page source for details")
WriteStringN(0, "</html>")
CloseFile(0)
; shellexecute
RunProgram("_test.html")