×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Regular Contributor
    Join Date
    Aug 2004
    Posts
    59

    Smile Converting an image to hexadecimal

    Hi ! Everybody ! does anybody know how to convert image files like *.gif to hexadecimal. I want to convert my mobile content to hexadecimal please help me. Thankyou.

    Kind Regards,
    Anthony.

  2. #2
    Regular Contributor
    Join Date
    Aug 2003
    Location
    uk
    Posts
    232
    Assuming what you want is a list bytes that you could paste into your code ..

    A quick C program that will do it:
    Code:
    // FileName=hex.c
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
    	int		i;
    	FILE	*fp;
    	int		c;
    	int		byte_count;
    	char	*sName;
    
    	if (argc == 1)
    	{
    		printf("Usage: %s <file list>\n", argv[0]);
    		return(-1);
    	}
    
    	for(i=1; i<argc; i++)
    	{
    		sName = argv[i];
    
    		printf("FILE=[%s]\n", sName);
    
    		fp = fopen(argv[i], "rb");
    
    		if (!fp)
    		{
    			printf("->ERROR: [%s] does NOT exist\n", sName);
    			continue;
    		}
    
    		byte_count=0;
    		while( (c=getc(fp)) != EOF )
    		{
    			byte_count++;
    			printf("0x%02X, ", c);
    
    			if ((byte_count%8) == 0)
    				printf("\n");
    		}
    
    		fclose(fp);
    	}
    
    	return(0);
    }
    Compile by doing: gcc hex.c -o hex.exe

    Alternatively if you want a perl script (and this is somewhat more complicated)

    Code:
    #!/usr/bin/perl
    
    # A rubbish binary file viewer by Alex Crowther - 13 December 2004
    
    # options:
    # --show_ascii	  => shows ascii as well on the right hand side.
    # --show_address  => shows the base address of each line on the left.
    # --bytes n       => line wrap after n rather than every 8 bytes.
    # --stdin
    
    $ARGC = scalar @ARGV;
    $prog = $0;
    
    sub USAGE {
      print("\nUsage: $prog [--stdin|--bytes n|--show_ascii|--show_address] FileName\n\n");
    }
    
    $bytes = 8;
    $show_ascii=0;
    $show_address=0;
    $stdin=0;
    
    sub EndOfFile
    {
    	my ($handle) = @_;
    
    	if ($stdin == 0)
    	{
    		return(eof(hFP));
    	}
    
    	# $stdin == 1
    
    	return(0);
    }
    
    # I am too lazy to work out what the built in package to do this is :P
    sub ProcessArgs
    {
    	for($i=0; $i < $ARGC; $i++)
    	{
    		$v = $ARGV[$i];
    		if (substr($v,0,2) eq "--")
    		{
    			$par = substr($v,2);
    			$optind = $ARGV[$i + 1];
    
    			if ($par eq "help")
    			{
    				&USAGE();
    				exit(0);
    			}
    
    			if ($par eq "show_ascii")
    			{
    				$show_ascii=1;
    				$fi = $fi + 1;
    			}
    
    			if ($par eq "show_address")
    			{
    				$show_address=1;
    				$fi = $fi + 1;
    			}
    
    			if ($par eq "bytes")
    			{
    				$bytes = $optind; 
    				$fi = $fi + 2;
    			}
    
    			if ($par eq "stdin")
    			{
    				$stdin=1;
    				$fi = $fi + 1;
    			}
    
    
    			# todo: error checking for bad options
    
    		}
    	}
    }
    
    #######################################################################
    
    if ($ARGC == 0)
    {
    	&USAGE();
    	exit(0);
    }
    
    $fi=0;
    
    &ProcessArgs();
    
    if ($stdin == 0)
    {
    	if ($fi >= $ARGC)
    	{
    		print("Filename Missing\n");
    		&USAGE();
    		exit(0);
    	}
    
    	$file = $ARGV[$fi];
    
    	if (open(hFP, "<$file") == 0)
    	{
    	  if ( -s $file ){
    		print("Cannot Open '$file' for some reason\n");
    	  }else{
    		print("File '$file' does NOT Exist\n");
    	  }
    	  &USAGE();
    	  exit(0);
    	}
    }else{
    	$hFP=STDIN;
    	$file="STDIN";
    }
    
    binmode hFP; # convert to binary mode
    
    $lw=0;
    $bc=0;
    
    $text="";
    
    printf("\nFile=[%s]\n\n", $file);
    
    if ($show_address == 1)
    {
    	printf("+------------+");
    
    	for($i=0; $i < $bytes; $i++)
    	{
    		printf("---", $i);
    	}
    	
    	printf("-+\n");
    
    	printf("| Address:   | ");
    
    	for($i=0; $i < $bytes; $i++)
    	{
    		printf("%02X ", $i);
    	}
    	
    	printf("|\n");
    	printf("+------------+");
    
    	for($i=0; $i < $bytes; $i++)
    	{
    		printf("---", $i);
    	}
    	printf("-+");
    	
    	if ($show_ascii == 1)
    	{
    		printf("-");
    		for($i=0; $i < $bytes; $i++)
    		{
    			printf("-", $i);
    		}
    		printf("-+");
    	}
    
    	printf("\n");
    }
    
    
    if ($show_address == 1)
    {
    	printf("| 0x%08X | ", $bc);
    }
    
    
    while($stdin ? read($hFP, $c, 1) : read(hFP, $c, 1))
    {
    	printf("%02X", ord($c));
    	# printf("%s", chr(ord($c)) );
    
    	if (ord($c) >= 32) # space
    	{
    		$text = $text . chr(ord($c));
    	}
    	else
    	{
    		$text = $text . ".";
    	}
    
    	if (EndOfFile())
    	{
    		while($lw < $bytes-1)
    		{
    			printf("   ");
    			$text = $text . " ";
    			$lw++;
    			
    		}
    	}
    
    	$bc++;
    	if ($lw == $bytes - 1)
    	{
    		if ($show_ascii == 1)
    		{
    			printf(" | $text");
    			$text="";
    		}
    
    		if ($show_address == 1)
    		{
    			printf(" |");
    		}
    
    		print("\n");
    		$lw=0;
    
    		if ($show_address == 1 && !EndOfFile() )
    		{
    			printf("| 0x%08X | ", $bc);
    		}
    	}else{
    		print(" ");
    		$lw++;
    	}
    }
    
    
    
    if ($show_address == 1)
    {
    	printf("+------------+");
    
    	for($i=0; $i < $bytes; $i++)
    	{
    		printf("---", $i);
    	}
    	
    	printf("-+");
    
    	if ($show_ascii == 1)
    	{
    		printf("-");
    		for($i=0; $i < $bytes; $i++)
    		{
    			printf("-", $i);
    		}
    		printf("-+");
    	}
    
    	printf("\n\n");
    }
    
    
    if ($stdin==0)
    {
    	close(hFP);
    }
    
    exit(0);
    I imagine there are tools on the internet to do this too. Infact I imagine many hex editors will let you copy/paste from them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×