- 1、本文档共15页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
网络字节顺序、大端法、小端法
/dodorunning/article/details/636605网络字节顺序、大端法、小端法 分类: /dodorunning/article/category/183723j2se 2006-03-23 23:53 3213人阅读 /dodorunning/article/details/636605评论(1) javascript:void(0);收藏 /dodorunning/article/details/636605举报 在国内的4种短信协议的协议头部分,都定义了4个字节长度的message length字段,字段的数据类型为无符号整形(也就是说,这个字段的范围是0-2^16-1);而在java语言中,没有无符号整形这种数据类型(如果用int类型来表示,由于java中int型是有符号数,则会发送溢出),我设想将message length存入long类型中,将数字的大小控制在0-2^16-1范围之内,当超过此范围归零重新开始。在网络传输时,将long类型先转化为byte数组,步骤如下:long l;byte[] b;b[0]=(byte)(l24);b[1]]=(byte)(l16);b[2]]=(byte)(l8);b[3]]=(byte)(l);此时,b[]中就按照网络字节顺序(大端法,即l的高位数据存放在byte[]的低位地址,因为地址是从低向高发展的)存放着4个bytes的数据使用OutputStream的public void write(byte[]?b,int?off,int?len)方法来向Socket写字节流,写byte[0]至byte[3]的字节。?java.io Class OutputStreamwritepublic abstract void write(int?b) throws /j2se/1.4.2/docs/api/java/io/IOException.htmlIOExceptionWrites the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored. Subclasses of OutputStream must provide an implementation for this method. ?Parameters: b - the byte. Throws: /j2se/1.4.2/docs/api/java/io/IOException.htmlIOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.writepublic void write(byte[]?b, int?off, int?len) throws /j2se/1.4.2/docs/api/java/io/IOException.htmlIOExceptionWrites len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation. The write method of OutputStream calls the write method of one argument on each of the bytes to be written out. Subclasses are encouraged to override this method and provide a more efficient implementation. If b is
文档评论(0)