&# 'x' ; replacer and Hex->Dec converter

By Blitzjager on Apr 06, 2009

Haven't posted too recently and I just made this so I figured I'd post it.
Probably some things that could've been done easier but oh wells.

The hexdec will work on its own but the char might not always work.
$char()
//echo -a $char(Rawr &# 124 ; asdf) - returns Rawr | asdf
//echo -a $char(Rawr &# x9F ; asdf) - returns Rawr Ÿ asdf
Note: No spaces between the # and ;
$hexdec()
//echo -a $hexdec(1F) - returns 31
//echo -a $hexdec(1FAE9CB) - returns 33221067

I made this to be used with sockets (used in an $htmlfree or something).
Eh, guess that's it.

alias char {
  var %x $regex(char,$1,/&#(x?[a-f\d]*);/gi), %y $1
  while (%x) {
    var %z $remove($regml(char,%x),x), %z $iif($regex(%z,/[a-f]/i),$hexdec(%z),$regml(char,%x))
    var %y $replace(%y,$+(&#,$regml(char,%x),;),$chr(%z))
    dec %x
  }
  return %y
}
alias hexdec {
  var %x $len($1)
  var %y 1
  var %z 0
  while (%y <= %x) {
    var %b $iif($mid($1,$+(-,%y),1) isin ABCDEF,$gettok(10 11 12 13 14 15,$findtok(A B C D E F,$v1,1,32),32),$v1)
    var %a $instok(%a,$calc(%b * (16 ^ %z)),$calc($numtok(%a,43) + 1),43)
    inc %y
    inc %z
  }
  return $calc(%a)
}

Comments

Sign in to comment.
Blitzjager   -  Apr 18, 2009
alias char { return $regsubex($1,/&#(x?([a-f\d]*));/gi,$chr($iif($left(\1,1) == x,$base(\2,16,10),\2))) }

Not sure how much time I spent on the original code but I hope it wasn't much lol.

 Respond  
Blitzjager   -  Apr 06, 2009

Awesome. Tyvm. :)
Atleast I learned how to convert it. :S

Don't think there's a way to do é stuff without having a list of some sort. :(

 Respond  
BlueThen   -  Apr 06, 2009

Base is pretty simple, the mirc help file just makes it seem complicated.

The $base identifier is basically just
$base(number, what base your number is, and what base you want to convert it to, how many digits you want it, and how precise)

10 is the decimal base, 2 is binary, and 16 is hexadecimal. Meaning
$base(45,10,2) would return 101101, $base(45,10,16) returns 2D. You can convert numbers back by simply swapping 10 and 2 or 16. So
$base(101101,2,10) would return 45... etc

The 4th and 5th parameters are option, but zerobase (the 4th parameter) is pretty much what the least number of digits you want your number to be. So 5 with zerobase of 6 would be 000005. Precision is how many digits you want to limit it after the decimal point.

 Respond  
Blitzjager   -  Apr 06, 2009

Just screwed around with $base. I'm not sure if it would've helped or how to use it but from what I screwed around with an anti-$base would've helped. :F
Anyone know if it would help here?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.