ID:1699552
 
mob/verb
Example()
world<<dec2hex(89)
world<<hex2dec("59")
world<<dec2bin(89)//Edit
world<<bin2dec("1011001")//Edit
proc
dec2hex(n)
return "[round(n/16)%16>9?ascii2text(round(n/16)%16+55):round(n/16)%16][n%16>9?ascii2text(n%16+55):n%16]"
hex2dec(n)
var/list/hex = list("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F")
return (hex.Find(copytext(n,1,2))-1)*16+(hex.Find(copytext(n,2,3))-1)
//Edited @ 7:35 AM 10/15/2014
//The following two I messed around with quickly, I'll dedicate time strictly to improving their efficiency later.

dec2bin(n)
var
list/out=list(0,0,0,0,0,0,0,0)
run=0
for(var/a = 1,a<=128,a+=a)
run++
if(n!=(n%(128/a)))
n%=(128/a)
out[run] = 1
for(var/a in out)
.="[.][a]"//Urgh this pains me to do. >:

bin2dec(n)
if(length(n)>8) return//Too large to work with. 255 max (1 byte)
.=0
if(length(n) && copytext(n,-1)=="1").+=1
if(length(n)>1 && copytext(n,-2,-1)=="1").+=2
if(length(n)>2 && copytext(n,-3,-2)=="1").+=4
if(length(n)>3 && copytext(n,-4,-3)=="1").+=8
if(length(n)>4 && copytext(n,-5,-4)=="1").+=16
if(length(n)>5 && copytext(n,-6,-5)=="1").+=32
if(length(n)>6 && copytext(n,-7,-6)=="1").+=64
if(length(n)>7 && copytext(n,-8,-7)=="1").+=128
Here's a bin2dec() I wrote to show how you can convert any number of bits.

It must be noted that BYOND has limited accuracy past the decimal value 16,777,215. This is not an error with the code, it is the nature of dealing with very large numbers on BYOND natively.

proc
bin2dec(binary as text)

var/factor = 1
var/bit

for(var/bitID = length(binary); bitID > 0; bitID --)
bit = copytext(binary, bitID, bitID + 1)
. += text2num(bit) * factor
factor *= 2
Thank you for contributing~ I do like your factor method.
@Alex: As a side note, you don't need to specify the argument type in procs. not sure if you knew that or not. It's only mandatory in verbs.

proc/SomeProc(A,B,C)
verb/SomeVerb(A as text, B as num, C as anything)


Also for loops allow the use of BASIC-style iterations:
for (var/i = 1 to 10 step 1)
// do stuff
In response to Mr_Goober
I like the added security. Just something I carry over from other languages.
Yes of course. Most type-strict languages prefer you define types as well.

If for whatever reason you need to check if an argument is a specific type, several functions exist:
istext(N) // checking for strings
isnum(T) // checking for reals
isloc(T) // checking for area/turfs
istype(D, /type) // checking for anything derived from /datum (not including primitives like text and numbers)
// and more.


That being said, one can create a small subset of functions to force a data type:

proc/Number(N)
if(isnum(N))
return N
else if (istext(N))
return text2num(N)
else
return 0
In response to Mr_Goober
Will specifying the argument type as text not already act like?
if (!istext(N)) return
I did a small test that looked something like this:
proc/Something(A as num)
world << A

client/New()
..()
Something("This is a string")


The compiler didn't object, and the output was indeed a string. So as it turns out, setting the argument type doesn't really do anything besides looks in the code. But you're still encouraged to do it that way if it helps you understand what your own parameters do when you check back on the code.
In response to Mr_Goober
Ah very cool, good to know thanks.