C # 16 binary and BCD code conversion codes

Source: Internet
Author: User

[CSharp]View PlainCopy
  1. Private Static byte[] ConvertFrom (string strtemp)
  2. {
  3. Try
  4. {
  5. if (Convert.toboolean (Strtemp.length & 1))//Digital binary code The last 1 bits are 1 is an odd number
  6. {
  7. strtemp = "0" + strtemp; 0 in front of odd digits
  8. }
  9. byte[] arytemp = new BYTE[STRTEMP.LENGTH/2];
  10. For (int i = 0; i < (STRTEMP.LENGTH/2); i++)
  11. {
  12. Arytemp[i] = (Byte) (((Strtemp[i * 2]- ' 0 ') << 4) |  (Strtemp[i * 2 + 1]- ' 0 '));
  13. }
  14. return arytemp; //High in front
  15. }
  16. Catch
  17. { return null;}
  18. }
  19. // <summary>
  20. ///BCD Conversion 16 binary (compressed BCD)
  21. // </summary>
  22. /// <param name= "strtemp" ></param>
  23. // <returns></returns>
  24. public Static byte[] ConvertFrom (string strtemp, int intlen)
  25. {
  26. Try
  27. {
  28. byte[] Temp = ConvertFrom (Strtemp.trim ());
  29. byte[] Return_byte = new Byte[intlen];
  30. if (Intlen! = 0)
  31. {
  32. if (Temp.length < Intlen)
  33. {
  34. For (int i = 0; i < intlen-temp.length; i++)
  35. {
  36. Return_byte[i] = 0x00;
  37. }
  38. }
  39. Array.copy (Temp, 0, Return_byte, Intlen-temp.length, temp.length);
  40. return return_byte;
  41. }
  42. Else
  43. {
  44. return Temp;
  45. }
  46. }
  47. Catch
  48. { return null;}
  49. }
  50. // <summary>
  51. ///16 Binary conversion BCD (unzip BCD)
  52. // </summary>
  53. /// <param name= "AData" ></param>
  54. // <returns></returns>
  55. public static string ConvertTo (byte[] AData)
  56. {
  57. Try
  58. {
  59. StringBuilder sb = new StringBuilder (Adata.length * 2);
  60. foreach (Byte b in AData)
  61. {
  62. Sb. Append (b >> 4);
  63. Sb. Append (b & 0x0f);
  64. }
  65. return SB.  ToString ();
  66. }
  67. Catch { return null;}
  68. }

C # 16 binary and BCD code conversion codes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.