0

我在 c 中编写了一个函数,将字节(无符号字符)BCD 字符串转换为 ASCII。请查看代码并建议一些改进。

有没有其他有效的方法可以将 BYTE BCD 转换为 ASCII。

BYTE_BCD_to_ASC(BYTE *SrcString, char *DesString)
{

        switch (((BCD *)SrcString)->l)
        {
            case 10:/*A*/
            case 11:/*B*/
            case 12:/*C*/
            case 13:/*D*/
            case 14:/*E*/
            case 15:/*F*/
                *DesString = (char)(((BCD *)SrcString)->l + '0' + 7);
                break;
            default:
                *DesString = (char)(((BCD *)SrcString)->l + '0');
                break;
        }

        switch (((BCD *)SrcString)->h)
        {
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
                *(DesString + 1) = (char)(((BCD *)SrcString)->h + '0' + 7);
                break;
            default:
                *(DesString + 1) = (char)(((BCD *)SrcString)->h + '0');
                break;
        }
    *(DesString + 2) = '\0';
}

谢谢

法哈尔

4

3 回答 3

5

我想我会做这样的事情:

void BCD2ASC(unsigned char src, char *dest) { 
    static const char outputs[] = "0123456789ABCDEF";
    *dest++ = outputs[src>>4];
    *dest++ = outputs[src&0xf];
    *dest = '\0';
}
于 2010-08-26T17:44:19.693 回答
2

尝试这个:

void BCD_To_ASCII(unsigned char bcd_value, char * p_ascii_text)
{
  //--------------------------------------------------
  // BCD contains digits 0 .. 9 in the binary nibbles
  //--------------------------------------------------
  *p_ascii_text++ = (bcd_value >> 4)  + '0';
  *p_ascii_text++ = (bcd_value & 0x0f) + '0';
  *p_ascii_text = '\0';
  return;
}
于 2010-08-26T20:21:38.133 回答
1

最后我实现了如下例程

static const char s_szHexAscNo[] = "0123456789ABCDEF";

void BYTE_BCD_to_ASC(BYTE *SrcString, char DesString[3], int BCDflag)
{
    assert(SrcString != NULL);
    assert(DesString != NULL);

    if ( BCDFlag_Normal == BCDflag)
    {
        *DesString++ = (char) s_szHexAscNo[((BCD *)SrcString)->l];
        *DesString++ = (char) s_szHexAscNo[((BCD *)SrcString)->h];
    }

    if (BCDFlag_Reverse == BCDflag)
    {
        *DesString++ = (char) s_szHexAscNo[((BCD_REVERSE *)SrcString)->l];
        *DesString++ = (char) s_szHexAscNo[((BCD_REVERSE *)SrcString)->h];
    }
    *DesString = '\0';
}

/* BCD and BCD_REVERSE  are defined as */

typedef struct
{
unsigned char    l : 4;
unsigned char    h : 4;
} BCD;

typedef struct
{
unsigned char    h : 4;
unsigned char    l : 4;
} BCD_REVERSE;

评论将不胜感激......

于 2010-09-01T03:41:21.060 回答