Convert String to
Hex ASCII
To convert String to Hex
ASCII, We can use format,
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();
}
}
No comments :
Post a Comment