java存储字节,java 数目字转化成字节存储算法
java 數字轉化成字節存儲算法
package com;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestByte {
/**
* ip轉化成4個字節進行存儲,然后再把四個字節轉換成byte
*/
public static void ipTobyte()
{
String ip="222.168.205.83";
String[] ips = ip.split("\\.");
int ip1 = Integer.valueOf(ips[0]);
int ip2 = Integer.valueOf(ips[1]);
int ip3 = Integer.valueOf(ips[2]);
int ip4 = Integer.valueOf(ips[3]);
System.out.println(ip1);
System.out.println(ip2);
System.out.println(ip3);
System.out.println(ip4);
byte b1 = (byte)ip1;
byte b2 = (byte)ip2;
byte b3 = (byte)ip3;
byte b4 = (byte)ip4;
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
// int 轉byte之后 操作127的數字將全部變成負數形式,如果想轉成原來的 int值 直接 & 0xFF
System.out.println(b1 & 0xFF);
System.out.println(b2 & 0xFF);
System.out.println(b3 & 0xFF);
System.out.println(b4 & 0xFF);
}
public static void byteToIP()
{
}
/**
* long -> 4位字節 -> long
* 主要是做了一個數據壓縮算法
* 比如說一個ip可以直接存儲成4個字節
*/
public static void numToBinaryString()
{
//源數字
long num = 98786657;
//轉化成二進制串
String binary = Long.toBinaryString(num);
System.out.println(binary);
int length = 32-binary.length();
//補充到32位
for(int i=0;i
{
binary = '0'+binary;
}
System.out.println(binary);
System.out.println(binary.length());
//分割成四個字節 (一個字節8位)
String b1 = binary.substring(0, 8);
String b2 = binary.substring(8, 16);
String b3 = binary.substring(16, 24);
String b4 = binary.substring(24, 32);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
//8位轉化成一個字節
int a1 = Integer.valueOf(b1, 2);
int a2 = Integer.valueOf(b2, 2);
int a3 = Integer.valueOf(b3, 2);
int a4 = Integer.valueOf(b4, 2);
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
byte c1 = (byte)a1;
byte c2 = (byte)a2;
byte c3 = (byte)a3;
byte c4 = (byte)a4;
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
//把字節轉化成long
long ret = 0;
ret |= c4 & 0xFF;
ret |= (c3<<8) & 0xFF00;
ret |= (c2<<16) & 0xFF0000;
ret |= (c1<<24) & 0xFF000000;
System.out.println(ret);
// Integer.toBinaryString(c4&0xFF);
//System.out.println();
}
/**
* 把字節寫入文件,看看是啥效果
* @throws IOException
*/
public static void writeByteToFile() throws IOException
{
File file = new File("a.txt");
FileOutputStream fis = new FileOutputStream(file);
fis.write(-106);
fis.write(-38);
fis.write(0);
fis.write(11);
fis.flush();
System.out.println(file.getAbsolutePath());
}
public static void main(String[] args) throws IOException
{
//TestByte.writeByteToFile();
//TestByte.numToBinaryString();
TestByte.ipTobyte();
}
}
1 樓
dyllove98
2011-05-28
/**
* 從ip的字符串形式得到字節數組形式
*
* @param ip
* 字符串形式的ip
* @return 字節數組形式的ip
*/
public static byte[] getIpByteArrayFromString(String ip) {
byte[] ret = new byte[4];
java.util.StringTokenizer st = new java.util.StringTokenizer(ip, ".");
try {
ret[0] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
ret[1] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
ret[2] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
ret[3] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return ret;
}
總結
以上是生活随笔為你收集整理的java存储字节,java 数目字转化成字节存储算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php实现下单成功提示语,推荐10款提示
- 下一篇: java替换html特殊字符,HTML特