我知道我接近完成这个UUEncoder功能;我将使用它与嵌入式设备进行通信。但是对于我的生活,我无法弄清楚如何在末尾正确插入空填充字符。
我一直试图效仿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++;
}
}
}
一切正常,但我在末尾0x48和0x2A得到两个不需要的字符。
答案 0 :(得分:1)
你的阵列太小了。你已宣布
unsigned char b[2];
unsigned char d[3];
但是索引都超出了范围
d[3] = ((b[2] & 0x3f) + ' ');
所以你应该声明
unsigned char b[3];
unsigned char d[4];
答案 1 :(得分:1)
\n
不是null
它是换行符。如果你把
b[1] = 0;
b[2] = 0;
最后两个字节为0x20 0x20
。但是如果你像这样编写循环
// Load chars or nulls
for (i = 0; i < 3; ++i)
{
if (hexDataIndex < hexDataCharCount)
b[i] = fileData_Hex_String[hexDataIndex];
else
b[i] = 0;
hexDataIndex++;
}
答案 2 :(得分:0)
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]);
}
}