• 0
Sign in to follow this  
cgm2k7

Help c++Builder hem em um memo

Question

Olá pessoal mais uma vez eu aqui pedindo ajuda..
É o seguinte estou desenvolvendo um aplicativo e preciso visualizar os arquivo que usuário está editando em Hex e ASCII, assim com na imagem veja abaixo:
Sem_t_tulo.png

Exatamente como na imagem só que em um memo ou qualquer outro componente. Achei este código na net mas não estou conseguido adapta-lo para abrir em um memo. Se alguém pude me ajudar... Agradeço.

# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <fstream>
# include <ctime>
	using namespace std;
	int main ( long argc, char *argv[] );
void handle ( char file_in_name[] );
void timestamp ( void );
	int main ( long argc, char *argv[] )
{
  char file_in_name[80];
  int i;
  bool VERBOSE = true;
	  if ( VERBOSE )
  {
    timestamp ( );
	    cout << "\n";
    cout << "HEXDUMP:\n";
    cout << "  C++ version\n";
    cout << "\n";
    cout << "  Compiled on " << __DATE__ << " at " << __TIME__ << ".\n";
    cout << "\n";
    cout << "  Produce a hexadecimal dump of a file.\n";
  }
//
//  If the input file was not specified, get it now.
//
  if ( argc <= 1 )
  {
    cout << "\n";
    cout << "HEXDUMP:\n";
    cout << "  Please enter the name of a file to be analyzed.\n";
	    cin.getline ( file_in_name, sizeof ( file_in_name ) );
	    handle ( file_in_name );
  }
//
//  Otherwise, get the file(s) from the argument list.
//
  else
  {
    for ( i = 1 ; i < argc ; ++i )
    {
      handle ( argv[i] );
    }
  }
	  if ( VERBOSE )
  {
    cout << "\n";
    cout << "HEXDUMP:\n";
    cout << "  Normal end of execution.\n";
	    cout << "\n";
    timestamp ( );
  }
  system("pause");
  return 0;
}
	void handle ( char file_in_name[] )
{
  long int addr;
  unsigned char buffer[20];
  long int cnt;
  long int cnt2;
  ifstream file_in;
  long n;
//
//  Open the file.
//
  file_in.open ( file_in_name );
	  if ( !file_in )
  {
    cout << "\n";
    cout << "HANDLE - Fatal error!\n";
    cout << "  Cannot open \"" << file_in_name << "\"\n";
    return;
  }
     
  cout << "\n";
  cout << "Hexdump of \"" << file_in_name << "\":\n";
  cout << "\n";
  cout <<
    "Address               Hexadecimal values                  Printable\n";
  cout <<
    "-------  -----------------------------------------------  -------------\n";
  cout << "\n";
//
//  Dump the file contents.
//
  addr = 0;
	  while ( 1 )
  {
    file_in.read ( ( char * ) buffer, 16 );
	    cnt = file_in.gcount();
	    if ( cnt <= 0 )
    {
      break;
    }
//
//  Print the address in decimal and hexadecimal.
//
    cout << setw(7) << ( int ) addr << "  ";
	    addr = addr + 16;
//
//  Print 16 data items, in pairs, in hexadecimal.
//
    cnt2 = 0;
    for ( n = 0; n < 16; n++ )
    {   
      cnt2 = cnt2 + 1;
      if ( cnt2 <= cnt )
      {
        cout << hex << setw(2) << setfill ( '0' ) << ( int ) buffer[n];
      }
      else
      {
        cout << "  ";
      }
      cout << " ";
    }
	    cout << setfill ( ' ' );
//
//  Print the printable characters, or a period if unprintable.
//
    cout << " ";
    cnt2 = 0;
    for ( n = 0; n < 16; n++ )
    {
      cnt2 = cnt2 + 1;
      if ( cnt2 <= cnt )
      {
        if ( buffer[n] < 32 || 126 < buffer[n] )
        {
          cout << '.';
        }
        else
        {
          cout << buffer[n];
        }
      }
    }
    cout << "\n";
    cout << dec;
	    if ( file_in.eof ( ) )
    {
      break;
    }
	  }
  file_in.close ( );
	  return;
}
	void timestamp ( void )
{
# define TIME_SIZE 40
	  static char time_buffer[TIME_SIZE];
  const struct tm *tm;
  size_t len;
  time_t now;
	  now = time ( NULL );
  tm = localtime ( &now );
	  len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
	  cout << time_buffer << "\n";
  return;
# undef TIME_SIZE
}

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this