<commit_before># encoding: utf-8
require 'rubygems'
require 'sinatra'
require 'haml'

get "/" do
  haml :index
end

post "/convert" do
  begin
    @after = convert(params)
    haml :index
  rescue
    ["だめです？",
     "おちるです？",
     "しんじゃうです？",
     "ふせいです？",
     "へんです？",
     "おかしいです？",
     "さよならです？"].sample
  end
end

get "/convert" do
  convert(params)
end

private
def convert(params)
  if params[:way] == "0"
    str2hex(params[:before], params[:charset])
  else
    hex2str(params[:before], params[:charset])
  end
end

def str2hex(str, charset="UTF-8")
  str.split(//).map {|char|
    char.encode(charset).bytes.map {|byte|
      byte.to_s(16)
    }.join
  }.join(' ')
end

def hex2str(hex, charset="UTF-8")
  if charset == "UTF-8"
    [hex.gsub(/\s+/, '')].pack("H*").force_encoding(charset)<commit_msg>Split method to char2hex<commit_after># encoding: utf-8
require 'rubygems'
require 'sinatra'
require 'haml'

get "/" do
  haml :index
end

post "/convert" do
  begin
    @after = convert(params)
    haml :index
  rescue
    ["だめです？",
     "おちるです？",
     "しんじゃうです？",
     "ふせいです？",
     "へんです？",
     "おかしいです？",
     "さよならです？"].sample
  end
end

get "/convert" do
  convert(params)
end

private
def convert(params)
  if params[:way] == "0"
    str2hex(params[:before], params[:charset])
  else
    hex2str(params[:before], params[:charset])
  end
end

def str2hex(str, charset="UTF-8")
  str.split(//).map {|char|
    char2hex(char, charset)
  }.join(' ')
end

def char2hex(char, charset)
  char.encode(charset).bytes.map {|byte|
    byte.to_s(16)
  }.join
end

def hex2str(hex, charset="UTF-8")
  if charset == "UTF-8"
    [hex.gsub(/\s+/, '')].pack("H*").force_encoding(charset)
{'fraction_non_alphanumeric': 0.18557919621749408, 'fraction_numerical': 0.014775413711583925, 'mean_word_length': 3.286075949367089, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5641951', 'n_tokens_mistral': 729, 'n_tokens_neox': 695, 'n_words': 146}