Using Ruby to call mssql (sqlserver) of ntext type unicode, etc.

When the number of library visits

DBI.connect(ODBC_NAME,ODBC_USER, ODBC_PWD) do |db|
  db.do('set textsize 9999')  # If you do not have this causes error
  db.select_all("select * from
end

unicode -> utf8 or gb2312


require 'iconv'
require 'cgi'
class Iconv

  def Iconv.unicode2utf8(unicode_string)
    unicode_string.gsub(/\\u\w{4}/) do |s|
      str = s.sub(/\\u/, "").hex.to_s(2)
      if str.length < 8
        CGI.unescape(str.to_i(2).to_s(16).insert(0, "%"))
      else
        arr = str.reverse.scan(/\w{0,6}/).reverse.select{|a| a != ""}.map{|b| b.reverse}
        # ["100", "111000", "000000"]
        hex = lambda do |s|
          (arr.first == s ? "1" * arr.length + "0" * (8 - arr.length - s.length) + s : "10" + s).to_i(2).to_s(16).insert(0, "%")
        end
        CGI.unescape(arr.map(&hex).join)
      end
    end
  end

  def Iconv.unicode2gb2312(unicode_string)
    u8_text = Iconv.unicode2utf8(unicode_string)
    Iconv.iconv("UTF-8//IGNORE","GB2312//IGNORE",u8_text)
  end
end

unicode2utf8 from http://yuweijun.blogspot.com/2008/12/rubyunicodeutf8.html be

分类:Ruby 时间:2009-03-03 人气:710
分享到:
blog comments powered by Disqus

相关文章

  • utf8和unicode编码究竟是什么关系?有何区别? 2014-08-06

    UTF8 == Unicode Transformation Format -- 8 bit 是Unicode传送格式。即把Unicode文件转换成BYTE的传送流。 UTF8流的转换程序: Input: unsigned integer c - the code point of the character to be encoded (输入一个unicode值) Output: byte b1, b2,b3, b4 - the encoded sequence of bytes (输出四个B

  • weblogic reported UTF8 representation for string and is too long for constant pool error 2010-08-17

    weblogic reported UTF8 representation for string is too long for constant pool error, tomacat under no any problems. Why? Check the Internet and found weblogic sp4 patch without a fight, they will report this error, Found online solutions and reasons: Thi

  • UTF8 and Unicode conversion 2010-08-12

    If the two byte UNICODE characters that are encoded into UTF-8 is likely to require three bytes, and if by a four-byte UNICODE characters that are encoded into UTF-8 may require six bytes. With 4 or 6 bytes to encode a UNICODE character may too, but

  • 采用UTF-8解决Ruby on Rails程序的中问题 2014-04-13

    将.rb文件和.rhtml文件都保存为utf-8格式 1.将.rb文件和.rhtml文件都保存为utf-8格式; 2.在/app/Controller/application.rb中增加如下代码: ruby 代码 before_filter :set_charset def set_charset headers["Content-Type"] = "text/html; charset=UTF-8" end

  • Step-by-step learning Ruby (8): string expression 2009-09-23

    Ruby, the built-in many ways the operation of the string the following, we take a look at some of the most important methods Connection string Multiplication can be done on the string String Comparison In fact, we know that the string is stored in th ...

  • unicode string space 2010-09-21

    function filterSpace(str){ str = escape(str); var txt = str.replace(/%A0/g, "%20");//将Unicode编码为A0的空格转换为20的space return unescape(txt); }

  • tcl [string map] is to facilitate the replacement string is 2011-04-12

    【Keywords】: Tcl string map replacement string Wrote string map?-nocase? mapping string Replaces substrings in string based on the key-value pairs in mapping. Mapping is a list of key value key value ... as in the form returned by array get. Each inst

  • Massive Data Analysis: Sawzall parallel processing (a Chinese version of paper) 2010-12-18

    from: http://peopleyun.com/?p=896 Google's engineers to use internal staff for the convenience of MapReduce, Sawzall developed called the DSL, while also launched a similar Hadoop's Pig Sawzall language, but in the syntax above is quite different.

  • Character encoding and decoding of the story (ASCII, ANSI, Unicode, Utf-8) 2010-03-25

    2009-01-31 15:10 Kevin Yang Long long time ago, a group of people, they decided to open and close with 8 transistors to be combined into different states to show the world everything. They think that eight switch states as the atomic unit of good, so they

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.184 (s). 12 q(s)