简体   繁体   English

在C中将Null填充添加到UUEncoder

[英]Adding Null Padding to UUEncoder in C

I know I'm close to finishing this UUEncoder function; 我知道我即将完成此UUEncoder函数; I'll be using it to communicate with an embedded device. 我将使用它与嵌入式设备进行通信。 But for my life, I can't figure out how to get the null padded characters inserted properly at the end. 但是我一生都无法弄清楚如何正确插入最后的空填充字符。

I've been trying to follow the example here . 我一直在尝试遵循此处的示例。

void encode(unsigned char fileData_Hex_String[], int hexDataCharCount)
{
    unsigned char UUE_Encoded_String[MAX_SIZE];
    unsigned char b[2];
    unsigned char d[3];
    int UUE_Encoded_String_Index = 0;

    hexDataCharCount =7;

    for(int hexDataIndex = 0;  hexDataIndex < hexDataCharCount; hexDataIndex)
    {
        // Load chars or nulls
        for (int i = 0; i < 3; ++i)
        {
            b[i] = fileData_Hex_String[hexDataIndex];
            printf("%i\n", (hexDataIndex - hexDataCharCount));
            if ((hexDataIndex - hexDataCharCount) > 0)
            {
                b[1] = '\n';
                b[2] = '\n';
                break;
            }
            hexDataIndex++;
        }


        // UUEncode
        d[0] = (((b[0] >> 2) & 0x3f) + ' ');
        d[1] = ((((b[0] << 4) | ((b[1] >> 4) & 0x0f)) & 0x3f) + ' ');
        d[2] = ((((b[1] << 2) | ((b[2] >> 6) & 0x03)) & 0x3f) + ' ');
        d[3] = ((b[2] & 0x3f) + ' ');

        // Put the UUEncoded chars into their own string.
        for (int i = 0; i < 4; i++)
        {
            UUE_Encoded_String[UUE_Encoded_String_Index] = d[i];
            printf(" 0x%2x \n", UUE_Encoded_String[UUE_Encoded_String_Index]);
            UUE_Encoded_String_Index++;
        }
    }
}

Everything works fine, except I get two unwanted characters at the end 0x48 and 0x2A. 一切正常,除了在结尾0x48和0x2A处得到两个多余的字符。

Your arrays are too small. 您的数组太小。 You have declared 您已经声明

unsigned char b[2];
unsigned char d[3];

but are indexing both out of range 但索引都超出范围

d[3] = ((b[2] & 0x3f) + ' ');

so you should declare 所以你应该声明

unsigned char b[3];
unsigned char d[4];

\\n is not a null it is a newline. \\n不是null ,而是换行符。 If you put 如果你把

b[1] = 0;
b[2] = 0;

The last two bytes will be 0x20 0x20 . 最后两个字节将是0x20 0x20 But your code will be more flexible if you write the loop like this 但是,如果您这样编写循环,则代码将更加灵活

// Load chars or nulls
for (i = 0; i < 3; ++i)
{
    if (hexDataIndex < hexDataCharCount)
        b[i] = fileData_Hex_String[hexDataIndex];
    else
        b[i] = 0;
    hexDataIndex++;
}

Working code after Weather Vane helped me. Weather Vane之后的工作代码对我有所帮助。

void encode2(unsigned char fileData_Hex_String[], int hexDataCharCount)
{
    unsigned char UUE_Encoded_String[MAX_SIZE];
    unsigned char b[3];
    unsigned char d[4];

    int paddedIndex = 0;
    int UUE_Encoded_String_Index = 0;

    for(int hexDataIndex = 0;  hexDataIndex < hexDataCharCount; hexDataIndex)
    {   
        // Load chars or nulls
        for (int i = 0; i < 3; i++)
        {

            if (hexDataIndex < hexDataCharCount)
            {
                b[i] = fileData_Hex_String[hexDataIndex];   
            }
            else
            {
                b[i] = 0;
                paddedIndex++;
            }
            //printf("%i\n", (hexDataIndex-hexDataCharCount));
            hexDataIndex++;
        }

        // UUEncode
        d[0] = (((b[0] >> 2) & 0x3f) + ' ');
        d[1] = ((((b[0] << 4) | ((b[1] >> 4) & 0x0f)) & 0x3f) + ' ');
        d[2] = ((((b[1] << 2) | ((b[2] >> 6) & 0x03)) & 0x3f) + ' ');
        d[3] = ((b[2] & 0x3f) + ' ');

        // Put the UUEncoded chars into their own string.
        for (int i = 0; i < 4; i++)
        {
            UUE_Encoded_String[UUE_Encoded_String_Index] = d[i];
            UUE_Encoded_String_Index++;
        }
        //printf("      %i      %i\n", hexDataIndex, hexDataCharCount);

    }
    printf("%i\n", (UUE_Encoded_String_Index-paddedIndex));
    for (int i = 0; i < (UUE_Encoded_String_Index-paddedIndex); ++i)
    {
        printf(" 0x%2x \n", UUE_Encoded_String[i]);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM