شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Utf8

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.Web;
   3:  using System.Web.UI;
   4:  using System.Web.UI.WebControls;
   5:  using System.Web.UI.HtmlControls;
   6:  using System.IO;
   7:  using System.Xml;
   8:  using System.Data;
   9:  using System.Data.OleDb;
  10:  using System.Text;
  11:  using System.Collections;
  12:  using System.Text.RegularExpressions;
  13:  using System.Globalization;
  14:   
  15:  namespace Ia.Cl.Model
  16:  {
  17:      ////////////////////////////////////////////////////////////////////////////
  18:   
  19:      /// <summary publish="true">
  20:      /// Handle UTF8 issues.
  21:      /// </summary>
  22:      /// <remarks> 
  23:      /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  24:      ///
  25:      /// 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
  26:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  27:      ///
  28:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  29:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  30:      /// 
  31:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  32:      /// 
  33:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  34:      /// </remarks> 
  35:      public class Utf8 : System.Web.UI.Page
  36:      {
  37:          /// <summary/>
  38:          protected DataSet ds;
  39:   
  40:          /// <summary/>
  41:          protected OleDbDataAdapter da;
  42:   
  43:          /// <summary/>
  44:          protected DataTable dt;
  45:   
  46:          /// <summary/>
  47:          protected Label result_l;
  48:   
  49:          /// <summary/>
  50:          public string connection_string = "Provider=MySQLProv;Location=localhost;Data Source=*;UID=;PWD=";
  51:   
  52:          ////////////////////////////////////////////////////////////////////////////
  53:   
  54:          /// <summary>
  55:          ///
  56:          /// </summary>
  57:          protected void Page_Load(object sender, EventArgs e)
  58:          {
  59:              UTF8Encoding utf8 = new UTF8Encoding();
  60:   
  61:              result_l.Text += "<br><br>X: " + 123.ToString("X");
  62:   
  63:              string line = "This unicode string Pi (\u03a0) and Sigma (\u03a3) and (أهلاً و سهلاً) and (日本).";
  64:              result_l.Text += "<br><br>Original: " + line;
  65:   
  66:              char[] chars;
  67:              chars = line.ToCharArray();
  68:   
  69:              /////////////////////////
  70:   
  71:              result_l.Text += "<br><br>Chars: ";
  72:              foreach (char c in chars)
  73:              {
  74:                  result_l.Text += "|" + c;
  75:              }
  76:   
  77:              /////////////////////////
  78:   
  79:              result_l.Text += "<br><br>Bytes inside Chars: ";
  80:              string s, hex = "";
  81:              foreach (char c in chars)
  82:              {
  83:                  result_l.Text += "[" + c + "]: [";
  84:                  s = c.ToString();
  85:   
  86:                  Byte[] line5 = utf8.GetBytes(s);
  87:                  hex = "";
  88:                  foreach (Byte b in line5)
  89:                  {
  90:                      hex += b.ToString("x");
  91:                      result_l.Text += "|" + (char)b + "(" + b + ")";
  92:                  }
  93:                  //   if(hex.Length == 2) hex = "00" + hex;
  94:                  result_l.Text += @"{|" + hex + "}";
  95:   
  96:                  result_l.Text += "]<br>";
  97:   
  98:              }
  99:   
 100:              /////////////////////////
 101:   
 102:              Byte[] line2 = utf8.GetBytes(line);
 103:              result_l.Text += "<br><br>Bytes: ";
 104:              foreach (Byte b in line2)
 105:              {
 106:                  result_l.Text += "|" + (char)b;
 107:              }
 108:   
 109:              /////////////////////////
 110:   
 111:              // Decode bytes back to string.
 112:              // Notice Pi and Sigma characters are still present.
 113:              string line3 = utf8.GetString(line2);
 114:              result_l.Text += "<br><br>Decoded: " + line3;
 115:   
 116:              /////////////////////////
 117:   
 118:              result_l.Text += "<br><br>Convert line with my function: " + Utf8ToHex(line);
 119:   
 120:              /////////////////////////
 121:   
 122:              result_l.Text += "<br><br>Reconstruct line with my function: " + HexToUtf8(Utf8ToHex(line));
 123:          }
 124:   
 125:          ////////////////////////////////////////////////////////////////////////////
 126:   
 127:          /// <summary>
 128:          ///
 129:          /// </summary>
 130:          protected string Utf8ToHex(string utf8_str)
 131:          {
 132:              byte[] bytes;
 133:              string s = "";
 134:              UTF8Encoding utf8 = new UTF8Encoding();
 135:   
 136:              bytes = utf8.GetBytes(utf8_str);
 137:   
 138:              foreach (byte b in bytes) s += b.ToString("x");
 139:   
 140:              return s;
 141:          }
 142:   
 143:          ////////////////////////////////////////////////////////////////////////////
 144:   
 145:          /// <summary>
 146:          ///
 147:          /// </summary>
 148:          protected string HexToUtf8(string hex_str)
 149:          {
 150:              int i, n;
 151:              byte[] bytes;
 152:              char[] chars;
 153:              string c_str = "", s = "";
 154:              UTF8Encoding utf8 = new UTF8Encoding();
 155:   
 156:              chars = hex_str.ToCharArray();
 157:   
 158:              bytes = new byte[chars.Length / 2];  // since hex_str has to chars for every byte
 159:   
 160:              n = 0;
 161:              for (i = 0; i < chars.Length; i += 2)
 162:              {
 163:                  c_str = chars[i].ToString() + chars[i + 1].ToString();
 164:                  bytes[n++] = (byte)Convert.ToInt32(c_str, 16);
 165:              }
 166:   
 167:              s = utf8.GetString(bytes);
 168:   
 169:              return s;
 170:          }
 171:   
 172:          ////////////////////////////////////////////////////////////////////////////
 173:   
 174:          /// <summary>
 175:          ///
 176:          /// </summary>
 177:          protected string Utf8ToAscii(string utf8_str)
 178:          {
 179:              byte[] bytes;
 180:              string str = "";
 181:              UTF8Encoding utf8 = new UTF8Encoding();
 182:              ASCIIEncoding ascii = new ASCIIEncoding();
 183:   
 184:              bytes = utf8.GetBytes(utf8_str);
 185:   
 186:              foreach (byte b in bytes)
 187:              {
 188:                  str += (char)b;
 189:              }
 190:   
 191:              /*
 192:                 foreach(byte b in bytes)
 193:                 {
 194:                  Response.Write(" " + b +":"+ (char)b);
 195:                 }
 196:              */
 197:   
 198:              //  str = ascii.GetString(bytes);
 199:              //  str = utf8.GetString(bytes);
 200:   
 201:              return str;
 202:          }
 203:   
 204:          ////////////////////////////////////////////////////////////////////////////
 205:   
 206:          /// <summary>
 207:          ///
 208:          /// </summary>
 209:          protected string AsciiToUtf8(string ascii_str)
 210:          {
 211:              char[] chars;
 212:              string str = "";
 213:              UTF8Encoding utf8 = new UTF8Encoding();
 214:              ASCIIEncoding ascii = new ASCIIEncoding();
 215:   
 216:              chars = ascii_str.ToCharArray();
 217:   
 218:              int i = 0;
 219:              byte[] bytes = new byte[chars.Length];
 220:   
 221:              foreach (char c in chars)
 222:              {
 223:                  bytes[i++] += (byte)c;
 224:              }
 225:   
 226:              //  bytes = ascii.GetBytes(ascii_str);
 227:              //  bytes = utf8.GetBytes(ascii_str);
 228:   
 229:              str = utf8.GetString(bytes);
 230:   
 231:              return str;
 232:          }
 233:   
 234:          ////////////////////////////////////////////////////////////////////////////
 235:   
 236:          /// <summary>
 237:          ///
 238:          /// </summary>
 239:          protected byte[] Utf8ToByte(string text)
 240:          {
 241:              UTF8Encoding utf8 = new UTF8Encoding();
 242:   
 243:              // Encode the string.
 244:              byte[] encodedBytes = utf8.GetBytes(text);
 245:   
 246:              return encodedBytes;
 247:          }
 248:   
 249:          ////////////////////////////////////////////////////////////////////////////
 250:   
 251:          /// <summary>
 252:          ///
 253:          /// </summary>
 254:          protected string ByteToUtf8(byte[] text)
 255:          {
 256:              string ichi;
 257:              UTF8Encoding utf8 = new UTF8Encoding();
 258:              // Decode bytes back to string.
 259:   
 260:              ichi = utf8.GetString(text);
 261:   
 262:              return ichi;
 263:          }
 264:   
 265:          ////////////////////////////////////////////////////////////////////////////
 266:   
 267:          /// <summary>
 268:          ///
 269:          /// </summary>
 270:          protected string ByteToChar(byte[] text)
 271:          {
 272:              string ichi = "";
 273:   
 274:              foreach (byte b in text)
 275:              {
 276:                  ichi += (char)b;
 277:              }
 278:   
 279:              return ichi;
 280:          }
 281:   
 282:          ////////////////////////////////////////////////////////////////////////////
 283:   
 284:          /// <summary>
 285:          ///
 286:          /// </summary>
 287:          protected string HtmlEncode(string encode)
 288:          {
 289:   
 290:              // Response.Write("[[["+ encode);
 291:   
 292:              encode = Utf8ToAscii(encode);
 293:   
 294:              encode = Server.HtmlEncode(encode);
 295:   
 296:              encode = encode.Replace(@"'", @"&#039");
 297:              encode = encode.Replace(@"\", @"&#092");
 298:              encode = encode.Replace(@"?", @"&#063"); // the MySQL database has problems with the question mark '?'
 299:   
 300:              // Response.Write(":"+ encode);
 301:   
 302:              encode = encode.Replace(@"Ž", @"&#142");
 303:   
 304:              // Response.Write(":"+ encode+"]]]");
 305:   
 306:              return encode;
 307:          }
 308:   
 309:          ////////////////////////////////////////////////////////////////////////////
 310:   
 311:          /// <summary>
 312:          ///
 313:          /// </summary>
 314:          string HtmlDecode(string decode)
 315:          {
 316:              decode = decode.Replace(@"&#142", @"Ž");
 317:   
 318:              decode = decode.Replace(@"&#039", @"'");
 319:              decode = decode.Replace(@"&#092", @"\");
 320:              decode = decode.Replace(@"&#063", @"?"); // the MySQL database has problems with the question mark '?'
 321:   
 322:              decode = Server.HtmlDecode(decode);
 323:              decode = AsciiToUtf8(decode);
 324:   
 325:              return decode;
 326:          }
 327:   
 328:          ////////////////////////////////////////////////////////////////////////////
 329:          ////////////////////////////////////////////////////////////////////////////
 330:      }
 331:  }