Differences between revisions 9 and 10
Revision 9 as of 2005-09-02 09:55:35
Size: 2782
Editor: 211
Comment:
Revision 10 as of 2005-11-07 14:49:54
Size: 3268
Editor: 211
Comment:
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
}}}

[PHP]에서 문자열자르기함수 from [http://x.debugs.co.kr/source/testcode/utf8_cutstr.php 거친마루님 페이지]
{{{
<?
function utf8_cutstr($str,$len,$tail='') {
    $c = substr(str_pad(decbin(ord($str{$len})),8,'0',STR_PAD_LEFT),0,2);
    if ($c == '10')
        for (;$c != '11' && $c{0} == 1;$c = substr(str_pad(decbin(ord($str{--$len})),8,'0',STR_PAD_LEFT),0,2));
    return substr($str,0,$len) . (strlen($str)-strlen($tail) >= $len ? $tail : '');
}
?>

UTF-8 UniversalTransformationFormat8Bit, [RFC] 2279

UniCode는 상대적으로 비효율적인 CharacterSet이다. 따라서, 적절한 인코딩 방법을 사용하여 영미권에서 자주 사용하는 문자셋에 대해 적은 바이트를 사용하도록 하는 좀더 효율적인 방식의 인코딩이 필요하게 되었는데, 이것이 UTF-8

[ASCII] 문자셋은 한 byte로 인코딩하고, 나머지에 대해서는 2,3byte로 인코딩한다.

관련정보

[Python]에서의 변환방법 (euc-kr -> utf-8) SeeAlso PythonAndHangul

   1 euckr2utf8 = lambda s: unicode(s, 'euc-kr').encode('utf-8')

[PHP]에서 문자열자르기함수 from [http://x.debugs.co.kr/source/testcode/utf8_cutstr.php 거친마루님 페이지]

<?
function utf8_cutstr($str,$len,$tail='') {
    $c = substr(str_pad(decbin(ord($str{$len})),8,'0',STR_PAD_LEFT),0,2);
    if ($c == '10')
        for (;$c != '11' && $c{0} == 1;$c = substr(str_pad(decbin(ord($str{--$len})),8,'0',STR_PAD_LEFT),0,2));
    return substr($str,0,$len) . (strlen($str)-strlen($tail) >= $len ? $tail : '');
}
?>

특정디렉토리의 밑의 특정확장자 파일변환

[yong27@est test]$ cat cvtEncoding
#!/bin/sh
for cfile in `find . -name "*.${1}"`
do
echo Processing ${cfile}
iconv -f "$2" -t "$3" "$cfile" > "${cfile}_"
mv "${cfile}_" "$cfile"
done

[yong27@est test]$ ./cvtEncoding py euc-kr utf-8
Processing ./enc.py

특정디렉토리 밑의 모든 파일 및 파일,경로네임 변환 --> [EncodingConverter.py]


BioinfoWiki도 그렇고, TatterTools도 UTF-8이 아닌 EUC-KR을 쓰고 있다. 점차적으로 변경요망. --[yong27], 2004-10-06

UTF-8로 전체 변환 --[yong27], 2005-06-26

특정문자열에 대해, 인코딩을 자동으로 인식하는 프로그램을 만들어야겠다. [Google]서 대충봤는데 없는듯하고... 모든 인코딩에 대해 다 할 수 는 없겠고, 한국어사용자를 위한 utf-8 or euc-kr만 인식하는것은 가능하지 않을까? 자음+모음의 형식이 대강 맞으면 utf-8 아니면 euc-kr.

일단은 가장 간단한 버젼.

   1 #encoding:utf-8
   2 import unittest
   3 
   4 class HangulEncodingRecognitor:
   5     def getEncoding(self, code):
   6         encoding = None
   7         try:
   8             u = unicode(code,'euc-kr')
   9             encoding = 'euc-kr'
  10         except UnicodeDecodeError:
  11             pass
  12         try:
  13             u = unicode(code,'utf-8')
  14             encoding = 'utf-8'
  15         except UnicodeDecodeError:
  16             pass
  17         assert encoding, "Unknown encoding"
  18         return encoding
  19 
  20 class Test(unittest.TestCase):
  21     def setUp(self):
  22         h='테스트 한글'
  23         u=unicode(h, 'utf-8')
  24         self.utf8 = u.encode('utf-8')
  25         self.euckr = u.encode('euc-kr')
  26     def testGetEncoding(self):
  27         her = HangulEncodingRecognitor()
  28         self.assertEquals('utf-8',her.getEncoding(self.utf8))
  29         self.assertEquals('euc-kr',her.getEncoding(self.euckr))
  30 
  31 if __name__=='__main__':
  32     unittest.main()

--[yong27], 2005-06-27

UTF8 (last edited 2013-07-23 10:30:29 by 61)

web biohackers.net