首页 > 技术文章 > 将一维数组转存为两维文本数组

hbg200 2018-05-09 14:33 原文

将数据转为文本,方便观察保存,避免抄写。这里为64个数据,转为两维文本数组。

源程序如下:

DWORD __fastcall ByteToAsciiNumber(DWORD *pOutValue, BYTE byInValue)//字节数值转Ascii数值码
{
  DWORD dwRet,dwTmp,dwQuotient,dwQuotient1,dwRemainder;//因内存排列问题,商和余是交换的

  if(byInValue < 10)
    {
        *pOutValue = byInValue + 0x30;
        dwRet = 1;
    } 
    else
  if(byInValue < 100)
    {
       dwQuotient = (byInValue / 10) + 0x30;
       dwRemainder = (byInValue % 10) + 0x30;

       dwTmp = (dwRemainder << 8) & 0xFF00;
       dwTmp |= dwQuotient;

       *pOutValue = dwTmp;
       dwRet = 2;
    }
    else
    {
       dwQuotient1 = byInValue / 100;
       dwTmp = byInValue % 100;
       dwQuotient = dwTmp / 10;
       dwRemainder = dwTmp % 10;

       dwQuotient1 += 0x30;
       dwQuotient += 0x30;
       dwRemainder += 0x30;

       dwTmp = (dwRemainder << 16) & 0xFF0000;
       dwTmp |= (dwQuotient << 8);
       dwTmp |= dwQuotient1;

       *pOutValue = dwTmp;
       dwRet = 3;
    }

  return dwRet;
}

测试程序如下:

void __fastcall TForm1::SpeedButton16Click(TObject *Sender)
{
   int i,z; DWORD dwValue,dwWriteCount;

   SaveDialog1->Title = "保存TEXT文件";
   SaveDialog1->Filter = "TXT文件(*.txt)|*.txt";
   SaveDialog1->DefaultExt = String("TXT");
   if(SaveDialog1->Execute())//找到文件
     {
      if(File.Create(SaveDialog1->FileName))
        {
          z = 0;

          for(i = 0; i < 256; i++)
             {
               dwWriteCount = ByteToAsciiNumber(&dwValue, i);//产生0-255Ascii码文本
               File.Write(&dwValue, dwWriteCount);//写入
               dwValue = 0x20; //空格
               File.Write(&dwValue, 1);//写入空格
               if(z < 16)//增加回车换行
                 {
                    z++;
                 }
                 else
                 {
                    z = 0;
                    dwValue = 0x0d;//回车
                    File.Write(&dwValue, 1);//写入回车
                    dwValue = 0x0a;//换行
                    File.Write(&dwValue, 1);//写入换行
                 }
             }
        }
     }
}

测试结果如下:

实现测试:

void __fastcall TForm1::SpeedButton16Click(TObject *Sender)
{
   int i,z; DWORD dwValue,dwWriteCount;

   SaveDialog1->Title = "保存TEXT文件";
   SaveDialog1->Filter = "TXT文件(*.txt)|*.txt";
   SaveDialog1->DefaultExt = String("TXT");
   if(SaveDialog1->Execute())//找到文件
     {
      if(File.Create(SaveDialog1->FileName))
        {
          z = 0;
          dwValue = 0x7b;//{
          File.Write(&dwValue, 1);
          dwValue = 0x0d;//回车
          File.Write(&dwValue, 1);//写入回车
          dwValue = 0x0a;//换行
          File.Write(&dwValue, 1);//写入换行
          dwValue = 0x20; //空格缩进
          File.Write(&dwValue, 1);//写入空格
          dwValue = 0x7b;//{
          File.Write(&dwValue, 1);
          for(i = 0; i < 64; i++)
             {
               dwWriteCount = ByteToAsciiNumber(&dwValue, bzTable1[i]);//产生Ascii码文本
               File.Write(&dwValue, dwWriteCount);//写入
               if(z < 7)//增加回车换行
                 {
                    z++;
                    dwValue = 0x2c; //逗号
                    File.Write(&dwValue, 1);
                    dwValue = 0x20; //空格
                    File.Write(&dwValue, 1);//写入空格
                 }
                 else
                 {
                    dwValue = 0x7d;//}
                    File.Write(&dwValue, 1);
                    if(i < 63)//最后一个判断
                      {
                         dwValue = 0x2c; //逗号
                         File.Write(&dwValue, 1);
                      }
                    z = 0;
                    dwValue = 0x0d;//回车
                    File.Write(&dwValue, 1);//写入回车
                    dwValue = 0x0a;//换行
                    File.Write(&dwValue, 1);//写入换行
                    if(i < 63)//最后一个判断
                      {
                         dwValue = 0x20; //空格缩进
                         File.Write(&dwValue, 1);//写入空格
                         dwValue = 0x7b;//{
                         File.Write(&dwValue, 1);
                      }
                 }
             }
          dwValue = 0x7d;//}
          File.Write(&dwValue, 1);
          dwValue = 0x3b;//;
          File.Write(&dwValue, 1);
        }
     }
}

实现结果:

有关File请参考我的《API读写32位系统4G以上大文件》,可封装为CFile,方便调用。

 

推荐阅读