[100分]怎么将图片转换成16进制字符串???

jiaxueq 2010-01-07 09:24:34
像一张图片转换成像这样的16进制字符串码

FFD8FFE000104A46494600010100000000000000FFDB004300100B0C0E0C0A100E0D0E1211101318281A181616183123251D283A333D3C3933383740485C4E404457453738506D51575F626768673E4D71797064785C656763FFDB0043011112121815182F1A1A2F63423842636363636363636363636363636363636363636363


...全文
650 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
TzSword 2010-01-08
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wenjjing2lianee 的回复:]
好心人真多啊,学习了
[/Quote]
你码楼的?
TzSword 2010-01-08
  • 打赏
  • 举报
回复
不小心搜了个在线转换的...
哈哈http://dancewithnet.com/lab/2009/data-uri-mhtml/create.php
wenjjing2lianee 2010-01-08
  • 打赏
  • 举报
回复
好心人真多啊,学习了
crazylaa 2010-01-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 johnson_hong 的回复:]
从图片拿到byte[],然后调用byte[]转16进制字符的方法就行了
Java codeprivatestatic String byte2hex(byte[] b) {
String hs="";
String stmp="";for (int n=0; n< b.length; n++) {
stmp= (java.lang.Integer.toHexString(b[n]&0XFF));if (stmp.length()==1) {
hs= hs+"0"+ stmp;
}else {
hs= hs+ stmp;
}
}return hs.toUpperCase();
}
[/Quote]
这个好
SambaGao 2010-01-07
  • 打赏
  • 举报
回复
winhex
SambaGao 2010-01-07
  • 打赏
  • 举报
回复
从网上下一个工具直接转不就行了。
config_man 2010-01-07
  • 打赏
  • 举报
回复
/
阿_布 2010-01-07
  • 打赏
  • 举报
回复
一个例子:

package test;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class Dom{

public static void main(String[] args) {
try {
File file = new File("D:\\dom.xml");
DocumentBuilderFactory dobf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dobf.newDocumentBuilder();
Document dc=db.parse(file);
NodeList ndl=dc.getElementsByTagName("VALUE");
for(int i=0;i<ndl.getLength();i++){
String noum=dc.getElementsByTagName("NO").item(i).getTextContent();
System.out.println(noum);
String add=dc.getElementsByTagName("ADDR").item(i).getTextContent();
System.out.println(add);
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(SAXException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}




<?xml version="1.0" encoding="UTF-8"?>
<RESULT>
<VALUE>
<NO>A1234</NO>
<ADDR>深圳XX</ADDR>
</VALUE>
<VALUE>
<NO>B1234</NO>
<ADDR>珠海XXX</ADDR>
</VALUE>
</RESULT>
sui_yuan_zhe 2010-01-07
  • 打赏
  • 举报
回复
d顶
mouer 2010-01-07
  • 打赏
  • 举报
回复

public static void main(String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream(new File("test.png"));
FileChannel fileChannel = fileInputStream.getChannel();

ByteBuffer buffer = ByteBuffer.allocate(1024);
StringBuffer sb = new StringBuffer();
while(fileChannel.read(buffer)!=-1){
buffer.flip();
while(buffer.hasRemaining()){
sb.append(Integer.toHexString(buffer.get()));
}
buffer.clear();
}
System.out.print(sb.toString().toUpperCase());
fileChannel.close();
fileInputStream.close();
}
invoked 2010-01-07
  • 打赏
  • 举报
回复
路过,学习。
苍蝇①号 2010-01-07
  • 打赏
  • 举报
回复
	public static void main(String []args) throws Exception{
DataInputStream in = new DataInputStream(new FileInputStream("C:/Users/Public/Pictures/Sample Pictures/Autumn Leaves.jpg"));
PrintStream out = new PrintStream(new FileOutputStream("d:/img.txt"));
while(in.available() > 0){
out.printf("%x", in.read());
}
out.close();
in.close();

}
树成 2010-01-07
  • 打赏
  • 举报
回复
首先你要获得图片的byte,然后通过如下方法:

byte[]b=new byte[]{127,99,98};
String hex="0123456789abcdef";
String r="";
for (int i = 0; i < b.length; i++) {
r+=String.valueOf( hex.charAt((b[i]>>4)&0xf))+
String.valueOf( hex.charAt((b[i]>>0)&0xf));
}
System.out.println(r);

其中的b变量是你需要转换的突破字节
Johnson_Hong 2010-01-07
  • 打赏
  • 举报
回复
从图片拿到byte[],然后调用byte[]转16进制字符的方法就行了
private static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧