Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to resolve an octal character, say, '\44', passed on command line?

Tags:

java

At compile time this line:

System.out.println(new String("\44"));

will print ACSII char "$"

But, how to print it when "\44" is passed as command-line argument?

like image 574
istarod Avatar asked Dec 05 '25 05:12

istarod


1 Answers

You basically strip the leading \ and use Integer.parseInt(arg, 8) since octal is base 8.

public static void main(String[] args) throws Exception {
    String arg = "\\44"; // simulate command line
    String argOctal = arg.substring(1); // get rid of leading \ to get an octal integer
    int codePoint = Integer.parseInt(argOctal, 8);
    System.out.println('\44');
    System.out.println((char)codePoint);
}

prints

$
$

This assumes you ran like

java YourClass "\44"
like image 64
Sotirios Delimanolis Avatar answered Dec 07 '25 01:12

Sotirios Delimanolis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!