首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java中socket读取数据有关问题

2013-03-21 
java中socket读取数据问题调试助手发送数据fe fe fe 69使用BufferedInputStream读取时,得到的字节数组中的

java中socket读取数据问题
调试助手发送数据fe fe fe 69
使用BufferedInputStream读取时,得到的字节数组中的数据是66 65 66 65 66 65 36 39,怎样才能得到fe fe fe 69 socket java
[解决办法]
转一下字节
BufferedInputStream bf=new BufferedInputStream(fs);
FileOutputStream os=new FileOutputStream("e:/os.txt");
byte[] buffer = new byte[1024];   
int count = 0;  
while((count = bf.read(buffer)) >= 0){  
os.write(buffer, 0, count);
}
[解决办法]
也可以直接使用DataInputStreamReader或BufferedReader等读取。
[解决办法]
我着一个笨办法:


import java.util.Arrays;
public  class ByteArrayToHex{
    public static void main(String[] args){
        byte[] result;
        byte[] original = {0x66, 0x65, 0x66, 0x65, 0x66, 0x65, 0x36, 0x39};//字节数组中的数据,为16进制.
        result = change(original);
        System.out.println(Arrays.toString(result));//显示。
    }

    public static byte[] change(byte[] b){
        byte[] out = new byte[b.length/2];//定义返回数组的大小。
        byte h,l;                         //两个临时变量。
        for(int i=0; i<b.length; i+=2){
            if(b[i]>=97&&b[i]<=102){
                h = (byte)(b[i]-97+10);//把高位的ascii码,转成其代表的字符。a-f
            }else if(b[i]>=65 && b[i]<=69){
                h = (byte)(b[i]-65+10);//A-F.
            }else{
                h = (byte)(b[i]-48);//0-9
            }
    h = (byte)(h<<4);//左移4位,放在高位。

            if(b[i+1]>=97 && b[i+1]<=102){
                l = (byte)(b[i+1]-97+10);//把低位的ascii码,转成其代表的字符或数字。a-f
            }else if(b[i+1]>=65 && b[i+1]<=69){
                l = (byte)(b[i+1]-65+10);//(A-F).
            }else{
                l = (byte)(b[i+1]-48);//0-9
            }


            out[i/2]=(byte)(h+l);//合并成一个字节。
        }//end for
return out;
    }
}

热点排行