BCD and ASCII transcoding-C language implementation

Source: Internet
Author: User

Recently because of the project needs, need to convert the BCD code to the corresponding string, the following is the C language implementation, tested good!

Reprint please indicate the source!

/*BCD and ASCII code conversion *//******************************************************************* function name:  ASC2BCD Function Description: Convert ASCII code to BCD parameter: BCD:             converted BCD Code         ASC: ASCII string to convert         len: ASCII string length to be converted return value:  uint32                               0: Success                              Other: Failure /uint32 ASC2BCD (uint8* BCD, const uint8* ASC, UInt32 len);/******************************************************************* function Name: Bcd2asc function Description: Convert BCD code to ASCII string parameter:             ASC: Converted ASCII string BCD: BCD code to be converted         len: BCD code length to convert:  uint32                               0: Success                              Other: Failure /uint32 BCD2ASC (uint8* ASC, const uint8* BCD, UInt32 Len);

#include <assert.h> #include "utils.h"//Based on look-up table for conversion between BCD and ASCII static uint8 bcd2ascii[16] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};static uint8 ascii2bcd1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};static Uint8 Ascii2bcd2[6]  = {0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};

UInt32 ASC2BCD (uint8 *bcd, const uint8 *ASC, UInt32 len) {    uint8 c = 0;    Uint8 index = 0;    Uint8 i = 0;          Len >>= 1;        for (; i < Len; i++) {        //first BCD        if (*asc >= ' A ' && *asc <= ' F ') {            index = *asc-' A ';             c  = Ascii2bcd2[index] << 4;        } else if (*asc >= ' 0 ' && *asc <= ' 9 ') {            index = *ASC-' 0 '; C12/>c  = Ascii2bcd1[index] << 4;        }        asc++;        Second BCD        if (*asc >= ' a ' && *asc <= ' F ') {            index = *asc-' a ';             C  |= Ascii2bcd2[index];        } else if (*asc >= ' 0 ' && *asc <= ' 9 ') {            index = *ASC-' 0 ';            C  |= Ascii2bcd1[index];        }        asc++;        *bcd++ = C;    }       return 0;}

UINT32BCD2ASC (uint8 *asc, const uint8 *BCD, UInt32 len) {    uint8 c = 0;    Uint8 i;    for (i = 0; i < len; i++) {        //first BCD        c = *bcd >> 4;        *asc++ = Bcd2ascii[c];        Second        C = *bcd & 0x0f;        *asc++ = Bcd2ascii[c];        bcd++;    }    return 0;}

int main (void)//test program {    const unsigned char ascii[12] = {' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C '};
   unsigned Char bcd[6];    ASC2BCD (BCD, ASCII, 0);         int i = 0;    printf ("ASCII =%s\n", ASCII);    for (; i < 6; i++) {        printf ("BCD = 0x%.2x\n", bcd[i]);    }       /*         unsigned char ascii[13] = {0};       Const unsigned char bcd[6] = {0x01, 0x23, 0x45, 0xCD, 0xEF, 0xAB};       BCD2ASC (ASCII, BCD, 6, 0);       printf ("ASCII =%s\n", ASCII);       */    return 0;}

BCD and ASCII transcoding-C language implementation

Related Article

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.