51,409
社区成员
发帖
与我相关
我的任务
分享
public class Test {
public static void main(String[] args) {
System.out.println(toHexString(-100));
}
public static String toHexString(int input) {
boolean b = false;//是正否为负数
if (input < 0) {
b = true;
input = -input;
}
String output = "";
int base = 16;
int c = 0;
int lowbit = 0;
base >>= 1;
do {
lowbit = input & 1;
input = (input >> 1) & 0xffffffff;
c = ((input % base) << 1) + lowbit;
if (c < 10)
c += '0';
else
c += 'A';
output += (char) c;
} while ((input /= base) != 0);
StringBuffer sb = new StringBuffer(output);
output = sb.reverse().toString();
if (b)
output = "-" + output;
return output;
}
}
public class Test{
public static void main(String[] args) {
System.out.println(toHexString(-100));
}
public static String toHexString(Integer i){//i>0
boolean b = false;//是正否为负数
if(i==0)return "0";
else if(i<0) {
b = true;//是负数
i=-i;
}
char[] hex = "0123456789ABCDEF".toCharArray();
String result = "" ;
while(i!=0){
result=hex[i%16]+result;
i/=16;
}
if(b) result="-"+result;
return result;
}
}
public class Test{
public static void main(String[] args) {
System.out.println(convert(1234));
}
public static String convert(Integer i){//i>0
char[] upperCharacter = "0123456789ABCDEF".toCharArray();
String result = "" ;
while(i!=0){
result=upperCharacter[i%16]+result;
i/=16;
}
return result;
}
}
public static String toHexString(int input)
{
String output="";
int base=16;
int c=0;
int lowbit=0;
base >>= 1;
do {
lowbit = input & 1;
input = (input >> 1) & 0xffffffff;
c = ((input % base) << 1) + lowbit;
if(c < 10)
c += '0';
else c +='A';
output+=(char)c;
} while((input/=base)!=0);
StringBuffer sb=new StringBuffer(output);
output= sb.reverse().toString();
return output;
}