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 人气:711
分享到:
blog comments powered by Disqus

相关文章

  • php utf-8转unicode的函数第1/2页 2014-10-06

    php下我们想把uft-8,转成unicode可以用下面的函数来实现 UTF编码 UTF-8就是以8位为单元对UCS进行编码.从UCS-2到UTF-8的编码方式如下: UCS-2编码(16进制) UTF-8 字节流(二进制) 0000 - 007F 0xxxxxxx 0080 - 07FF 110xxxxx 10xxxxxx 0800 - FFFF 1110xxxx 10xxxxxx 10xxxxxx 例如"汉"字的Unicode编码是6C49.6C49在0800-FFFF之间,所以肯

  • php实现utf-8转unicode函数分享 2013-10-17

    这篇文章主要介绍了php实现utf-8转unicode函数分享,需要的朋友可以参考下 代码很简单,功能却很实用,推荐给大家. 奉上代码先: public function utf8_unicode($str) { $unicode = array(); $values = array(); $lookingFor = 1; for ($i = 0; $i < strlen( $str ); $i++ ) { $thisValue = ord( $str[ $i ] ); if ( $thisVa

  • 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

  • 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

  • java encoding utf-8 \ gbk \ unicode encoding conversion between 2010-11-05

    See the question was raised: String heh=" I love I love home "; byte[] bytes=heh.getBytes(); System.out.println(bytes.length); The result is 15 String heh=" I love I love home "; byte[] bytes=heh.getBytes("unicode"); System.o

  • UTF8\ASCII\UNICODE转换 2013-08-20

    wchar_t * ANSIToUnicode( const char* str ) { int textlen ; wchar_t * result; textlen = MultiByteToWideChar( CP_ACP, 0, str,-1, NULL,0 ); result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t)); memset(result,0,(textlen+1)*sizeof(wchar_t)); MultiByteT

  • 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

  • php will be utf-8 (3 bytes) string to byte 2010-08-20

    // The UTF-8 string converted to bytes function strToByteArr($str){ //$str = iconv('gbk', 'utf-8', $str); $arr=array(); $len=strlen($str); for($i=0;$i<$len;$i++){ if(ord(substr($str,$i,1))>0xa0){ //$arr[$i]= substr($str,$i,3);// Double-byte characte

  • unicode string space 2010-09-21

    function filterSpace(str){ str = escape(str); var txt = str.replace(/%A0/g, "%20");// A Unicode encoded as A0 White space is converted to 20 space return unescape(txt); }

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

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