)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Utf8 (Ia.Cl.Model)

Public general use code classes and xml files that we've compiled and used over the years:

Handle UTF8 issues.

    1: using System;
    2: using System.Data;
    3: using System.Data.OleDb;
    4: using System.Net;
    5: using System.Text;
    6:  
    7: namespace Ia.Cl.Model
    8: {
    9:     ////////////////////////////////////////////////////////////////////////////
   10:  
   11:     /// <summary publish="true">
   12:     /// Handle UTF8 issues.
   13:     /// </summary>
   14:     /// <remarks> 
   15:     /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   16:     ///
   17:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   18:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   19:     ///
   20:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   21:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   22:     /// 
   23:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   24:     /// 
   25:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   26:     /// </remarks> 
   27:     public class Utf8
   28:     {
   29:         /// <summary/>
   30:         protected DataSet ds;
   31:  
   32:         /// <summary/>
   33:         protected OleDbDataAdapter da;
   34:  
   35:         /// <summary/>
   36:         protected DataTable dt;
   37:  
   38:         /// <summary/>
   39:         public string connection_string = "Provider=MySQLProv;Location=localhost;Data Source=*;UID=;PWD=";
   40:  
   41:         ////////////////////////////////////////////////////////////////////////////
   42:  
   43:         /// <summary>
   44:         ///
   45:         /// </summary>
   46:         protected void Page_Load(object sender, EventArgs e)
   47:         {
   48:         }
   49:  
   50:         ////////////////////////////////////////////////////////////////////////////
   51:  
   52:         /// <summary>
   53:         ///
   54:         /// </summary>
   55:         protected string Utf8ToHex(string utf8_str)
   56:         {
   57:             byte[] bytes;
   58:             string s = "";
   59:             UTF8Encoding utf8 = new UTF8Encoding();
   60:  
   61:             bytes = utf8.GetBytes(utf8_str);
   62:  
   63:             foreach (byte b in bytes) s += b.ToString("x");
   64:  
   65:             return s;
   66:         }
   67:  
   68:         ////////////////////////////////////////////////////////////////////////////
   69:  
   70:         /// <summary>
   71:         ///
   72:         /// </summary>
   73:         protected string HexToUtf8(string hex_str)
   74:         {
   75:             int i, n;
   76:             byte[] bytes;
   77:             char[] chars;
   78:             string c_str = "", s = "";
   79:             UTF8Encoding utf8 = new UTF8Encoding();
   80:  
   81:             chars = hex_str.ToCharArray();
   82:  
   83:             bytes = new byte[chars.Length / 2];  // since hex_str has to chars for every byte
   84:  
   85:             n = 0;
   86:             for (i = 0; i < chars.Length; i += 2)
   87:             {
   88:                 c_str = chars[i].ToString() + chars[i + 1].ToString();
   89:                 bytes[n++] = (byte)Convert.ToInt32(c_str, 16);
   90:             }
   91:  
   92:             s = utf8.GetString(bytes);
   93:  
   94:             return s;
   95:         }
   96:  
   97:         ////////////////////////////////////////////////////////////////////////////
   98:  
   99:         /// <summary>
  100:         ///
  101:         /// </summary>
  102:         protected string Utf8ToAscii(string utf8_str)
  103:         {
  104:             byte[] bytes;
  105:             string str = "";
  106:             UTF8Encoding utf8 = new UTF8Encoding();
  107:             ASCIIEncoding ascii = new ASCIIEncoding();
  108:  
  109:             bytes = utf8.GetBytes(utf8_str);
  110:  
  111:             foreach (byte b in bytes)
  112:             {
  113:                 str += (char)b;
  114:             }
  115:  
  116:             /*
  117:                foreach(byte b in bytes)
  118:                {
  119:                 Response.Write(" " + b +":"+ (char)b);
  120:                }
  121:             */
  122:  
  123:             //  str = ascii.GetString(bytes);
  124:             //  str = utf8.GetString(bytes);
  125:  
  126:             return str;
  127:         }
  128:  
  129:         ////////////////////////////////////////////////////////////////////////////
  130:  
  131:         /// <summary>
  132:         ///
  133:         /// </summary>
  134:         protected string AsciiToUtf8(string ascii_str)
  135:         {
  136:             char[] chars;
  137:             string str = "";
  138:             UTF8Encoding utf8 = new UTF8Encoding();
  139:             ASCIIEncoding ascii = new ASCIIEncoding();
  140:  
  141:             chars = ascii_str.ToCharArray();
  142:  
  143:             int i = 0;
  144:             byte[] bytes = new byte[chars.Length];
  145:  
  146:             foreach (char c in chars)
  147:             {
  148:                 bytes[i++] += (byte)c;
  149:             }
  150:  
  151:             //  bytes = ascii.GetBytes(ascii_str);
  152:             //  bytes = utf8.GetBytes(ascii_str);
  153:  
  154:             str = utf8.GetString(bytes);
  155:  
  156:             return str;
  157:         }
  158:  
  159:         ////////////////////////////////////////////////////////////////////////////
  160:  
  161:         /// <summary>
  162:         ///
  163:         /// </summary>
  164:         protected byte[] Utf8ToByte(string text)
  165:         {
  166:             UTF8Encoding utf8 = new UTF8Encoding();
  167:  
  168:             // Encode the string.
  169:             byte[] encodedBytes = utf8.GetBytes(text);
  170:  
  171:             return encodedBytes;
  172:         }
  173:  
  174:         ////////////////////////////////////////////////////////////////////////////
  175:  
  176:         /// <summary>
  177:         ///
  178:         /// </summary>
  179:         protected string ByteToUtf8(byte[] text)
  180:         {
  181:             string ichi;
  182:             UTF8Encoding utf8 = new UTF8Encoding();
  183:             // Decode bytes back to string.
  184:  
  185:             ichi = utf8.GetString(text);
  186:  
  187:             return ichi;
  188:         }
  189:  
  190:         ////////////////////////////////////////////////////////////////////////////
  191:  
  192:         /// <summary>
  193:         ///
  194:         /// </summary>
  195:         protected string ByteToChar(byte[] text)
  196:         {
  197:             string ichi = "";
  198:  
  199:             foreach (byte b in text)
  200:             {
  201:                 ichi += (char)b;
  202:             }
  203:  
  204:             return ichi;
  205:         }
  206:  
  207:         ////////////////////////////////////////////////////////////////////////////
  208:  
  209:         /// <summary>
  210:         ///
  211:         /// </summary>
  212:         protected string HtmlEncode(string encode)
  213:         {
  214:  
  215:             // Response.Write("[[["+ encode);
  216:  
  217:             encode = Utf8ToAscii(encode);
  218:  
  219:             encode = WebUtility.HtmlEncode(encode);
  220:  
  221:             encode = encode.Replace(@"'", @"&#039");
  222:             encode = encode.Replace(@"\", @"&#092");
  223:             encode = encode.Replace(@"?", @"&#063"); // the MySQL database has problems with the question mark '?'
  224:  
  225:             // Response.Write(":"+ encode);
  226:  
  227:             encode = encode.Replace(@"Ž", @"&#142");
  228:  
  229:             // Response.Write(":"+ encode+"]]]");
  230:  
  231:             return encode;
  232:         }
  233:  
  234:         ////////////////////////////////////////////////////////////////////////////
  235:  
  236:         /// <summary>
  237:         ///
  238:         /// </summary>
  239:         string HtmlDecode(string decode)
  240:         {
  241:             decode = decode.Replace(@"&#142", @"Ž");
  242:  
  243:             decode = decode.Replace(@"&#039", @"'");
  244:             decode = decode.Replace(@"&#092", @"\");
  245:             decode = decode.Replace(@"&#063", @"?"); // the MySQL database has problems with the question mark '?'
  246:  
  247:             decode = WebUtility.HtmlDecode(decode);
  248:             decode = AsciiToUtf8(decode);
  249:  
  250:             return decode;
  251:         }
  252:  
  253:         ////////////////////////////////////////////////////////////////////////////
  254:         ////////////////////////////////////////////////////////////////////////////
  255:     }
  256: }