求问,怎样把一个字符串小数如何转化为二进制成二进制然后输出

二进制怎么转化成字符串
这种二进制怎么转化成字符串
你别逗我,二进制不都是01吗?哪来那么多数字和字符的????
--- 共有 2 条评论 ---
: 确定你这不是二进制,二进制都是0和1组成,你这个极有可能是十六进制的字符串
我要知道我会发吗 我重新编辑了一下帖子帮忙看下
你这应该是十六进制的
十六进制转字符串就简单多了,网上一查一大堆现有函数
--- 共有 3 条评论 ---
: 不用客气
看到了 确实是16进制的2669人阅读
前几天在csdn看到有人问类似的问题,当时没有时间帮他解决出来。最近抽空写了一个小例子,希望对他能有所帮助。
import java.io.BufferedRimport java.io.FileNotFoundEimport java.io.FileRimport java.io.FileWimport java.io.IOE
public class Test03 {
&public static void main(String[] args) {&&String string="ab 中国";&&FileWriter fw =&&BufferedReader fr =&&try {&&&fw = new FileWriter("C://a.txt");&&&byte[] byts = string.getBytes();&&&for(int i=0;i&byts.i++)&&&{&&&&fw.write(getBit(byts[i]));&&&}&&&fw.close();&&&fr = new BufferedReader(new FileReader("C://a.txt"));&&&fw = new FileWriter("C://b.txt");&&&char[] cbuf = new char[8];&&&int len = -1;&&&while(-1!=(len=fr.read(cbuf)))&&&{&&&&if(cbuf[0]=='0')&&&&{&&&&&byte[] bt = new byte[1];&&&&&bt[0] = Byte.parseByte(String.valueOf(cbuf), 2);&&&&&fw.write(new String(bt));&&&&}&&&&if(cbuf[0]=='1')&&&&{&&&&&byte[] bt = new byte[2];&&&&&bt[0] = (byte) (-((Integer.parseInt(String.valueOf(cbuf), 2)^0xFF)+1));&&&&&if((8==(len=fr.read(cbuf)))&&cbuf[0]=='1')&&&&&&bt[1] = (byte) (-((Integer.parseInt(String.valueOf(cbuf), 2)^0xFF)+1));&&&&&fw.write(new String(bt));&&&&}&&&&&&&}&&&&&&&&&&&&&&&&&& fr.close();&&&& fw.close();&&&&&&} catch (FileNotFoundException e) {&&&// TODO Auto-generated catch block&&&e.printStackTrace();&&} catch(IOException e)&&{&&&e.printStackTrace();&&}&&finally{&&&try {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& fr.close();&&&&fw.close();&&&} catch (IOException e) {&&&&// TODO Auto-generated catch block&&&&e.printStackTrace();&&&}&&}
&}&public static String getBit(int b)&{&&String result="";&&if(b&0)&&{
&&& result=Integer.toBinaryString(b);&&&& result=("".substring(result.length())).concat(result);&&}&&else if(b==0)&&{&&& result="";&&}&&else&&{&&&result=(Integer.toBinaryString(b)).substring(24);&&}&&&&}
注:1. 本代码中只适用于把中文字符解析为两个字节,英文字符解析为一个字节的中文编解码方式。如果使用其它的编码方式,请进行相应的变化。
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:3074次
排名:千里之外怎么样将一个文件流转换为二进制的字符串? - ITeye问答
现在我需要通过将图片转换为二进行制的字符串的方式传输图片,将如何实现?请各位帮一下忙,谢谢!!!
[flash=200,200][url][img][list][*]引用[u][i][b][/b][/i][/u][/list][/img][/url][/flash]
然后再把BY 转换成NEW STRING(BY)
public static byte[] getFileToByte(File file) {
byte[] by = new byte[(int) file.length()];
InputStream is = new FileInputStream(file);
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
byte[] bb = new byte[2048];
ch = is.read(bb);
while (ch != -1) {
bytestream.write(bb, 0, ch);
ch = is.read(bb);
by = bytestream.toByteArray();
} catch (Exception ex) {
ex.printStackTrace();
用sun.misc.BASE64Encoder不错
php推荐用base64,5只支持那个
二进制的传输方式速度上慢啊
用对象传输啊
FormFile file = uploaldForm.getFile();
String fileP
//取得上传的文件
InputStream stream = file.getInputStream();
//把文件读入
filePath = request.getRealPath("/upload");
//上传到指定的upload包中
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "/"
+ file.getFileName());
//建立一个上传文件的输出流
System.out.println(filePath + "/" + file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
//将文件写入服务器
bos.close();
stream.close();[align=left][/align]引用
} catch (Exception e) {
e.printStackTrace();
return mapping.findForward("uploald");
request.setAttribute("file",& filePath + "\\" + file.getFileName());
return mapping.findForward("ok");
public String encode(InputStream in)throws IOException{
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
byte[] data = new byte[in.available()];
in.read(data);
return encoder.encode(data);
public byte[] decode(String base64Str)throws IOException{
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
return decoder.decodeBuffer(base64Str);
Base64& 可以在不同的平台, 特别是ISO8859编码系统中避免乱码问题.
你确定要用二进制 字符串 来传输图片? "" 表示一个byte ?
这相当于用 8个byte 来传输一个 byte ,效率太低了
推荐base64,经过测试,没有问题。
读到文件 byte data[] ,然后用Base64编码得到字符串,接收方用Base64解码。
用Base64吧
FileInputStream uFile = new FileInputStream(fileName);
int i = uFile.available(); //TODO 得到文件大小
byte data[] = new byte[i];
uFile.read(data); //TODO 读数据
uFile.close();
你应该使用一种编码方式来转换。参考Base64编码,比较有效率,得到的字符串长度约为二进制数据的1.3倍。上面的人提供的方式转换起来会长得多。
2进制流?2进制字符串?
FileInputStream fs = new FileInputStream("c://bugs.jpg");
byte[] b = new byte[fs.available()];
fs.read(b);
fs.close();
for(Byte by:b){
& System.out.print(Long.toString((long)by,2));
}
FileInputStream fs&& =&& new&& FileInputStream("c://bugs.jpg");&
&&&&&&& byte[] b = new byte[fs.available()];
&&&&&&& fs.read(b);
&&&&&&& fs.close();
已解决问题
未解决问题}

我要回帖

更多关于 php 输出二进制字符串 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信