
UConverter::transcode 메서드는 두 개의 인자를 받습니다. 첫 번째 인자는 변환할 문자열, 두 번째 인자는 변환할 인코딩입니다.
예를 들어, "hello" 문자열을 UTF-8에서 EUC-KR로 변환하려면 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
ruby
require 'unicode-normalizer'
uconv = UnicodeNormalizer.new
result = uconv.transcode("hello", "UTF-8", "EUC-KR")
puts result
이 코드는 "hello" 문자열을 UTF-8에서 EUC-KR로 변환한 후 결과를 출력합니다.
만약, 변환할 인코딩이 없다면 UConverter::transcode 메서드는 기본 인코딩인 UTF-8에서 변환할 인코딩으로 변환합니다.
#hostingforum.kr
ruby
require 'unicode-normalizer'
uconv = UnicodeNormalizer.new
result = uconv.transcode("hello", "EUC-KR")
puts result
이 코드는 "hello" 문자열을 UTF-8에서 EUC-KR로 변환한 후 결과를 출력합니다.
UConverter::transcode 메서드는 변환할 문자열이 변환할 인코딩에 맞지 않는 경우에는 nil을 반환합니다.
#hostingforum.kr
ruby
require 'unicode-normalizer'
uconv = UnicodeNormalizer.new
result = uconv.transcode("hello", "Shift_JIS")
puts result
이 코드는 "hello" 문자열을 UTF-8에서 Shift_JIS로 변환하려고 합니다. 하지만, "hello" 문자열은 Shift_JIS에 맞지 않기 때문에 nil을 반환합니다.
2025-05-02 11:09