全角转半角与半角转全角(转)

 

1.全角:指一个字符占用两个标准字符位置。汉字字符和规定了全角的英文字符及国标GB2312-80中的图形符号和特殊字符都是全角字符。一般的系统命令是不用全角字符的,只是在作文字处理时才会使用全角字符。
2.半角:指一字符占用一个标准的字符位置。通常的英文字母、数字键、符号键都是半角的,半角的显示内码都是一个字节。在系统内部,以上三种字符是作为基本代码处理的,所以用户输入命令和参数时一般都使用半角。
3.全角与半角在计算机中的表示:据我所知,全角的第一个字节是163(我用-93),然后第二个字节与半角相差128。全角空格和半角空格也要考虑进去。
4.C/C++版本:
view plaincopy to clipboardprint?
string ToDBS(string str) {   
    string result = "";   
    unsigned char tmp; unsigned char tmp1;   
    for (unsigned int i = 0; i < str.length(); i++) {   
        tmp = str[i];   
        tmp1 = str[i + 1];   
        cout << "uchar:" << (int) tmp << endl;   
        if (tmp == 163) {///第一个字节是163,标志着是全角字符   
            result += (unsigned char) str[i + 1] - 128;   
            i++;   
            continue;   
        } else if (tmp > 163) {//汉字   
            result += str.substr(i, 2);   
            i++;   
            continue;   
        } else if (tmp == 161 && tmp1 == 161) {///处理全角空格   
            result += "";   
            i++;   
        } else {   
            result += str.substr(i, 1); } } return result;   
 
string ToDBS(string str) {
    string result = "";
    unsigned char tmp; unsigned char tmp1;
    for (unsigned int i = 0; i < str.length(); i++) {
        tmp = str[i];
        tmp1 = str[i + 1];
        cout << "uchar:" << (int) tmp << endl;
        if (tmp == 163) {///第一个字节是163,标志着是全角字符
            result += (unsigned char) str[i + 1] - 128;
            i++;
            continue;
        } else if (tmp > 163) {//汉字
            result += str.substr(i, 2);
            i++;
            continue;
        } else if (tmp == 161 && tmp1 == 161) {///处理全角空格
            result += "";
            i++;
        } else {
            result += str.substr(i, 1); } } return result;
}

 
5.C#版本:
view plaincopy to clipboardprint?
/// <summary>   
       /// 转全角的函数(SBC case)   
       /// </summary>   
       /// <param name="input">任意字符串</param>   
       /// <returns>全角字符串</returns>   
       ///<remarks>   
       ///全角空格为12288,半角空格为32   
       ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248   
       ///</remarks>           
       public static string ToSBC(string input)   
       {   
           //半角转全角:   
           char[] c = input.ToCharArray();   
           for (int i = 0; i < c.Length; i++)   
           {   
               if (c[i] == 32)   
               {   
                   c[i] = (char)12288;   
                   continue;   
               }   
               if (c[i] < 127)       
  
                   c[i] = (char)(c[i] + 65248);   
           }   
           return new string(c);   
       }   
  
  
          
       /// <summary>   
       /// 转半角的函数(DBC case)   
       /// </summary>   
       /// <param name="input">任意字符串</param>   
       /// <returns>半角字符串</returns>   
       ///<remarks>   
       ///全角空格为12288,半角空格为32   
       ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248   
       ///</remarks>   
       public static string ToDBC(string input)   
       {   
           char[] c = input.ToCharArray();   
           for (int i = 0; i < c.Length; i++)   
           {   
               if (c[i] == 12288) {   
                   c[i] = (char)32;                        
  
                     continue;   
               }   
               if (c[i] > 65280 && c[i] < 65375)     
  
                   c[i] = (char)(c[i] - 65248);   
           }   
           return new string(c);   
       }   
 /// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        ///<remarks>
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        ///</remarks>        
        public static string ToSBC(string input)
        {
            //半角转全角:
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)   

                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }


        
        /// <summary>
        /// 转半角的函数(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        ///<remarks>
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        ///</remarks>
        public static string ToDBC(string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288) {
                    c[i] = (char)32;                    

                      continue;
                }
                if (c[i] > 65280 && c[i] < 65375) 

                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }

 

 
 6.java版本
view plaincopy to clipboardprint?
**     
 * 半角转全角     
 * @param input String.     
 * @return 全角字符串.     
 */     
public static String ToSBC(String input) {      
         char c[] = input.toCharArray();      
         for (int i = 0; i < c.length; i++) {      
           if (c[i] == ' ') {      
             c[i] = '/u3000';                 //采用十六进制,相当于十进制的12288      
     
     
           } else if (c[i] < '/177') {    //采用八进制,相当于十进制的127      
             c[i] = (char) (c[i] + 65248);      
     
           }      
         }      
         return new String(c);      
}      
     
     
/**    
 * 全角转半角    
 * @param input String.    
 * @return 半角字符串    
 */     
public static String ToDBC(String input) {      
          
     
         char c[] = input.toCharArray();      
         for (int i = 0; i < c.length; i++) {      
           if (c[i] == '/u3000') {      
             c[i] = ' ';      
           } else if (c[i] > '/uFF00' && c[i] < '/uFF5F') {      
             c[i] = (char) (c[i] - 65248);      
     
           }      
         }      
    String returnString = new String(c);      
          
         return returnString;      
}   


你可能感兴趣的:(c,String,input,图形)