|
From: <wsh...@us...> - 2003-08-08 14:30:04
|
Update of /cvsroot/emc/rcslib/src/java/rcs/utils
In directory sc8-pr-cvs1:/tmp/cvs-serv2022
Added Files:
Tag: wps_multiplat_dev_branch
StrToInt.java
Log Message:
.
--- NEW FILE: StrToInt.java ---
package rcs.utils;
import java.util.*;
/*
*
* StrToInt
*
*/
public class StrToInt
{
public static int convert(String str) throws NumberFormatException
{
int multiplier = 1;
StringTokenizer tokenizer = new StringTokenizer(str," \t\r\n\b:;[]()+");
while(tokenizer.hasMoreTokens())
{
multiplier = 1;
String token = tokenizer.nextToken();
if(null == token)
{
throw new NumberFormatException(str);
}
if(token.startsWith("-"))
{
multiplier = -1;
token = token.substring(1);
}
int point_index = token.indexOf(".");
if(point_index > 0)
{
token = token.substring(0,point_index);
} else if(point_index == 0)
{
return 0;
}
try
{
if(token.startsWith("0x"))
{
return multiplier * Integer.parseInt(token.substring(2), 16);
}
else if(token.startsWith("0") && !token.equals("0"))
{
return multiplier * Integer.parseInt(token.substring(1), 8);
}
else
{
return multiplier * Integer.parseInt(token);
}
}
catch(NumberFormatException e)
{
continue;
}
}
throw new NumberFormatException(str);
}
public static void main(String args[])
{
System.out.println("convert(3.14) = "+convert("3.14"));
System.out.println("convert(((NMLTYPE) 1006)) = "+convert("((NMLTYPE) 1006)"));
System.out.println("convert((32)) = "+convert("(32)"));
System.out.println("convert((0x100)) = "+convert("(0x100)"));
}
}
|