Subscribed unsubscribe Subscribe Subscribe

k-yamadaのブログ

プログラミングのメモ

UTF-8からShift_JISに変換する時の注意

(http://qiita.com/akkun_choi/items/a1fbde5d6d065df3fc97)」を参考にやってみたのですが、うまくencode出来なかったので、一部修正しました。

以下はMac OS XUbuntuで動作確認しています。

ソースコード

# app/models/encode_utils.rb: 

class EncodeUtils
  class << self
    def to_sjis(str)
      sjis_safe(str).encode("Shift_JIS",
        :invalid => :replace, :undef => :replace)
    end

    def sjis_safe(str)
      [
        ["FF5E", "301C"], # wave-dash
        ["FF0D", "2212"], # full-width minus
        ["FFE0", "00A2"], # cent as currency
        ["FFE1", "00A3"], # lb(pound) as currency
        ["FFE2", "00AC"], # not in boolean algebra
        ["2015", "2014"], # hyphen
        ["2225", "2016"], # double vertical lines
      ].inject(str) do |s, (before, after)|
        before_str = before.to_i(16).chr('UTF-8')
        after_str  = after.to_i(16).chr('UTF-8')
        s.gsub(before_str, after_str)
      end
    end
  end
end

rspec

# spec/models/encode_utils_spec.rb:

# -*- coding: utf-8 -*-
require 'spec_helper'

describe EncodeUtils do
  def hex_to_sjis(hex)
    hex.to_i(16).chr('UTF-8').encode("Shift_JIS")
  end

  describe '.to_sjis' do
    it 'hiragana should change to sjis' do
      expect(EncodeUtils.to_sjis("あいうえお")).to eq("あいうえお".encode("Shift_JIS"))
    end

    it 'harf-width special char should change to sjis' do
      expect(EncodeUtils.to_sjis("〜−¢£¬−‖")).to eq("〜−¢£¬−‖".encode("Shift_JIS"))
    end

    it 'wave-dash should change to sjis' do
      expect(EncodeUtils.to_sjis("")).to eq(hex_to_sjis("301C"))
    end

    it 'full-width minus should change to sjis' do
      expect(EncodeUtils.to_sjis("")).to eq(hex_to_sjis("2212"))
    end

    it 'cent as currency should change to sjis' do
      expect(EncodeUtils.to_sjis("")).to eq(hex_to_sjis("00A2"))
    end

    it 'lb(pound) as currency should change to sjis' do
      expect(EncodeUtils.to_sjis("")).to eq(hex_to_sjis("00A3"))
    end

    it 'not in boolean algebra should change to sjis' do
      expect(EncodeUtils.to_sjis("")).to eq(hex_to_sjis("00AC"))
    end

    it 'hyphen should change to sjis' do
      expect(EncodeUtils.to_sjis("")).to eq(hex_to_sjis("2014"))
    end

    it 'double vertical lines should change to sjis' do
      expect(EncodeUtils.to_sjis("")).to eq(hex_to_sjis("2016"))
    end
  end
end