📄 convert.java
字号:
package HECore.stddata;
import HPCore.Exception.*;
import java.util.*;
import HPCore.stdfunc.*;
public class convert {
/**
* Returns Vint object representation of a int .
**/
public static Variant int2var(short val)
{
return new VInt((double)val);
}
/**
* Returns VDouble object representation of a double
**/
public static Variant dbl2var(double db)
{
return new VDouble(db);
}
public static short str2int(String str) throws HpException
{
double dbl=str2dbl(str);
short result=dbl2int(dbl);
return result;
// return (short)(new VString(str).dblValue());
}
/**
* Returns VBoolean object representation of a boolean
**/
public static Variant bool2var(boolean bool)
{
return new VBoolean(bool);
}
/**
* Returns VDate object representation of a date.
**/
public static Variant date2var(HDate date)
{
return new VDate(date.dblValue());
}
/**
*Returns VCurr object representation of a currency.
**/
public static Variant curr2var(HCurr curr)
{
return new VCurr(curr.dblValue());
}
/**
* Returns VString object representation of a string
**/
public static Variant str2var(String str)
{
return new VString(str);
}
/**
* Returns VObject object representation of a object
**/
public static Variant obj2var(Object obj)
{
return new VObject(obj);
}
public static VObject var2obj(Variant obj) throws HpException
{
if(obj.getType()==Variant.V_NULL)
throw new HpException( 94," Invalid use of Null");
if(obj instanceof HECore.stddata.VObject)
return (VObject)obj;
else
{
VObject o = new VObject(obj.objValue());
return o;
}
}
public static VObject ctrl2obj(Object obj)
{
return new VObject(obj);
}
/**
* Returns VByte object representation of a short in VB .
**/
public static Variant byt2var(short sh)
{
return new VByte((double)sh);
}
/**
* Returns VLong object representation of a int
**/
public static Variant lng2var(int l)
{
return new VLong((double)l);
}
/**
* Returns VLong object representation of a float
**/
public static Variant sgl2var(float f)
{
return new VSingle((double)f);
}
/**
* Returns a string representation of a short
**/
public static String byte2str(short i)
{
return new Short(i).toString();
}
/**
* Returns a string representation of a short
**/
public static String int2str(short i)
{
return new Short(i).toString();
}
/**
* Returns a string representation of a int
**/
public static String lng2str(int i)
{
return new Integer(i).toString();
}
/**
* Returns a string representation of a float
**/
public static String sgl2str(float i)
{
return new Float(i).toString();
}
/**
* Returns a string representation of a double
**/
public static String dbl2str(double i)
{
return new Double(i).toString();
}
/**
* Returns a double representation of a string
**/
public static double str2dbl(String srcstr) throws HpException
{
int len = 0;
int i = 0,j = 0;
String tempstr = dealString(srcstr);
double result = 0.0;
double inc = 10.0;
char c = '\0';
double div = 1.0;
boolean dec = false;
boolean neg = false;
j = 0;
len = tempstr.length();
if(len == 0)
throw new HpException(13,"Type mismatch");
c = tempstr.charAt(0);
if(c == '$'){
tempstr = tempstr.substring(1,len);
len --;
if(len == 0)
throw new HpException(13,"Type mismatch");
}else
if(c == '&')
return OHtoString(tempstr);
do
{
c = tempstr.charAt(j);
switch(c){
case '-':
if(j == 0)
neg = true;
else
throw new HpException(13,"Type mismatch");
break;
case '+':
if(j == 0)
neg = false;
else
throw new HpException(13,"Type mismatch");
break;
case '.':
if(dec == false)
dec = true;
else
throw new HpException(13,"Type mismatch");
break;
case 'D':
case 'd':
tempstr = tempstr.toLowerCase().replace('d','e');
case 'E':
case 'e':
if(j==0 || j == len-1)
throw new HpException(13,"Type mismatch");
else
{
try {
result = (new Double(tempstr)).doubleValue();
} catch(NumberFormatException e) { throw new HpException(13,"Type mismatch");}
j = len;
}
break;
default:
if(Character.isDigit(c))
{
if(dec == false)
result = (double)(result * 10 + (c - '0'));
else
{
div = div * 10.0;
result =(double)(result + (c - '0') / div);
}
}
else
throw new HpException(13,"Type mismatch");
break;
}
j++;
}while(j < len);
if(Double.isInfinite(result))
throw new HpException(6,"Overflow");
if(neg == true)
result = - result;
return result;
}
private static String dealString(String srcstr) throws HpException {
String str = srcstr.trim();
int index = srcstr.indexOf(",");
if(index == 0)
throw new HpException(13,"Type mismatch");
if(index == -1)
return str;
String tempstr = "";
while(index != -1) {
tempstr = str.substring(0,index);
str = str.substring(index + 1);
index = str.indexOf(",");
}
tempstr += str;
return tempstr;
}
private static double OHtoString(String s) throws HpException
{
double value = 0;
boolean oflag = false;
boolean hflag = false;
int len = s.length();
int i= 0;
char c = ' ';
c = s.charAt(i++);
if(c != '&')
throw new HpException(13,"Type mismatch");
c = s.charAt(i++);
if(c == 'H' || c == 'h')
hflag = true;
else
if(c== 'O' || c == 'o')
oflag = true;
else
if((c >= '0' && c <= '9'))
{
oflag = true;
i -- ;
}
else
throw new HpException(13,"Type mismatch");
boolean negflag = false ; // is negative ??
if(hflag && (len == 6 || len == 10))
negflag = isNegative(s.charAt(i));
for( ; i < len ; i++)
{
c = s.charAt(i);
if(hflag)
{
value = value * 16;
if(c >= '0' && c <= '9')
value += c - '0';
else
if(c >= 'a' && c <= 'f')
value += c - 'a' + 10;
else
if(c >= 'A' && c <= 'F')
value += c - 'A' + 10;
else
throw new HpException(13,"Type mismatch");
if(negflag)
value -= 15;
}
else
{
if(c >= '0' && c <= '7')
value = value * 8 + c - '0';
else
throw new HpException(13,"Type mismatch");
}
}
if(negflag)
value = value - 1;
return value;
}
private static boolean isNegative(char c) {
if(c >= 'a' && c <= 'f')
return true;
if(c >= 'A' && c <= 'F')
return true;
if(c == '8' || c == '9')
return true;
return false;
}
/**
*Convert string to boolean.
*/
public static boolean str2bool(String val) throws HpException
{
if(val.equalsIgnoreCase("True"))
return true;
else
if(val.equalsIgnoreCase("False"))
return false;
else{
double db=str2dbl(val);
if(db ==(double)0)
return false;
else
return true;
}
}
/**
*Convert string to single.
*/
public static float str2sgl(String val) throws HpException
{
double dbl=str2dbl(val);
return (new Double(dbl)).floatValue();
}
/**
*Convert string to long
*/
public static int str2lng(String val) throws HpException
{
double dbl=str2dbl(val);
int dbl_int=dbl2int(dbl);
return (new Double(dbl_int)).intValue();
}
/**
*Convert boolean to string
*/
public static String bool2str(boolean val)
{
String bool_str;
if(val)
bool_str="True";
else
bool_str="False";
return bool_str;
}
/**
*Convert boolean to double
*/
public static double bool2dbl(boolean val)
{
double bool_dbl;
if(val)
bool_dbl=-1.0;
else
bool_dbl=0.0;
return bool_dbl;
}
/**
*Convert boolean to single.
*/
public static float bool2sgl(boolean val)
{
double bool_sgl;
bool_sgl=bool2dbl(val);
if(bool_sgl == 0.0)
return 0;
else
return -1;
}
/**
*Convert boolean to integer.
*/
public static short bool2int(boolean val)
{
int bool_int;
if(val)
bool_int=-1;
else
bool_int=0;
return (short)bool_int;
}
/**
*Convert boolean to long
*/
public static int bool2lng(boolean val)
{
return bool2int(val);
}
/**
*Convert variant to integer
*/
public static short var2int(Variant val) throws HpException
{
if(val.getType() ==Variant.V_NULL)
throw new HpException(94,"Invalid use of Null");
else
return val.intValue();
}
/**
*Convert variant to boolean.
*/
public static boolean var2bool(Variant val) throws HpException
{
if(val.getType() ==Variant.V_NULL)
throw new HpException(94,"Invalid use of Null");
return val.boolValue();
}
/**
*Convert variant to long
*/
public static int var2lng(Variant val) throws HpException
{
if(val.getType() ==Variant.V_NULL)
throw new HpException(94,"Invalid use of Null");
else
return val.lngValue();
//return 0;
}
/**
*Convert variant to single.
*/
public static float var2sgl(Variant val) throws HpException
{
if(val.getType() == Variant.V_NULL)
throw new HpException(94,"Invalid use of Null");
return val.sglValue();
//return 0f;
}
/**
*Convert variant to double
*/
public static double var2dbl(Variant val) throws HpException
{
if(val.getType() ==Variant.V_NULL)
throw new HpException(94,"Invalid use of Null");
return val.dblValue();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -