各位朋友,我一个搞数据库的接到一个搞程序的任务,需要编写一个小程序,客户输入10进制,我得把其数据以16进制存放,我用过Java做过开发,
代码如下: File file=new File("D:\\liu\\zz\\sample\\appcggint.erg");
FileOutputStream fos=new FileOutputStream(file);
        DataOutputStream dos=new DataOutputStream(fos);
int num=27;
String s= Integer.toHexString(num);
dos.writeBytes(s);
文件生成了,结果是1b没问题,但是是以ascii码存放的,toHexString返回的是字符串,以字节写进去不是真正的16进制格式,大家给个思路吧,谢谢大家了!

解决方案 »

  1.   

    我加100分哈,
    我的那个小代码生成的文件,用写字板打开是1b,没问题,用tiny hexer(看16进制的软件)打开成了3162,我晕倒了,用ASCII显示了,大家帮帮啊。
      

  2.   

    你根本就不用在程序里转:
    File file=new File("c:\\1.txt");
    FileOutputStream fos=new FileOutputStream(file);
    DataOutputStream dos=new DataOutputStream(fos);
    dos.write(27);
    这样用tiny hexer看到的就是1b了
      

  3.   

    public class Test {
        public static void main(String[] args) throws MalformedURLException, IOException {
            PrintWriter out = new PrintWriter("d:/test.txt");
            try {
                SimpleDateFormat format = new SimpleDateFormat("MM月dd日HH时mm分ss秒");
                Date date = format.parse("3月30日0时12分41秒");
                int month = date.getMonth() + 1;
                int day = date.getDate();
                int hours = date.getHours();
                int minutes = date.getMinutes();
                int seconds = date.getSeconds();
                String str = new String(Integer.toHexString(month) + Integer.toHexString(day) +  Integer.toHexString(hours) + Integer.toHexString(minutes) + Integer.toHexString(seconds));
                out.print(str);
                out.flush();
                out.close();        } catch (ParseException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    测试了一下,找开文件时,格式与你的要求相似,但前面没有补0
      

  4.   

    你的程序不就是对的吗?
    你要把10进制变成16进制的数据,那不就是27变成1B吗?然后存到数据库中不就行了。你用tiny hexer打开一个文本文件,他又把数据做了一次转换,也就是以16进制表示
    的ASSCII码。这只是不同工具对同一个数据的不同显示,这不是转换。所以你的程序就可以呀,直接存到数据库就对了。
      

  5.   

    看看行不?import java.util.*; 
    import java.sql.*;
    import java.sql.Date;
    import java.text.*;
    import java.io.*;
    public class Test{ 
    public static void main(String[] args){ 
    Date dt=new Date(System.currentTimeMillis());
    Calendar  calendar =new GregorianCalendar(2008,1,1);
    calendar.setTime(dt);
    try{
    PrintStream ps=new PrintStream(new File("xxxe.txt"));
    int year=calendar.get(Calendar.YEAR);
    int month=calendar.get(Calendar.MONTH);
    int date =calendar.get(Calendar.DATE);
    ps.print(String.valueOf(Integer.toHexString(year)));
    ps.print(String.valueOf(Integer.toHexString(month+1)));
    ps.print(String.valueOf(Integer.toHexString(date)));
    }catch(FileNotFoundException e){

    }

     } 

      

  6.   

    不齐的话,补0import java.util.*; 
    import java.sql.*;
    import java.sql.Date;
    import java.text.*;
    import java.io.*;
    public class Test{ 
    public static void main(String[] args){ 
    Date dt=new Date(System.currentTimeMillis());
    Calendar  calendar =new GregorianCalendar(2008,1,1);
    calendar.setTime(dt);
    try{
    PrintStream ps=new PrintStream(new File("xxxe.txt"));
    int month=calendar.get(Calendar.MONTH);
    int date =calendar.get(Calendar.DATE);
    int hour =calendar.get(Calendar.HOUR);
    int minute =calendar.get(Calendar.MINUTE);
    int seconds =calendar.get(Calendar.SECOND);
    if(String.valueOf(Integer.toHexString(month+1)).length()<2){//说明只有一位
    ps.println("0"+String.valueOf(Integer.toHexString(month+1)));
    }else{
    ps.println(String.valueOf(Integer.toHexString(month+1)));
    }
    if(String.valueOf(Integer.toHexString(date)).length()<2){//说明只有一位
    ps.println("0"+String.valueOf(Integer.toHexString(date)));
    }else{
    ps.println(String.valueOf(Integer.toHexString(date)));
    }
    if(String.valueOf(Integer.toHexString(hour)).length()<2){//说明只有一位
    ps.println("0"+String.valueOf(Integer.toHexString(hour)));
    }else{
    ps.println(String.valueOf(Integer.toHexString(hour)));
    }
    if(String.valueOf(Integer.toHexString(minute)).length()<2){//说明只有一位
    ps.println("0"+String.valueOf(Integer.toHexString(minute)));
    }else{
    ps.println(String.valueOf(Integer.toHexString(minute)));
    }
    if(String.valueOf(Integer.toHexString(seconds)).length()<2){//说明只有一位
    ps.println("0"+String.valueOf(Integer.toHexString(seconds)));
    }else{
    ps.println(String.valueOf(Integer.toHexString(seconds)));
    }
    }catch(FileNotFoundException e){
    System.out.println("文件没有找到");
    }

     } 

      

  7.   

    感谢大家捧场哈,加100分结贴,我描述的不清楚不好意思,目的是用文本打开全是乱码,用tiny hexer打开是16进制,不过MT502确实是我要的,希望大家以后多多帮助。