各位朋友,我一个搞数据库的接到一个搞程序的任务,需要编写一个小程序,客户输入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进制格式,大家给个思路吧,谢谢大家了!
代码如下: 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进制格式,大家给个思路吧,谢谢大家了!
解决方案 »
- java gui jframe窗体
- 初学者问题:关于bean中的property
- 请教如何分割时间段的问题
- 请问:将.doc文本读出来,用什么语句来实现呀?
- 请高手帮帮我,一个简单的书上的问题,有谁碰见过?
- 显示图片-java的组件有吗?
- 关于数组的一个问题,高手请解决一下,谢谢了!!!
- 请问这个方法是InputStream.read(byte[] b, int off, int len)什么
- 请问这段代码在编译是怎么总是报错呢??
- ◎!¥◎%当真没人知道何处有JBuilder6下载是不?
- 关于str == "" 与 str.length() == 0
- eclipse怎么创建java application源文件
我的那个小代码生成的文件,用写字板打开是1b,没问题,用tiny hexer(看16进制的软件)打开成了3162,我晕倒了,用ASCII显示了,大家帮帮啊。
File file=new File("c:\\1.txt");
FileOutputStream fos=new FileOutputStream(file);
DataOutputStream dos=new DataOutputStream(fos);
dos.write(27);
这样用tiny hexer看到的就是1b了
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
你要把10进制变成16进制的数据,那不就是27变成1B吗?然后存到数据库中不就行了。你用tiny hexer打开一个文本文件,他又把数据做了一次转换,也就是以16进制表示
的ASSCII码。这只是不同工具对同一个数据的不同显示,这不是转换。所以你的程序就可以呀,直接存到数据库就对了。
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){
}
}
}
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("文件没有找到");
}
}
}