BYTE BCD 到 ASCII 转换优化

发布于 2024-09-16 18:35:47 字数 1075 浏览 8 评论 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';
}

谢谢法哈尔

I've written a function in c that converts a byte (unsigned char) BCD string into ASCII. Please have look at the code and advice some improvements.

Is there any other efficient way that can convert BYTE BCD to 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';
}

thanks

Fakhar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

所有深爱都是秘密 2024-09-23 18:35:47

我想我会做这样的事情:

void BCD2ASC(unsigned char src, char *dest) { 
    static const char outputs[] = "0123456789ABCDEF";
    *dest++ = outputs[src>>4];
    *dest++ = outputs[src&0xf];
    *dest = '\0';
}

I think I'd do something like this:

void BCD2ASC(unsigned char src, char *dest) { 
    static const char outputs[] = "0123456789ABCDEF";
    *dest++ = outputs[src>>4];
    *dest++ = outputs[src&0xf];
    *dest = '\0';
}
海夕 2024-09-23 18:35:47

试试这个:

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;
}

Try this:

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;
}
想你只要分分秒秒 2024-09-23 18:35:47

最后我已经实现了例程,如下

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;

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

finally I have implemented the routine as follows

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;

comments would be appreciated....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文