Number string and BCD code to each other C #

Source: Internet
Author: User

<summary>
The number is encoded in BCD.
</summary>
<param name= "Num8bitbyte" >the num8 bit byte.</param>
<returns></returns>
public static byte[] BYTEARRAYTOBCD (byte[] num8bitbyte)//8-bit ASCII
{
byte[] Num4bitbyte = new Byte[8];

Num4bitbyte = Bitconverter.getbytes (0XFFFFFFFFFFFFFFFF);
for (int i = 0; i<num8bitbyte.length; i++)
{
byte num =convert.tobyte (Num8bitbyte[i]-0x30);
if (i% 2 = 0)
{
NUM4BITBYTE[I/2] = Convert.tobyte ((NUM4BITBYTE[I/2] & 0xF0) | num);
}
Else
{
NUM4BITBYTE[I/2] = Convert.tobyte ((NUM4BITBYTE[I/2) &0x0f) | (Num << 4));
}

}

return num4bitbyte;
}

<summary>
BCDs to String.
</summary>
<param name= "Bcdnum" >the BCD num.</param>
<returns></returns>
public static string bcdtostring (byte[] bcdnum)
{
String Retstring= "";
byte[] Bytechar = new Byte[bcdnum.length];
Bytechar = Bitconverter.getbytes (0XFFFFFFFFFFFFFFFF);
BYTE temphigh=0,templow=0;
int i = 0;
while (Temphigh!= 0x0f && templow!= 0xF0)
{
Temphigh = Convert.tobyte (Bcdnum[i] & 0xF0);//remove high four bits;
Temphigh = Convert.tobyte (Temphigh >> 4);
Templow = Convert.tobyte (Bcdnum[i] & 0x0f) << 4);
Bytechar[i] = convert.tobyte (Templow | temphigh);
i++;
}
String[] Hexstring=bitconverter.tostring (Bytechar). Trim (). Split ('-');
foreach (String str in hexstring)
{
retstring = str. Trim ();
}
int lastindex = Retstring.indexof (' F ');
return retstring = retstring.substring (0, lastindex);
}

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.