There are many reasons a programmer will need to manipulate bytes. Lets
say you want to write a program that saves a file using the current
date and time for the file name. You decide to use asctime because it gives
you the time format you want.
The Time function is a standard c library :)
example :
Thu Jul 19 19:13:51 2007.txt
but there is a problem, the colon between the hour and minutes will cause an illegal file
name error so we need to change it to dashes.
Thu Jul 19 19-13-51 2007 .txt
A legal file name :)
This can be done with a substitution table
void subChar( char *pString )
{
int i;
for ( i = 0; pString[ i ] != 0; i++ )
{
//if there is an 'a'
if ( pString[i] == 'a' )
{
//replace it with a 'y'
pString[i] = 'y';
}
//if there is a 't'
else if ( pString[i] == 't' )
{
//replace it with a 'm'
pString[i] = 'm';
}
//and so on
//and so on
}
return ;
}
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <time.h>
//function to remove colon
void subChar( char *pString )
{
int i;
for ( i = 0; pString[ i ] != 0; i++ )
{
//if there is a colon
if ( pString[ i ] == ':' )
{
//replace it with a dash
pString[ i ] = '-';
}
else if ( pString[ i ] == '\n' )
{
pString[ i ] = ' ';
}
}
return ;
}
void main( void )
{
char string[ 128 ];
char filename[ 128 ];
time_t cur;
time( &cur );
//get the current time and copy to string
//header time.h required
strcpy( string, asctime( localtime(&cur) ) );
//call out function to remove ": and \n"
subChar( string );
//take our new string and add.txt to it
sprintf( filename, "%s.txt", string );
//print out our new filename
printf( "%s", filename );
getch( );
}
__________________________________________________________
Another example of manipulating bytes would be if you wanted to write an encryption
program to safe guard your data. A substitution table can play a very important role
in writing an encryption program, but by itself would not be at all secure.
When you have a character array like,
char name[] = "Dave";
it puts 'D', 'a', 'v', 'e', into each memory address. These can be read
back as any value, because it's just number values in memory.
Letters are stored as number values in the memory addresses. They are
stored using the ascii table. If you look the ascii table up in the internet
you can find tons of information about it. Basically the number 65 means 'A',
and it goes up from there. Here is the ascii table
http://www.neurophys.wisc.edu/www/comp/docs/ascii.html
On the left hand side it shows the decimal number, on the right hand side
it shows the ascii value of it. If you look at decimal number 65 you'll
see the ascii value of 'A'. So if you do printf( "ascii value of %d is %c", 65, 65 );
it will print "ascii value of 65 is A". We are telling printf to print out the number
65 and then number 65 represents in the ascii table.
For example, you can do
name[0] = name[0] + 3;
and it will take the number value in the name[0] memory address (68) and
add 3 to it. The ascii representation of 'D' is 68. It will add 3 to 68
and make it 71. The next time you print out name[0] it will print out a 'G'
(the ascii value of 71).
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(void)
{
char name[] = "Dave";
name[0] = name[0] + 3;
name[1] = name[1] + 3;
name[2] = name[2] + 3;
name[3] = name[3] + 3;
printf("%c",name[0]);
printf("%c",name[1]);
printf("%c",name[2]);
printf("%c",name[3]);
getch();
}
__________________________________________________________
Same program, but using a loop
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(void)
{
int i;
char name[] = "Dave";
for (i = 0; name[i]!=0; i++)
{
printf("%c",name[i]+3);
}
getch();
}