Wednesday, May 27, 2009

Convert String to character array

This example shows how to do a String to character array conversion. The String class has a neat method for doing this, toCharArray().
In the code example below the String is converted to a char array and each element of the array is printed out.
 
 
/**
* Main.java
*
* @author chitnan patel
*/
public class Main {
/**
* Converts a String to a character array
*/
public void convertStringToCharArray() {
String str = "Abcdefg";
char[] cArray = str.toCharArray();
for (char c : cArray)
System.out.println(c);
}
/**
* Starts the program
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().convertStringToCharArray();
}
}
The output from the executed code:
 
A
b
c
d
e
f
g

Character array to String conversion

This java code example shows how to convert an array of characters to a String.
To accomplish this we can utilize one of the String class many contructors, which takes an array of characters as parameter.
 
/**
* Main.java
*
* @author chintan patel
*/
public class Main {
/**
* Converts an array of characters to a String
*/
public void convertCharArrayToString() {
char[] charArray = new char[] {'a', 'b', 'c'};
String str = new String(charArray);
System.out.println(str);
}
/**
* Starts the program
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().convertCharArrayToString();
}
}

Convert byte to String

This example shows three ways of converting a variable of datatype byte to a String.
The first example uses the wrapper class Byte and it's static method toString to convert the byte.
The second example just concatenates the byte with an empty String, and the value is automatically casted to a String.
The final example creates a byte array which is passed to the constructor of the String class.
The output of the code below is:
65
65
A
 
 
 
/**
*
* @author chintan patel
*/
public class Main {
/**
* Example method for converting a byte to a String.
*/
public void convertByteToString() {
byte b = 65;
//Using the static toString method of the Byte class
System.out.println(Byte.toString(b));
//Using simple concatenation with an empty String
System.out.println(b + "");
//Creating a byte array and passing it to the String constructor
System.out.println(new String(new byte[] {b}));
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().convertByteToString();
}
}

Convert Boolean to String

To convert a boolean value to a String you need to use the wrapper class of the boolean datatype.
It has the same name as the datatype boolean, but it begins with the captial B - Boolean.
To convert a boolean value you create an instance of the wrapper class sending the boolean value as argument to the constructor.
Then you simply call the toString() method which will return a String representation of the boolean value.
 
/**
* Main.java
*
* @author chintan patel
*/
public class Main {
/**
* Boolean to String conversion
*/
public void convertBooleanToString() {
boolean theValue = true;
//Do the boolean to String conversion
String theValueAsString = new Boolean(theValue).toString();
System.out.println(theValueAsString);
}
/**
* Starts the program
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().convertBooleanToString();
}
}

Convert String to Boolean

There's actually two ways to do a string to boolean conversion. Either you can create an instance of the wrapper class Boolean with a string value as argument to the constructor and call the method booleanValue(), or you can do as in the example below:
call the static method parseBoolean() of the wrapper class to convert the string value to a boolean.
Either way, the string value sent in has to be either 'true' or 'TRUE' to result in a boolean with the value of true, but it doesn't have to be 'false' to result in a false boolean value.
It actually makes sense that any other string value than 'true' or 'TRUE' will result in a return value that is set to false.
 
 
/**
* Main.java
*
* @author chintan patel
*/
public class Main {
/**
* String to boolean conversion
*/
public void convertStringToBoolean() {
String strBoolean = "true";
//Do the String to boolean conversion
boolean theValue = Boolean.parseBoolean(strBoolean);
System.out.println(theValue);
}
/**
* Starts the program
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().convertStringToBoolean();
}
}

Convert String to byte array

This little code example shows a String to byte array conversion.
The String class has a method called getBytes which returns an array of bytes.
In the example the length of the array is printed out to illustrate that the length of the array is the same as the number of characters in the String.
/**
*
* @author chintan patel
*/
public class Main {
/*
* This method converts a String to an array of bytes
*/
public void convertStringToByteArray() {
String stringToConvert = "This String is 76 characters long and will be converted to an array of bytes";
byte[] theByteArray = stringToConvert.getBytes();
System.out.println(theByteArray.length);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().convertStringToByteArray();
}
}

Convert byte[ ] array to String

Making a byte array to String conversion in Java is very simple since one of the String class constructors takes an array of bytes as argument.
We simply create the object with our array as argument to convert the byte array to a String, and then print out the value.
/*
* Main.java
*
* @author chintan patel
*/
public class Main {
/*
* This method converts an byte array to a String object.
*/
public void convertByteArrayToString() {
byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};
String value = new String(byteArray);
System.out.println(value);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().convertByteArrayToString();
}
}

Tuesday, May 26, 2009

How To Embeded Flash in Java Desktop App

Most of examples about playing Flash animation on desktop application I found on the internet to are done by VB. But today I found java version. Flash SWT Plugin, it work very well on my latest project :).

To use Flash SWT Plugin, download com.docuverse.swt.flash-0.4.zip and extract it somewhere on local disk. Now our eclipse flash project need to add atleast two external jars e.g. flash.jar[from where we just extract the zip] and swt.jar and now we are ready to code.

THE SOURCE
To play swf file, all we need is FlashPlayer object and activate it that all. it'll look like this.




import com.docuverse.swt.flash.FlashPlayer;

public class SWTPlayer {
....
private void run() {
// TODO Auto-generated method stub
Display display = new Display();
Shell shell = new Shell(display);
FlashPlayer player = new FlashPlayer(shell);
player.loadMovie(0, "yourflashfile.swf");
player.setSize(550, 400);
player.activate();
shell.setSize(550, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

How To: Test Your Antivirus

How To: Test Your Antivirus


Have you ever wondered if your anti virus is working properly? I haven’t had a virus alarm for a while now so I started doubting if my antivirus
was working properly or was just hogging my system memory and wasting my time download updates and definitions for nothing.So I decided to find a way to test it, but how could I test my antivirus without risking damaging my system? Why creating my own little virus of course.I’ll show you how to make a virus using notepad that will test the ability and efficiency of your current antivirus without causing any damage to your computer even if you don’t have an anti virus nothing will happen.Lets get to it:


1) Open a new notepad document

2) Type the code below into the notepad file.

X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


3) Save the notepad file as FakeVirus.com or whatever you want, To comfortably find the file, I suggest placing it in the Desktop.To save a file in .com format all you need to do is change the “Save as type:” to All Files and then just enter FakeVirus.com in the “File name:” section and click Save.

4) Run the FakeVirus.com file to test your computers efficiency. If your anti virus software
doesn’t give you a warning or alert, I suggest changing your AntiVirus immediately as your current one doesn’t work properly.Check out this article for a few good free antivirus softwares.

5) After your done with the file just delete it and empty the trash, remember this fake virus won’t affect your computer in any way.

Enjoy.

Monday, May 25, 2009

Dos Commands

Let's take a look at some important DOS commands available in Windows XP. These commands/tools must be run from the command prompt of the cmd.exe command interpreter.

To run cmd.exe, click Start, click Run, type cmd and click OK. You will then see a black window similar to the one above.

Tip: Wherever you have to type the path to a folder after the command, just drag the file or folder from Windows Explorer into the command window instead of typing the whole path.

Assoc: Displays or modifies file name extension associations. Used without parameters, assoc displays a list of all the current file name extension associations.

At: Schedules commands and programs to run on a computer at a specified time and date. You can use at only when the Schedule service is running. Used without parameters, at lists scheduled commands.

Attrib: Displays, sets, or removes the read-only, archive, system, and hidden attributes assigned to files or directories. Used without parameters, attrib displays attributes of all files in the current directory.

Comp: Compares the contents of two files or sets of files byte by byte. Comp can compare files on the same drive or on different drives, and in the same directory or in different directories. When comp compares the files, it displays their locations and file names. Used without parameters, comp prompts you to enter the files to compare.

Copy: Copies one or more files from one location to another.

Cprofile: Cleans specified profiles of wasted space and, if user-specific file associations are disabled, removes these associations from the registry. Profiles that are currently in use are not modified.

Defrag: Locates and consolidates fragmented boot files, data files, and folders on local volumes.

Driverquery: Displays a list of all installed device drivers and their properties.

Fc: Compares two files and displays the differences between them. Fc can even resynchronize files if there is a mismatch.

Find: Searches for a specific string of text in a file or files. After searching the specified file or files, find displays any lines of text that contain the specified string.

Getmac: Returns the media access control (MAC) address and list of network protocols associated with each address for all network cards in each computer, either locally or across a network.

Help: Provides online information about system commands (that is, non-network commands). Used without parameters, help lists and briefly describes every system command.

Helpctr: Starts Help and Support Center. Used without parameters, helpctr displays the Help and Support Center home page.

Hostname: Displays the host name portion of the full computer name of the computer.

Ipconfig: Displays all current TCP/IP network configuration values and refreshes Dynamic Host Configuration Protocol (DHCP) and Domain Name System (DNS) settings. Used without parameters, ipconfig displays the IP address, subnet mask, and default gateway for all adapters.

Irftp: Sends files over an infrared link. Used without parameters or used with /s, irftp opens the Wireless Link dialog box, where you can select the files that you want to send without using the command line.

Lpq: Displays the status of a print queue on a computer running Line Printer Daemon (LPD). Used without parameters, lpq displays command-line help for the lpq command.

Mmc: Opens Microsoft Management Console (MMC). Using the mmc command-line options, you can open a specific MMC console, open MMC in author mode, or specify that the 32-bit or 64-bit version of MMC is opened.

More: Generally used along with other commands, displays one screen of output at a time.

Move: More convenient than copy-paste. Moves one or more files from one directory to the specified directory. For example, you have some Excel files having their names start with "MonthlyReport" in your My Documents folder which you want to move to a separate folder. Using the Windows XP Explorer, you would have to manually select every single file and then use cut-paste. Using the move command to perform the same task is much simpler and easier. Type the following command and hit enter:
move MonthlyReport*.xls c:\MonthlyReports\

Msinfo32: Displays a comprehensive view of your hardware, system components, and software environment.

Netstat: This command is very useful to diagnose slow internet connections. It displays active TCP connections, ports on which the computer is listening, Ethernet statistics, the IP routing table, IPv4 statistics (for the IP, ICMP, TCP, and UDP protocols), and IPv6 statistics (for the IPv6, ICMPv6, TCP over IPv6, and UDP over IPv6 protocols). Used without parameters, netstat displays active TCP connections.

Openfiles: Queries or displays open files. Also queries, displays, or disconnects files opened by network users.

Ping: Verifies IP-level connectivity to another TCP/IP computer by sending Internet Control Message Protocol (ICMP) Echo Request messages. The receipt of corresponding Echo Reply messages are displayed, along with round-trip times. Ping is the primary TCP/IP command used to troubleshoot connectivity, reachability, and name resolution. Used without parameters, ping displays help.

Recover: Recovers readable information from a bad or defective disk.

Runas: Allows a user to run specific tools and programs with different permissions than the user's current logon provides.

Shutdown: Allows you to shut down or restart a local or remote computer. Used without parameters, shutdown will logoff the current user.

Start: Starts a separate Command Prompt window to run a specified program or command. Used without parameters, start opens a second command prompt window.

Systeminfo: Displays detailed configuration information about a computer and its operating system, including operating system configuration, security information, product ID, and hardware properties, such as RAM, disk space, and network cards.

Taskkill: Ends one or more tasks or processes. Processes can be killed by process ID or image name.

Tasklist: Displays a list of applications and services with their Process ID (PID) for all tasks running on either a local or a remote computer.

Tree: Graphically displays the directory structure of a path or of the disk in a drive.

Ver: Displays the Windows XP version number.

Vol: Displays the disk volume label and serial number, if they exist. A serial number is displayed for a disk formatted with MS-DOS version 4.0 or later.

W32tm: A tool used to diagnose problems occurring with Windows Time.

Xcopy: Copies files and directories, including subdirectories.

You can find the complete list of command prompt commands for Windows XP, Syntax, Parameters & Examples here.