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

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.Data;
   3:  using System.Data.OleDb;
   4:  using System.Text;
   5:  using System.Web.UI.WebControls;
   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 : System.Web.UI.Page
  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:          protected Label result_l;
  40:   
  41:          /// <summary/>
  42:          public string connection_string = "Provider=MySQLProv;Location=localhost;Data Source=*;UID=;PWD=";
  43:   
  44:          ////////////////////////////////////////////////////////////////////////////
  45:   
  46:          /// <summary>
  47:          ///
  48:          /// </summary>
  49:          protected void Page_Load(object sender, EventArgs e)
  50:          {
  51:              UTF8Encoding utf8 = new UTF8Encoding();
  52:   
  53:              result_l.Text += "<br><br>X: " + 123.ToString("X");
  54:   
  55:              string line = "This unicode string Pi (\u03a0) and Sigma (\u03a3) and (أهلاً وسهلاً) and (日本).";
  56:              result_l.Text += "<br><br>Original: " + line;
  57:   
  58:              char[] chars;
  59:              chars = line.ToCharArray();
  60:   
  61:              /////////////////////////
  62:   
  63:              result_l.Text += "<br><br>Chars: ";
  64:              foreach (char c in chars)
  65:              {
  66:                  result_l.Text += "|" + c;
  67:              }
  68:   
  69:              /////////////////////////
  70:   
  71:              result_l.Text += "<br><br>Bytes inside Chars: ";
  72:              string s, hex = "";
  73:              foreach (char c in chars)
  74:              {
  75:                  result_l.Text += "[" + c + "]: [";
  76:                  s = c.ToString();
  77:   
  78:                  Byte[] line5 = utf8.GetBytes(s);
  79:                  hex = "";
  80:                  foreach (Byte b in line5)
  81:                  {
  82:                      hex += b.ToString("x");
  83:                      result_l.Text += "|" + (char)b + "(" + b + ")";
  84:                  }
  85:                  //   if(hex.Length == 2) hex = "00" + hex;
  86:                  result_l.Text += @"{|" + hex + "}";
  87:   
  88:                  result_l.Text += "]<br>";
  89:   
  90:              }
  91:   
  92:              /////////////////////////
  93:   
  94:              Byte[] line2 = utf8.GetBytes(line);
  95:              result_l.Text += "<br><br>Bytes: ";
  96:              foreach (Byte b in line2)
  97:              {
  98:                  result_l.Text += "|" + (char)b;
  99:              }
 100:   
 101:              /////////////////////////
 102:   
 103:              // Decode bytes back to string.
 104:              // Notice Pi and Sigma characters are still present.
 105:              string line3 = utf8.GetString(line2);
 106:              result_l.Text += "<br><br>Decoded: " + line3;
 107:   
 108:              /////////////////////////
 109:   
 110:              result_l.Text += "<br><br>Convert line with my function: " + Utf8ToHex(line);
 111:   
 112:              /////////////////////////
 113:   
 114:              result_l.Text += "<br><br>Reconstruct line with my function: " + HexToUtf8(Utf8ToHex(line));
 115:          }
 116:   
 117:          ////////////////////////////////////////////////////////////////////////////
 118:   
 119:          /// <summary>
 120:          ///
 121:          /// </summary>
 122:          protected string Utf8ToHex(string utf8_str)
 123:          {
 124:              byte[] bytes;
 125:              string s = "";
 126:              UTF8Encoding utf8 = new UTF8Encoding();
 127:   
 128:              bytes = utf8.GetBytes(utf8_str);
 129:   
 130:              foreach (byte b in bytes) s += b.ToString("x");
 131:   
 132:              return s;
 133:          }
 134:   
 135:          ////////////////////////////////////////////////////////////////////////////
 136:   
 137:          /// <summary>
 138:          ///
 139:          /// </summary>
 140:          protected string HexToUtf8(string hex_str)
 141:          {
 142:              int i, n;
 143:              byte[] bytes;
 144:              char[] chars;
 145:              string c_str = "", s = "";
 146:              UTF8Encoding utf8 = new UTF8Encoding();
 147:   
 148:              chars = hex_str.ToCharArray();
 149:   
 150:              bytes = new byte[chars.Length / 2];  // since hex_str has to chars for every byte
 151:   
 152:              n = 0;
 153:              for (i = 0; i < chars.Length; i += 2)
 154:              {
 155:                  c_str = chars[i].ToString() + chars[i + 1].ToString();
 156:                  bytes[n++] = (byte)Convert.ToInt32(c_str, 16);
 157:              }
 158:   
 159:              s = utf8.GetString(bytes);
 160:   
 161:              return s;
 162:          }
 163:   
 164:          ////////////////////////////////////////////////////////////////////////////
 165:   
 166:          /// <summary>
 167:          ///
 168:          /// </summary>
 169:          protected string Utf8ToAscii(string utf8_str)
 170:          {
 171:              byte[] bytes;
 172:              string str = "";
 173:              UTF8Encoding utf8 = new UTF8Encoding();
 174:              ASCIIEncoding ascii = new ASCIIEncoding();
 175:   
 176:              bytes = utf8.GetBytes(utf8_str);
 177:   
 178:              foreach (byte b in bytes)
 179:              {
 180:                  str += (char)b;
 181:              }
 182:   
 183:              /*
 184:                 foreach(byte b in bytes)
 185:                 {
 186:                  Response.Write(" " + b +":"+ (char)b);
 187:                 }
 188:              */
 189:   
 190:              //  str = ascii.GetString(bytes);
 191:              //  str = utf8.GetString(bytes);
 192:   
 193:              return str;
 194:          }
 195:   
 196:          ////////////////////////////////////////////////////////////////////////////
 197:   
 198:          /// <summary>
 199:          ///
 200:          /// </summary>
 201:          protected string AsciiToUtf8(string ascii_str)
 202:          {
 203:              char[] chars;
 204:              string str = "";
 205:              UTF8Encoding utf8 = new UTF8Encoding();
 206:              ASCIIEncoding ascii = new ASCIIEncoding();
 207:   
 208:              chars = ascii_str.ToCharArray();
 209:   
 210:              int i = 0;
 211:              byte[] bytes = new byte[chars.Length];
 212:   
 213:              foreach (char c in chars)
 214:              {
 215:                  bytes[i++] += (byte)c;
 216:              }
 217:   
 218:              //  bytes = ascii.GetBytes(ascii_str);
 219:              //  bytes = utf8.GetBytes(ascii_str);
 220:   
 221:              str = utf8.GetString(bytes);
 222:   
 223:              return str;
 224:          }
 225:   
 226:          ////////////////////////////////////////////////////////////////////////////
 227:   
 228:          /// <summary>
 229:          ///
 230:          /// </summary>
 231:          protected byte[] Utf8ToByte(string text)
 232:          {
 233:              UTF8Encoding utf8 = new UTF8Encoding();
 234:   
 235:              // Encode the string.
 236:              byte[] encodedBytes = utf8.GetBytes(text);
 237:   
 238:              return encodedBytes;
 239:          }
 240:   
 241:          ////////////////////////////////////////////////////////////////////////////
 242:   
 243:          /// <summary>
 244:          ///
 245:          /// </summary>
 246:          protected string ByteToUtf8(byte[] text)
 247:          {
 248:              string ichi;
 249:              UTF8Encoding utf8 = new UTF8Encoding();
 250:              // Decode bytes back to string.
 251:   
 252:              ichi = utf8.GetString(text);
 253:   
 254:              return ichi;
 255:          }
 256:   
 257:          ////////////////////////////////////////////////////////////////////////////
 258:   
 259:          /// <summary>
 260:          ///
 261:          /// </summary>
 262:          protected string ByteToChar(byte[] text)
 263:          {
 264:              string ichi = "";
 265:   
 266:              foreach (byte b in text)
 267:              {
 268:                  ichi += (char)b;
 269:              }
 270:   
 271:              return ichi;
 272:          }
 273:   
 274:          ////////////////////////////////////////////////////////////////////////////
 275:   
 276:          /// <summary>
 277:          ///
 278:          /// </summary>
 279:          protected string HtmlEncode(string encode)
 280:          {
 281:   
 282:              // Response.Write("[[["+ encode);
 283:   
 284:              encode = Utf8ToAscii(encode);
 285:   
 286:              encode = Server.HtmlEncode(encode);
 287:   
 288:              encode = encode.Replace(@"'", @"&#039");
 289:              encode = encode.Replace(@"\", @"&#092");
 290:              encode = encode.Replace(@"?", @"&#063"); // the MySQL database has problems with the question mark '?'
 291:   
 292:              // Response.Write(":"+ encode);
 293:   
 294:              encode = encode.Replace(@"Ž", @"&#142");
 295:   
 296:              // Response.Write(":"+ encode+"]]]");
 297:   
 298:              return encode;
 299:          }
 300:   
 301:          ////////////////////////////////////////////////////////////////////////////
 302:   
 303:          /// <summary>
 304:          ///
 305:          /// </summary>
 306:          string HtmlDecode(string decode)
 307:          {
 308:              decode = decode.Replace(@"&#142", @"Ž");
 309:   
 310:              decode = decode.Replace(@"&#039", @"'");
 311:              decode = decode.Replace(@"&#092", @"\");
 312:              decode = decode.Replace(@"&#063", @"?"); // the MySQL database has problems with the question mark '?'
 313:   
 314:              decode = Server.HtmlDecode(decode);
 315:              decode = AsciiToUtf8(decode);
 316:   
 317:              return decode;
 318:          }
 319:   
 320:          ////////////////////////////////////////////////////////////////////////////
 321:          ////////////////////////////////////////////////////////////////////////////
 322:      }
 323:  }