Thursday, September 27, 2012

Java: Convert String to Hex ASCII


Convert String to Hex ASCII

To convert String to Hex ASCII, We can use format,
public static String format(String format, Object... args)

Returns a formatted string using the specified format string and arguments.
Parameters:
format - A format string
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns:
A formatted string
Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
NullPointerException - If the format is null

By using formatter class, we can convert char to hexadecimal integer.
Conversion
Argument
Category Description
'x', 'X'
integral
The result is formatted as a hexadecimal integer
'd'
integral
The result is formatted as a decimal integer  
'o'
integral
The result is formatted as an octal integer

Sample Code

public class ConvertStringToHexASCII
{
      public static void main(String[] args)
      {
            System.out.println(ConvertStringToHexASCII.convertStringToHexASCII("ABC"));
      }

      private static String convertStringToHexASCII(String value)
      {
            StringBuffer convertedValue = new StringBuffer();
            for (char c : value.toCharArray())
            {
                  convertedValue.append(String.format("0x%02x", (int)c));
                  convertedValue.append(", ");
            }
            return convertedValue.toString();
      }
}

Output: 0x41, 0x42, 0x43, 

Note: For ASCII Table Visit http://www.asciitable.com/

Wednesday, August 29, 2012

Tomcat Configuration Https without port number

Install tomcat or Unzip tomcat and run the start.bat file from APACHE_HOME\bin.
Tomcat runs on http with 8080 port default.
To test, type http://localhost:8080 in browser.

To enable HTTPS

We need to change few configuration in the APACHE_HOME \conf\server.xml file.

Uncomment the connector in APACHE_HOME \conf\server.xml file.

Create an abc.jks file using keytool and place in the ${user.home}.
Note: ${user.home} in windows “C:\Documents and Settings\user”.
Now we can access https with the following set up.
To test, type https://localhost:8443 in browser.

Remove 8443 port

Default security port is 443.
So, change the port from 8443 to 443 in the above connector as follows.

To test, type https://localhost in browser.

Thursday, July 26, 2012

IPhone: Solution for [[UIDevice currentDevice] uniqueIdentifier]]


[[UIDevice currentDevice] uniqueIdentifier] is deprecated in iOS5

APPLE DOCUMENTATION:
To create a unique identifier specific to your app, you can call the CFUUIDCreate function to create a UUID, and write it to the defaults database using the NSUserDefaults
/*
 * Generates UUID using CFUUIDCreate
 */
- (NSString *) generateUUID
{
    NSString *  result;
    CFUUIDRef   uuid;
    CFStringRef uuidStr;
    
    uuid = CFUUIDCreate(NULL);
    assert(uuid != NULL);
    
    uuidStr = CFUUIDCreateString(NULL, uuid);
    assert(uuidStr != NULL);
    
    result = [NSString stringWithFormat:@"%@", uuidStr];
    assert(result != nil);
    
    CFRelease(uuidStr);
    CFRelease(uuid);
    
    return result;
}

// Use the Below Code to save the UUID into the NSUserDefaults, so that we can use, anywhere in the application.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *fetchUUID = [defaults objectForKey:@"UUID"];
if (fetchedUUID == nil || [fetchedUUID isEqualToString:@""])
{
     NSLog(@"Checking in if");
     [defaults setObject:[self generateUUID] forKey:@"UUID"];
}    
fetchUUID = [defaults objectForKey:@"UUID"];

Use the above code to get UUID in iOS5.