首页 > 编程知识 正文

java 转16进制,java int转16进制

时间:2023-05-04 12:10:05 阅读:186941 作者:2142

1、Java10十进制十六进制

/**卡号位数:8*/publicstaticbytecard _ num _ bit=8;/* * * is blank * * @ param value * @ return true : blank; false 3360 not blank */privatestaticbooleanisblank (string value ) if ) value==null||''.equals ) value.trim ) ) }/** * 10用十进制转换为十六进制,卡号的位数* * @ paramstr * @ return */publicstaticstringtohexstr (stringstr ) { String result=" " String regex='^\d{1,}$ '; if (! isblank(str ) ) { str=str.trim; if(str.Matches(regex ) (stringhexstr=long.tohexstring ) long.parselong(str.trim ) ).toUpperCase ); Hexstr.length (card _ num _ bit ) hexstr=org.Apache.com mons.lang3. string utils.left pad ) hexstr,carstr }elseif(ishex(str ) ) if ) str.Length ) CARD_NUM_BIT ) str=org.Apache.com mons.lang3. string utils.str } }返回结果; () ) ) ) )。

2、Java16十六进制十进制

/* * * is blank * * @ param value * @ return true : blank; false 3360 not blank */privatestaticbooleanisblank (string value ) if ) value==null||''.equals ) value.trim ) ) }/** *进制数* * @ paramstr hex * @ return */publicstaticbooleanishex (stringstrhex ) { int i=0; if(strhex.length(2) if ) strhex.charat )0)=='x ) ) strhex.charat )1)=='x'|strhex.charat )1 } } for i strHex.length (; I ) charch=strHEX.charat(I; if () ch='0'ch='9' )|) ch='a'ch='f ' )|) a'ch='f ' ) ) continue; 返回假; } return true; }/** *对应于进制的数值* * @ param ch * @ return * @ throws exception */privatestaticintgethex (charch ) throwsexception(if ) if(ch='a'ch='f ' )返回(int ) ) ch - 'A' 10 ); thrownewexception(errorparam ); }/** *乘方* * @ param nvalue * @ param ncount * @ return * @ throws exception */privatestaticlonggetpower (intn value,int nt (); if(ncount==0)返回1; long nSum=1; for(intI=0; i nCount; I ) { nSum=nSum * nValue; } return nSum; }/** * 16将十六进制转换为十进制,乘以与十六进制的各位对应的16的乘方进行加法运算。 * @param strHex要转换的字符串* @param force是否强制用十六进制转换,纯粹的数字也可能是十六进制。 true是以十六进制形式处理纯数字的* @ return */publicstaticlonghextolong (stringstrhextolong )。 String regex='^\d{1,}$ '; if (! isblank(strhex ) ) { strHex=strHex.trim; } else { return nResult; (if (! forcestrhex.matches(regex ) (returnlong.parselong ) strhex ); (if (! ishex(strhex ) ) { return nResult; } String str=strHex.toUpperCase (; if(str.length(2) if ) str.charat )0)=='0' str.charAt(1)1)='x ' ) str=str.substring )2); } } int nLen=str.length (; for(intI=0; i nLen; I ) charch=str.charat(Nlen-I-1 ); try{nresult=(gethex(ch ) *getpower ) 16,I ); }catch(exceptione ) { e.printStackTrace ); } } return nResult; }/** * 16十六进制十进制* @ paramstrhex * @ return */publicstaticlonghextolong (stringstrhex ) returnhextolong ) str hex,false

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。