java 板


LINE

各位前辈大大好 因为最近要做binary format 转成 一般ascii format而遇到一些问题 输入档是序列合成资料全都是用binary表示int,其说明: * Binary The file is in the binary format. The items in an itemset are displayed in increasing order, then followed by a -1, which is the itemset delimiter. When all the itemsets in a sequence are output, follows a -2 as the sequence delimiter. Then starts a new sequence. -1当间隔,-2当换行,其他都是数字表示item 然而我却在转档地方卡住,我用DataInputStream的readInt()读取, 以下是我程式码: DataInputStream br = new DataInputStream(new FileInputStream(args[0])); FileWriter writer = new FileWriter(args[0] + "aspmf"); int intRead; while (br.available() > 0) { intRead = br.readInt(); if (intRead != -2) writer.write(intRead + " "); else writer.write("-2\n"); } 然而输出党却是失败的: 285212672 385875968 -1 -16777217 50331648 285212672 402653184 -1 33554432 83886080 100663296 134217728 285212672 335544320 369098752 -1 0 1342177 28 167772160 285212672 -1 -16777217 402653184 -1 -16777217 33554432 83886080 100663296 285212672 -1 16777216 33554432 134217728 285212672 3187671 04 369098752 385875968 402653184 -1 33554432 -1 0 83886080 134217728 -1 -16777217 读的数量没错但是转成int只有-1跟0是转正确的 後来改用byte[]一次读四个再转int就没有问题 // DataInputStream br = new DataInputStream(new FileInputStream(args[0])); FileInputStream br = new FileInputStream(args[0]); FileWriter writer = new FileWriter(args[0] + "aspmf"); byte[] buf = new byte[4]; int intRead; while (br.available() > 0) { br.read(buf); intRead = convertirOctetEnEntier(buf); //intRead = br.readInt(); if (intRead != -2) writer.write(intRead + " "); else writer.write("-2\n"); [B} //这是直接用网路上的,应该是正确的 public static int convertirOctetEnEntier(byte[] b){ int MASK = 0xFF; int result = 0; result = b[0] & MASK; result = result + ((b[1] & MASK) << 8); result = result + ((b[2] & MASK) << 16); result = result + ((b[3] & MASK) << 24); return result; } 输出结果: 17 23 -1 -2 3 17 24 -1 2 5 6 8 17 20 22 -1 0 8 10 17 -1 -2 24 -1 -2 2 5 6 17 -1 1 2 8 17 19 22 23 24 -1 2 -1 0 5 8 -1 -2 java api说明文件 DataInputStream readInt() Returns: the next four bytes of this input stream, interpreted as an int. 应该是要输出一样的东西,还是我对功能有所误解? 附上完整程式码还有输入档 http://git.io/vf9Q6 (点Raw可下载) http://git.io/vf97n 感谢 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.25.178.235
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/java/M.1430134619.A.617.html ※ 编辑: moers (114.25.178.235), 04/27/2015 19:38:07
1F:→ ssccg: java的readInt是big endian,看起来你资料是little endian 04/27 19:44
2F:→ moers: 原来如此,太感谢了! 04/27 19:52







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BuyTogether站内搜寻

TOP