Monday, December 12, 2011

Java: Get the class name of any Object

Using the getClass() method of any object, you can easily obtain the name of the class of that Object. Below is a simple code snippet to clear the things.


TestClass t = new TestClass();
System.out.println(t.getClass().getName());
// output: TestClass




Tuesday, November 29, 2011

Java: Converting a byte array into Hexadecimal String

I have written a method named byteArrayToHexString() which takes a byte array as parameter, and returns the Hexadecimal representation of the byte array. Below is the method.


public static String byteArrayToHexString(byte []input){
String string = "";
if(input == null){
return string;
}

for(byte b : input){
string += Integer.toHexString(Integer.parseInt(Byte.toString(b))).toUpperCase();
}

return string;
}




Tuesday, October 25, 2011

javax.ejb.EJBException: Could not unmarshal method ID

If you are seeing the following error from your EJB then the solution is really simple.

javax.ejb.EJBException: Could not unmarshal method ID; nested exception is:
java.rmi.UnmarshalException: Method not found: 'YOUR_METHOD';


This is because you have written some code but didn't compile or redeployed to your server after compile. So, build/compile your project again and deploy to your server. You'll not see this error again. :-)




Thursday, September 29, 2011

Java: Contains() method for String class

If you need to check whether a string contains inside another string or not, you can do this using the indexOf() method from String class. I didn't find the contains function or something similar to that from String class which will take String as parameter for checking.

Below is the code that you can use like contains(String string);


String string = "My Test String...";
String contains = "Test";
if(string.indexOf(contains) >= 0){
System.out.println("["+contains+"] is inside ["+string+"]");
}


Basically indexOf function returns the position of the string from searching string. An index always starts from 0 as we know.




Sunday, September 25, 2011

C#: Encode String into base64

Below is the c# code that will convert the provided string into it's corresponding base64 string. This code will first convert the string into byte array, after that it will convert the array into base64 string.


String stringData = "hello world";
Byte[] byteData = Encoding.ASCII.GetBytes(stringData);
stringData = Convert.ToBase64String(byteData);
Console.WriteLine(stringData);




C#: Decode base64 String

Last time I had an example of Encoding of string into base64. This time Decoding.

Below is the c# code block that will convert the provided base64 string into readable one. This c# code will first decode the base64 string into byte array. After that it will convert that byte array into String data.


// "hello world"
String stringData = "aGVsbG8gd29ybGQ=";
Byte[] byteData = Convert.FromBase64String(stringData);
stringData = Encoding.ASCII.GetString(byteData);
Console.WriteLine(stringData);




Find last few characters from a string

Below is the code that uses substr() function that will return the last three characters from the string. You can change that -3 and 3 parameters according to your need.

First param -3 means start from index 3 from last. And second param 3 means the three characters.


<html><body>

<script type="text/javascript">
var string = "Test String";
string = string.substr(-3, 3);
document.write(string);
</script>

</body></html>


Pretty simple code...