本篇文章给大家谈谈java解析二进制字符串,以及java二进制运算符对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、java二进制的数字与字符串互转?2、java实现解析二进制文件3、java如何把二进制字符串转成整数4、java怎么将二进制转化成字符串
java二进制的数字与字符串互转?
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Convert {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String x;
x = sc.next();
String ZERO = “00000000”;
byte[] bs = x.getBytes(StandardCharsets.UTF_8); //如果不采用默认的UTF-8格式编码,那么一个汉字相当于两个字节,一个字节就是8个二进制数(8bit)
System.out.println(bs.length+”————-“);
for (byte b : bs) {
String s = Integer.toBinaryString(b);
if (s.length() 8) {
s = s.substring(s.length() – 8);
} else if (s.length() 8) {
s = ZERO.substring(s.length()) + s;
}
System.out.println(s);
}
}
}
java实现解析二进制文件
/**
*
*/
package com.igen.case10;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
/**
*
* @ClassName Case10
* @Description TODO
*
* @author wjggwm
* @data 2017年2月7日 上午11:46:25
*/
public class Case10 {
static final String fileName = “/test.png”;
static final String filePath = “D:/files/case10”;
static final String sourceFileName = “binary”;
public static void main(String[] args) {
try {
readFile(Case10.class.getResource(sourceFileName).toURI().getPath());
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
/**
*
* @Description 解析二进制文件
* @param sourceFileName
*
* @author wjggwm
* @data 2017年2月7日 上午11:47:12
*/
public static void readFile(String sourceFileName) {
InputStream in = null;
try {
in = new FileInputStream(sourceFileName);
// 读取字符串数据长度字节
byte[] txtLenByte = new byte[2];
in.read(txtLenByte);
int txtlen = byte2ToUnsignedShort(txtLenByte, 0);
// 读取字符串字节
byte[] txtByte = new byte[txtlen];
in.read(txtByte);
//字符串为UTF-8编码
String txt = new String(txtByte, “UTF-8”);
// 输出字符串
System.out.println(txt);
// 读取图片数据长度
byte[] imgLenByte = new byte[4];
in.read(imgLenByte);
int imgLen = byte4ToInt(imgLenByte, 0);
// 读取图片数据
byte[] img = new byte[imgLen];
in.read(img);
// 生成图片文件
saveToImgByBytes(filePath, fileName, img);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* @Description 将字节写入文件
* @param imgName
* @param imgByte
*
* @author wjggwm
* @data 2017年2月7日 上午11:07:45
*/
public static void saveToImgByBytes(String filePath, String imgName, byte[] imgByte) {
try {
File dic = new File(filePath);
if (!dic.exists()) {
dic.mkdirs();
}
File image = new File(filePath + imgName);
if (!image.exists()) {
image.createNewFile();
}
FileOutputStream fos = new FileOutputStream(image);
fos.write(imgByte);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @Description byte数组转换为无符号short整数
* @param bytes
* @param off
* @return
*
* @author wjggwm
* @data 2017年2月7日 上午11:05:58
*/
public static int byte2ToUnsignedShort(byte[] bytes, int off) {
// 注意高位在后面,即大小端问题
int low = bytes[off];
int high = bytes[off + 1];
return (high 8 0xFF00) | (low 0xFF);
}
/**
*
* @Description byte数组转换为int整数
* @param bytes
* @param off
* @return
*
* @author wjggwm
* @data 2017年2月7日 上午11:07:23
*/
public static int byte4ToInt(byte[] bytes, int off) {
// 注意高位在后面,即大小端问题
int b3 = bytes[off] 0xFF;
int b2 = bytes[off + 1] 0xFF;
int b1 = bytes[off + 2] 0xFF;
int b0 = bytes[off + 3] 0xFF;
return (b0 24) | (b1 16) | (b2 8) | b3;
}
}
java如何把二进制字符串转成整数
java将二进制装换成整数,可以使用Integer类的方法,来进行转换,如下:
Integer.parseInt(“80000000”,2);
Integer属于int基本类型的包装类,可以实现各个类型的互相转换,还可以输出成string类型,只需要使用toString()方法。
java怎么将二进制转化成字符串
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/*
* String(byte[] bytes, String charsetName):通过指定的字符集解码字节数组
* byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组
*
* 编码:把看得懂的变成看不懂的
* String — byte[]
*
* 解码:把看不懂的变成看得懂的
* byte[] — String
*
* 举例:谍战片(发电报,接电报)
*
* 码表:小本子
* 字符 数值
*
* 要发送一段文字:
* 今天晚上在老地方见
*
* 发送端:今 — 数值 — 二进制 — 发出去
* 接收端:接收 — 二进制 — 十进制 — 数值 — 字符 — 今
*
* 今天晚上在老地方见
*
* 编码问题简单,只要编码解码的格式是一致的。
*/
public class StringDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = “你好”;
// String — byte[]
byte[] bys = s.getBytes(); // [-60, -29, -70, -61]
// byte[] bys = s.getBytes(“GBK”);// [-60, -29, -70, -61]
// byte[] bys = s.getBytes(“UTF-8”);// [-28, -67, -96, -27, -91, -67]
System.out.println(Arrays.toString(bys));
// byte[] — String
String ss = new String(bys); // 你好
// String ss = new String(bys, “GBK”); // 你好
// String ss = new String(bys, “UTF-8”); // ???
System.out.println(ss);
}
}
关于java解析二进制字符串和java二进制运算符的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。