XmlConvert.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. //
  2. // System.Xml.XmlConvert
  3. //
  4. // Authors:
  5. // Dwivedi, Ajay kumar ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Alan Tam Siu Lung ([email protected])
  8. // Atsushi Enomoto ([email protected])
  9. //
  10. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.IO;
  34. using System.Text;
  35. using System.Globalization;
  36. using System.Xml.Schema;
  37. namespace System.Xml {
  38. public class XmlConvert {
  39. const string encodedColon = "_x003A_";
  40. const NumberStyles floatStyle = NumberStyles.AllowCurrencySymbol |
  41. NumberStyles.AllowExponent |
  42. NumberStyles.AllowDecimalPoint |
  43. NumberStyles.AllowLeadingSign;
  44. static readonly string [] datetimeFormats = {
  45. // dateTime
  46. "yyyy-MM-ddTHH:mm:ss",
  47. "yyyy-MM-ddTHH:mm:ss.f",
  48. "yyyy-MM-ddTHH:mm:ss.ff",
  49. "yyyy-MM-ddTHH:mm:ss.fff",
  50. "yyyy-MM-ddTHH:mm:ss.ffff",
  51. "yyyy-MM-ddTHH:mm:ss.fffff",
  52. "yyyy-MM-ddTHH:mm:ss.ffffff",
  53. "yyyy-MM-ddTHH:mm:ss.fffffff",
  54. "yyyy-MM-ddTHH:mm:sszzz",
  55. "yyyy-MM-ddTHH:mm:ss.fzzz",
  56. "yyyy-MM-ddTHH:mm:ss.ffzzz",
  57. "yyyy-MM-ddTHH:mm:ss.fffzzz",
  58. "yyyy-MM-ddTHH:mm:ss.ffffzzz",
  59. "yyyy-MM-ddTHH:mm:ss.fffffzzz",
  60. "yyyy-MM-ddTHH:mm:ss.ffffffzzz",
  61. "yyyy-MM-ddTHH:mm:ss.fffffffzzz",
  62. "yyyy-MM-ddTHH:mm:ssZ",
  63. "yyyy-MM-ddTHH:mm:ss.fZ",
  64. "yyyy-MM-ddTHH:mm:ss.ffZ",
  65. "yyyy-MM-ddTHH:mm:ss.fffZ",
  66. "yyyy-MM-ddTHH:mm:ss.ffffZ",
  67. "yyyy-MM-ddTHH:mm:ss.fffffZ",
  68. "yyyy-MM-ddTHH:mm:ss.ffffffZ",
  69. "yyyy-MM-ddTHH:mm:ss.fffffffZ",
  70. // time
  71. "HH:mm:ss",
  72. "HH:mm:ss.f",
  73. "HH:mm:ss.ff",
  74. "HH:mm:ss.fff",
  75. "HH:mm:ss.ffff",
  76. "HH:mm:ss.fffff",
  77. "HH:mm:ss.ffffff",
  78. "HH:mm:ss.fffffff",
  79. "HH:mm:sszzz",
  80. "HH:mm:ss.fzzz",
  81. "HH:mm:ss.ffzzz",
  82. "HH:mm:ss.fffzzz",
  83. "HH:mm:ss.ffffzzz",
  84. "HH:mm:ss.fffffzzz",
  85. "HH:mm:ss.ffffffzzz",
  86. "HH:mm:ss.fffffffzzz",
  87. "HH:mm:ssZ",
  88. "HH:mm:ss.fZ",
  89. "HH:mm:ss.ffZ",
  90. "HH:mm:ss.fffZ",
  91. "HH:mm:ss.ffffZ",
  92. "HH:mm:ss.fffffZ",
  93. "HH:mm:ss.ffffffZ",
  94. "HH:mm:ss.fffffffZ",
  95. // date
  96. "yyyy-MM-dd",
  97. "yyyy-MM-ddzzz",
  98. "yyyy-MM-ddZ",
  99. // gYearMonth
  100. "yyyy-MM",
  101. "yyyy-MMzzz",
  102. "yyyy-MMZ",
  103. // gYear
  104. "yyyy",
  105. "yyyyzzz",
  106. "yyyyZ",
  107. // gMonthDay
  108. "--MM-dd",
  109. "--MM-ddzzz",
  110. "--MM-ddZ",
  111. // gDay
  112. "---dd",
  113. "---ddzzz",
  114. "---ddZ",
  115. };
  116. public XmlConvert()
  117. {}
  118. private static string TryDecoding (string s)
  119. {
  120. if (s == null || s.Length < 6)
  121. return s;
  122. char c = '\uFFFF';
  123. try {
  124. c = (char) Int32.Parse (s.Substring (1, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
  125. } catch {
  126. return s [0] + DecodeName (s.Substring (1));
  127. }
  128. if (s.Length == 6)
  129. return c.ToString ();
  130. return c + DecodeName (s.Substring (6));
  131. }
  132. public static string DecodeName (string name)
  133. {
  134. if (name == null || name.Length == 0)
  135. return name;
  136. int pos = name.IndexOf ('_');
  137. if (pos == -1 || pos + 6 >= name.Length)
  138. return name;
  139. if ((name [pos + 1] != 'X' && name [pos + 1] != 'x') || name [pos + 6] != '_')
  140. return name [0] + DecodeName (name.Substring (1));
  141. return name.Substring (0, pos) + TryDecoding (name.Substring (pos + 1));
  142. }
  143. public static string EncodeLocalName (string name)
  144. {
  145. string encoded = EncodeName (name);
  146. int pos = encoded.IndexOf (':');
  147. if (pos == -1)
  148. return encoded;
  149. return encoded.Replace (":", encodedColon);
  150. }
  151. internal static bool IsInvalid (char c, bool firstOnlyLetter)
  152. {
  153. if (c == ':') // Special case. allowed in EncodeName, but encoded in EncodeLocalName
  154. return false;
  155. if (firstOnlyLetter)
  156. return !XmlChar.IsFirstNameChar (c);
  157. else
  158. return !XmlChar.IsNameChar (c);
  159. }
  160. private static string EncodeName (string name, bool nmtoken)
  161. {
  162. StringBuilder sb = new StringBuilder ();
  163. int length = name.Length;
  164. for (int i = 0; i < length; i++) {
  165. char c = name [i];
  166. if (IsInvalid (c, i == 0 && !nmtoken))
  167. sb.AppendFormat ("_x{0:X4}_", (int) c);
  168. else if (c == '_' && i + 6 < length && name [i+1] == 'x' && name [i + 6] == '_')
  169. sb.Append ("_x005F_");
  170. else
  171. sb.Append (c);
  172. }
  173. return sb.ToString ();
  174. }
  175. public static string EncodeName (string name)
  176. {
  177. return EncodeName (name, false);
  178. }
  179. public static string EncodeNmToken(string name)
  180. {
  181. return EncodeName (name, true);
  182. }
  183. // {true, false, 1, 0}
  184. public static bool ToBoolean(string s)
  185. {
  186. s = s.Trim (XmlChar.WhitespaceChars);
  187. switch(s)
  188. {
  189. case "1":
  190. return true;
  191. case "true":
  192. return true;
  193. case "0":
  194. return false;
  195. case "false":
  196. return false;
  197. default:
  198. throw new FormatException(s + " is not a valid boolean value");
  199. }
  200. }
  201. // LAMESPEC: It has been documented as public, but is marked as internal.
  202. internal static string ToBinHexString (byte [] buffer)
  203. {
  204. StringWriter w = new StringWriter ();
  205. WriteBinHex (buffer, 0, buffer.Length, w);
  206. return w.ToString ();
  207. }
  208. internal static void WriteBinHex (byte [] buffer, int index, int count, TextWriter w)
  209. {
  210. if (buffer == null)
  211. throw new ArgumentNullException ("buffer");
  212. if (index < 0)
  213. throw new ArgumentOutOfRangeException ("index", index, "index must be non negative integer.");
  214. if (count < 0)
  215. throw new ArgumentOutOfRangeException ("count", count, "count must be non negative integer.");
  216. if (buffer.Length < index + count)
  217. throw new ArgumentOutOfRangeException ("index and count must be smaller than the length of the buffer.");
  218. // Copied from XmlTextWriter.WriteBinHex ()
  219. int end = index + count;
  220. for (int i = index; i < end; i++) {
  221. int val = buffer [i];
  222. int high = val >> 4;
  223. int low = val & 15;
  224. if (high > 9)
  225. w.Write ((char) (high + 55));
  226. else
  227. w.Write ((char) (high + 0x30));
  228. if (low > 9)
  229. w.Write ((char) (low + 55));
  230. else
  231. w.Write ((char) (low + 0x30));
  232. }
  233. }
  234. public static byte ToByte(string s)
  235. {
  236. return Byte.Parse(s, CultureInfo.InvariantCulture);
  237. }
  238. public static char ToChar(string s)
  239. {
  240. return Char.Parse(s);
  241. }
  242. #if NET_2_0
  243. [Obsolete]
  244. #endif
  245. public static DateTime ToDateTime (string s)
  246. {
  247. return ToDateTime (s, datetimeFormats);
  248. }
  249. #if NET_2_0
  250. public static DateTime ToDateTime (string value, XmlDateTimeSerializationMode mode)
  251. {
  252. string modestr = null;
  253. switch (mode) {
  254. case XmlDateTimeSerializationMode.Local:
  255. case XmlDateTimeSerializationMode.RoundTripKind:
  256. default:
  257. return ToDateTime (value, "yyyy-MM-ddTHH:mm:ss.fffffffzzz");
  258. case XmlDateTimeSerializationMode.Utc:
  259. return ToDateTime (value, "yyyy-MM-ddTHH:mm:ss.fffffffZ").ToUniversalTime ();
  260. case XmlDateTimeSerializationMode.Unspecified:
  261. return ToDateTime (value, "yyyy-MM-ddTHH:mm:ss.fffffff");
  262. }
  263. }
  264. #endif
  265. public static DateTime ToDateTime(string s, string format)
  266. {
  267. DateTimeFormatInfo d = new DateTimeFormatInfo();
  268. d.FullDateTimePattern = format;
  269. return DateTime.Parse(s, d);
  270. }
  271. public static DateTime ToDateTime(string s, string[] formats)
  272. {
  273. DateTimeStyles style = DateTimeStyles.AllowLeadingWhite |
  274. DateTimeStyles.AllowTrailingWhite;
  275. return DateTime.ParseExact (s, formats, DateTimeFormatInfo.InvariantInfo, style);
  276. }
  277. public static Decimal ToDecimal(string s)
  278. {
  279. return Decimal.Parse(s, CultureInfo.InvariantCulture);
  280. }
  281. public static double ToDouble(string s)
  282. {
  283. if (s == null)
  284. throw new ArgumentNullException();
  285. if (s == "INF")
  286. return Double.PositiveInfinity;
  287. if (s == "-INF")
  288. return Double.NegativeInfinity;
  289. if (s == "NaN")
  290. return Double.NaN;
  291. return Double.Parse (s, floatStyle, CultureInfo.InvariantCulture);
  292. }
  293. public static Guid ToGuid(string s)
  294. {
  295. return new Guid(s);
  296. }
  297. public static short ToInt16(string s)
  298. {
  299. return Int16.Parse(s, CultureInfo.InvariantCulture);
  300. }
  301. public static int ToInt32(string s)
  302. {
  303. return Int32.Parse(s, CultureInfo.InvariantCulture);
  304. }
  305. public static long ToInt64(string s)
  306. {
  307. return Int64.Parse(s, CultureInfo.InvariantCulture);
  308. }
  309. [CLSCompliant (false)]
  310. public static SByte ToSByte(string s)
  311. {
  312. return SByte.Parse(s, CultureInfo.InvariantCulture);
  313. }
  314. public static float ToSingle(string s)
  315. {
  316. if (s == null)
  317. throw new ArgumentNullException();
  318. if (s == "INF")
  319. return Single.PositiveInfinity;
  320. if (s == "-INF")
  321. return Single.NegativeInfinity;
  322. if (s == "NaN")
  323. return Single.NaN;
  324. return Single.Parse(s, floatStyle, CultureInfo.InvariantCulture);
  325. }
  326. public static string ToString(Guid value)
  327. {
  328. return value.ToString("D", CultureInfo.InvariantCulture);
  329. }
  330. public static string ToString(int value)
  331. {
  332. return value.ToString(CultureInfo.InvariantCulture);
  333. }
  334. public static string ToString(short value)
  335. {
  336. return value.ToString(CultureInfo.InvariantCulture);
  337. }
  338. public static string ToString(byte value)
  339. {
  340. return value.ToString(CultureInfo.InvariantCulture);
  341. }
  342. public static string ToString(long value)
  343. {
  344. return value.ToString(CultureInfo.InvariantCulture);
  345. }
  346. public static string ToString(char value)
  347. {
  348. return value.ToString(CultureInfo.InvariantCulture);
  349. }
  350. public static string ToString(bool value)
  351. {
  352. if (value) return "true";
  353. return "false";
  354. }
  355. [CLSCompliant (false)]
  356. public static string ToString(SByte value)
  357. {
  358. return value.ToString(CultureInfo.InvariantCulture);
  359. }
  360. public static string ToString(Decimal value)
  361. {
  362. return value.ToString (CultureInfo.InvariantCulture);
  363. }
  364. [CLSCompliant (false)]
  365. public static string ToString(UInt64 value)
  366. {
  367. return value.ToString(CultureInfo.InvariantCulture);
  368. }
  369. public static string ToString (TimeSpan value)
  370. {
  371. StringBuilder builder = new StringBuilder ();
  372. if (value.Ticks < 0) {
  373. builder.Append ('-');
  374. value = value.Negate ();
  375. }
  376. builder.Append ('P');
  377. if (value.Days > 0)
  378. builder.Append (value.Days).Append ('D');
  379. if (value.Days > 0 || value.Hours > 0 || value.Minutes > 0 || value.Seconds > 0 || value.Milliseconds > 0) {
  380. builder.Append('T');
  381. if (value.Hours > 0)
  382. builder.Append (value.Hours).Append ('H');
  383. if (value.Minutes > 0)
  384. builder.Append (value.Minutes).Append ('M');
  385. if (value.Seconds > 0 || value.Milliseconds > 0) {
  386. builder.Append (value.Seconds);
  387. if (value.Milliseconds > 0)
  388. builder.Append ('.').AppendFormat ("{0:000}", value.Milliseconds);
  389. builder.Append ('S');
  390. }
  391. }
  392. return builder.ToString ();
  393. }
  394. public static string ToString(double value)
  395. {
  396. if (Double.IsNegativeInfinity(value)) return "-INF";
  397. if (Double.IsPositiveInfinity(value)) return "INF";
  398. if (Double.IsNaN(value)) return "NaN";
  399. return value.ToString(CultureInfo.InvariantCulture);
  400. }
  401. public static string ToString(float value)
  402. {
  403. if (Single.IsNegativeInfinity(value)) return "-INF";
  404. if (Single.IsPositiveInfinity(value)) return "INF";
  405. if (Single.IsNaN(value)) return "NaN";
  406. return value.ToString(CultureInfo.InvariantCulture);
  407. }
  408. [CLSCompliant (false)]
  409. public static string ToString(UInt32 value)
  410. {
  411. return value.ToString(CultureInfo.InvariantCulture);
  412. }
  413. [CLSCompliant (false)]
  414. public static string ToString(UInt16 value)
  415. {
  416. return value.ToString(CultureInfo.InvariantCulture);
  417. }
  418. #if NET_2_0
  419. [Obsolete]
  420. #endif
  421. public static string ToString (DateTime value)
  422. {
  423. return value.ToString ("yyyy-MM-ddTHH:mm:ss.fffffffzzz", CultureInfo.InvariantCulture);
  424. }
  425. #if NET_2_0
  426. public static string ToString (DateTime value, XmlDateTimeSerializationMode mode)
  427. {
  428. string modestr = null;
  429. switch (mode) {
  430. case XmlDateTimeSerializationMode.Local:
  431. case XmlDateTimeSerializationMode.RoundTripKind:
  432. default:
  433. return value.ToString (
  434. "yyyy-MM-ddTHH:mm:ss.fffffffzzz",
  435. CultureInfo.InvariantCulture);
  436. break;
  437. case XmlDateTimeSerializationMode.Utc:
  438. return value.ToUniversalTime ().ToString (
  439. "yyyy-MM-ddTHH:mm:ss.fffffffZ",
  440. CultureInfo.InvariantCulture);
  441. break;
  442. case XmlDateTimeSerializationMode.Unspecified:
  443. return value.ToString (
  444. "yyyy-MM-ddTHH:mm:ss.fffffff",
  445. CultureInfo.InvariantCulture);
  446. break;
  447. }
  448. }
  449. #endif
  450. public static string ToString(DateTime value, string format)
  451. {
  452. return value.ToString(format, CultureInfo.InvariantCulture);
  453. }
  454. public static TimeSpan ToTimeSpan(string s)
  455. {
  456. if (s.Length == 0)
  457. throw new ArgumentException ("Invalid format string for duration schema datatype.");
  458. int start = 0;
  459. if (s [0] == '-')
  460. start = 1;
  461. bool minusValue = (start == 1);
  462. if (s [start] != 'P')
  463. throw new ArgumentException ("Invalid format string for duration schema datatype.");
  464. start++;
  465. int parseStep = 0;
  466. int days = 0;
  467. bool isTime = false;
  468. int hours = 0;
  469. int minutes = 0;
  470. int seconds = 0;
  471. bool error = false;
  472. int i = start;
  473. while (i < s.Length) {
  474. if (s [i] == 'T') {
  475. isTime = true;
  476. parseStep = 4;
  477. i++;
  478. start = i;
  479. continue;
  480. }
  481. for (; i < s.Length; i++) {
  482. if (!Char.IsDigit (s [i]))
  483. break;
  484. }
  485. int value = int.Parse (s.Substring (start, i - start), CultureInfo.InvariantCulture);
  486. switch (s [i]) {
  487. case 'Y':
  488. days += value * 365;
  489. if (parseStep > 0)
  490. error = true;
  491. else
  492. parseStep = 1;
  493. break;
  494. case 'M':
  495. if (parseStep < 2) {
  496. days += 365 * (value / 12) + 30 * (value % 12);
  497. parseStep = 2;
  498. } else if (isTime && parseStep < 6) {
  499. minutes = value;
  500. parseStep = 6;
  501. }
  502. else
  503. error = true;
  504. break;
  505. case 'D':
  506. days += value;
  507. if (parseStep > 2)
  508. error = true;
  509. else
  510. parseStep = 3;
  511. break;
  512. case 'H':
  513. hours = value;
  514. if (!isTime || parseStep > 4)
  515. error = true;
  516. else
  517. parseStep = 5;
  518. break;
  519. case 'S':
  520. seconds = value;
  521. if (!isTime || parseStep > 6)
  522. error = true;
  523. else
  524. parseStep = 7;
  525. break;
  526. default:
  527. error = true;
  528. break;
  529. }
  530. if (error)
  531. break;
  532. ++i;
  533. start = i;
  534. }
  535. if (error)
  536. throw new ArgumentException ("Invalid format string for duration schema datatype.");
  537. TimeSpan ts = new TimeSpan (days, hours, minutes, seconds);
  538. return minusValue ? -ts : ts;
  539. }
  540. [CLSCompliant (false)]
  541. public static UInt16 ToUInt16(string s)
  542. {
  543. return UInt16.Parse(s, CultureInfo.InvariantCulture);
  544. }
  545. [CLSCompliant (false)]
  546. public static UInt32 ToUInt32(string s)
  547. {
  548. return UInt32.Parse(s, CultureInfo.InvariantCulture);
  549. }
  550. [CLSCompliant (false)]
  551. public static UInt64 ToUInt64(string s)
  552. {
  553. return UInt64.Parse(s, CultureInfo.InvariantCulture);
  554. }
  555. public static string VerifyName (string name)
  556. {
  557. if (name == null)
  558. throw new ArgumentNullException("name");
  559. if (!XmlChar.IsName (name))
  560. throw new XmlException("'" + name + "' is not a valid XML Name");
  561. return name;
  562. }
  563. public static string VerifyNCName (string ncname)
  564. {
  565. if (ncname == null)
  566. throw new ArgumentNullException("ncname");
  567. if (!XmlChar.IsNCName (ncname))
  568. throw new XmlException ("'" + ncname + "' is not a valid XML NCName");
  569. return ncname;
  570. }
  571. #if NET_2_0
  572. public static string VerifyNMTOKEN (string name)
  573. #else
  574. internal static string VerifyNMTOKEN (string name)
  575. #endif
  576. {
  577. if (name == null)
  578. throw new ArgumentNullException("name");
  579. if (!XmlChar.IsNmToken (name))
  580. throw new XmlException("'" + name + "' is not a valid XML NMTOKEN");
  581. return name;
  582. }
  583. // It is documented as public method, but in fact it is not.
  584. internal static byte [] FromBinHexString (string s)
  585. {
  586. char [] chars = s.ToCharArray ();
  587. byte [] bytes = new byte [chars.Length / 2 + chars.Length % 2];
  588. FromBinHexString (chars, 0, chars.Length, bytes);
  589. return bytes;
  590. }
  591. internal static int FromBinHexString (char [] chars, int offset, int charLength, byte [] buffer)
  592. {
  593. int bufIndex = offset;
  594. for (int i = 0; i < charLength - 1; i += 2) {
  595. buffer [bufIndex] = (chars [i] > '9' ?
  596. (byte) (chars [i] - 'A' + 10) :
  597. (byte) (chars [i] - '0'));
  598. buffer [bufIndex] <<= 4;
  599. buffer [bufIndex] += chars [i + 1] > '9' ?
  600. (byte) (chars [i + 1] - 'A' + 10) :
  601. (byte) (chars [i + 1] - '0');
  602. bufIndex++;
  603. }
  604. if (charLength %2 != 0)
  605. buffer [bufIndex++] = (byte)
  606. ((chars [charLength - 1] > '9' ?
  607. (byte) (chars [charLength - 1] - 'A' + 10) :
  608. (byte) (chars [charLength - 1] - '0'))
  609. << 4);
  610. return bufIndex - offset;
  611. }
  612. }
  613. }