URL encode for Unicode mIRC v7.x

By Spoofing on Jul 13, 2010

I think all understand :)

Example, type: //echo $url_encode(тест)
in mIRC >= v7.0

Returns: %D1%82 %D0%B5 %D1%81 %D1%82 (without spaces, of course)
(two bytes for one unicode russian symbol)

alias url_encode {
  var %i = 1, %len = $len($1), %str
  while (%i <= %len) {
    var %char = $asc($mid($1,%i,1)), %i = %i + 1
    if (%char isnum 33-47) || (%char isnum 48-57) || (%char isnum 58-64) || (%char isnum 65-90) || (%char isnum 97-122) var %str = $+(%str,$chr(%char))
    elseif (%char < 128) var %str = $+(%str,%,$base(%char,10,16,2))
    elseif (%char < 2048) var %str = $+(%str,%,$base($calc(192 + $int($calc(%char / 64))),10,16,2),%,$base($calc(128 + (%char % 64)),10,16,2))
    elseif (%char < 65536) var %str = $+(%str,%,$base($calc(224 + $int($calc(%char / 4096))),10,16,2),%,$base($calc(128 + ($int($calc(%char / 64)) % 64)),10,16,2),%,$base($calc(128 + (%char % 64)),10,16,2))
    elseif (%char < 2097152) var %str = $+(%str,%,$base($calc(240 + $int($calc(%char / 262144))),10,16,2),%,$base($calc(128 + ($int($calc(%char / 4096)) % 64)),10,16,2),%,$base($calc(128 + ($int($calc(%char / 64)) % 64)),10,16,2),%,$base($calc(128 + (%char % 64)),10,16,2))
  }
  return %str
}

Comments

Sign in to comment.
FordLawnmower   -  Jul 31, 2010

Just messing with the new mIRC and it looks like this works fine for Unicode:

alias UrlEncode return $regsubex($1-,/([^\d\w])/g,$+(%,$base($asc(\t),10,16)))

The regsubex engine seems to be splitting the unicode back into pairs before processing.

 Respond  
FordLawnmower   -  Jul 13, 2010

@Spoofing How did you figure out the math for this?
It's the same as my old one here -->> http://www.hawkee.com/snippet/6515/
I only did it like that because I was having trouble figuring out how to use bit shifting in mSL. I finally sorted it out with bit shifting here -->> http://www.mircscripts.org/showdoc.php?type=code&id=4403

 Respond  
WorldDMT   -  Jul 13, 2010

the first condition can be like this i think!!

if (%char isnum 33-122) var %str $+(%str,$chr($v1))

why u set var %str and u reset it down i think the first one is useless

 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.