본문 바로가기
coding/tcl

tcl/tk 사용하여 encoding 개발하기 ( url-encode )

by 김학준0724 2022. 9. 6.

tcl로 특수문자를 ascii코드로 변환(encoding) 방법을 정리하려고합니다.

코드는 아래와 같습니다.

 

1) url-encode 소스

proc url-encode {string} {
    variable map
    variable alphanumeric a-zA-Z0-9 #알파벳 및 숫자 필터 : 필터 외에 문자는 특수문자로 변환 실시
    for {set i 0} {$i <= 256} {incr i} { 
        set c [format %c $i]
        if {![string match \[$alphanumeric\] $c]} { #필터링된 특수문자는 if 문 진행
            set map($c) %[format %.2X $i] #특수문자는 format 처리됨
        }
    }
    # These are handled specially
    array set map { " " + \n %0d%0a }

    # The spec says: "non-alphanumeric characters are replaced by '%HH'"
    # 1 leave alphanumerics characters alone
    # 2 Convert every other character to an array lookup
    # 3 Escape constructs that are "special" to the tcl parser
    # 4 "subst" the result, doing all the array substitutions

    regsub -all \[^$alphanumeric\] $string {$map(&)} string
    # This quotes cases like $map([) or $map($) => $map(\[) ...
    regsub -all {[][{})\\]\)} $string {\\&} string
    return [subst -nocommand $string]
}

 

2) 함수 호출 부분 소스

set url "$url?c=pgmCompare&custCode=[url-encode $custCode]&device=[url-encode $device]"

 

3) 결과

input : custcode = south korean
          device=TP90-
output : http://webserver.com?c=pgmCompare&device=TP90%2D&custcode=south+korean

 

 

4) 참고 - ascii 코드표

'coding > tcl' 카테고리의 다른 글

tcl/tk 사용하여 decoding 개발하기 ( url-decode )  (1) 2022.09.15