February 2006 - Posts

Batchfile chat program

In the company I'm currently working, sending messages using "Net Send" in command prompt is the only way we can communicate in the LAN, everything else is blocked by the System Administrator. I thought that there should be a better way of typing "Net Send" over and over again. For this purpose, I wrote a simple batch file that would automate calling "Net Send" and "Net View", which can remember the last user you're sending message to, and other small things. For those who may want this, here's the source-script of the batchfile:

@echo off
echo Welcome to NETCHAT version 1.00
echo Created by Chris Vega
echo.
echo type ? in Message to get help

:setuser
echo.
set /p usr=User:
if %usr%==chuser goto setuser
if %usr%==list goto listusers_u
if %usr%==clear goto clearscreen_u
if %usr%==? goto showhelp_u
if %usr%==quit goto quithere

:setmsg
set /p msg=Message:

if %msg%==chuser goto setuser
if %msg%==list goto listusers
if %msg%==clear goto clearscreen
if %msg%==? goto showhelp
if %msg%==quit goto quithere
@net send %usr% %msg%

goto setmsg

:showhelp
echo.
echo [Available commands]
echo.
echo ?          - Display this help
echo chuser     - Change the user you are sending message to
echo list       - Get the list of available users
echo clear      - Clear the screen
echo quit       - Exit
echo.
echo.
goto setmsg

:listusers
net view
goto setmsg

:clearscreen
cls
goto setmsg

:showhelp_u
echo.
echo [Available commands]
echo.
echo ?          - Display this help
echo chuser     - Change the user you are sending message to
echo list       - Get the list of available users
echo clear      - Clear the screen
echo quit       - Exit
echo.
echo.
goto setuser

:listusers_u
net view
goto setuser

:clearscreen_u
cls
goto setuser

:quithere

Copy all the BLUE text above into notepad and save it to netchat.bat, run it, and enjoy chatting with "Net Send" using this batchfile. The script will keep looping until you typed "quit", thus simulating a very simple command interpreter based chatting program.

Attached is netchat.txt, you can download it as an alternative method above, then rename it to netchat.bat.

-chris

Posted by cvega with 1 comment(s)
Filed under:

Swap two integer values without using a temporary variable

The traditional programming practice when switching content between two integer variables is to use another temporary variable to hold data, then assigning data to and from the switching variables, forming a triangle of data movement.

var
     a :integer
     b :integer
     temp:integer

algo (

  temp = a
  a = b
  b = temp

)



However, did you know that in old school there is a better and more neat way of doing this, using the miracles of XOR?

Let's see the traditional way (code is C#):

int a = 1001;
int b = 2002;

// Switching means you need to declare extra temp variable
// to hold the first value, so you won't loose it for later

int temp = a;

// And goes the switch
a = b;
b = temp;

// a contains 2002
// b contains 1001


There's nothing wrong with above code, in fact it is what's been teaching in all of the programming books. Even bubble sort algorithm uses that type of value switching.

Now let's play with XOR (the operator used in XOR in C# is ^ character)

int a = 1001;
int b = 2002;

// First you XOR b to a
a ^= b;   // a = 1083 (seed value)

// Next, XOR the result of first XOR to b
b ^= a;

// Finally, XOR b to a again (b contains the old value of a)
a ^= b;

// a contains 2002
// b contains 1001


Pretty cool isn't?

So how did it worked? I will leave that to you to figure out [:P]
The explanation is pretty simple though.

Cheers,

-chris

Posted by cvega with 9 comment(s)
Filed under: