byte 跟 short的转换??

yongxuanlai 2003-03-09 02:35:36
此算法把2个short(其实用12bit就可以表示大小了)
变成 3个byte(按照次序)
__——— __—--
index[0] index[1]

即把上面一行的数据 顺序读入到 3个byte里
把划底线的那两个4位去掉

放到文件;然后从文件读出,再恢复成2个short;
这是一个调试的小程序, 百思不得其解,不知道错在哪》

谢谢!!
try{


byte buf[]=new byte[3];

buf[1]=(byte)(index[0]<<4);//index 是一个短整形数组
buf[0]=(byte)(index[0]>>4);

buf[2]=(byte)index[1];
buf[1]+=(byte)(index[1]>>8);

out.write(buf,0,3);
System.out.print(index[0]+"\t"+index[1]+"\t");

//output the decoding ,just for dubugging

short codebuf[]=new short[2];

//把3 byte的数据 恢复成2个short
codebuf[0]=(short)(buf[0]<<4);
codebuf[0]+=(short)(buf[1]>>4);

codebuf[1]=(short)buf[2];
codebuf[1]=(byte)(buf[1]<<4);
codebuf[1]+=(short)(buf[1]<<4);


System.out.println("\t"+codebuf[0]+"\t"+codebuf[1]);
...全文
268 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yongxuanlai 2003-03-09
  • 打赏
  • 举报
回复
怎么都没人阿?

这是一个简单的转换:

这个程序仅仅是把2个short转换成4 byte
再转换回来
可是,结果好像有点随机, 有对的 也有错的;
这是为何那》?????
//******************
class TestChange
{
public static void main(String args[])
{

short index[]=new short[2];
byte buf[]=new byte[4];
short codebuf[]=new short[2];

for(int i=0;i<100;i++)
{
index[0]=(short)(Math.random()*1000);
index[1]=(short)(Math.random()*1000);



System.out.print(index[0]+"\t"+index[1]+"\t");

buf[1]=(byte)(index[0]);
buf[0]=(byte)(index[0]>>8);

buf[3]=(byte)(index[1]);
buf[2]=(byte)(index[1]>>8);

//now to decode

codebuf[0]=(short)(buf[0]<<8);
codebuf[0]=(short)((int)codebuf[0]+(int)buf[1] ) ;

codebuf[1]=(short)(buf[2]<<8);
codebuf[1]=(short)((int)codebuf[1]+(int)buf[3]);


System.out.println("\t"+codebuf[0]+"\t"+codebuf[1]);

}//for
}
}


××××××××××××××××××××××××××××××
287 865 287 865
37 743 37 487
499 104 243 104
752 263 496 263
342 103 342 103
949 978 693 722
350 118 350 118
843 273 843 273
688 490 432 234
616 920 616 664
720 69 464 69
594 157 594 -99
603 678 603 422
241 396 -15 140
358 40 358 40
228 747 -28 491
214 743 -42 487
819 41 819 41
942 791 686 791
880 665 880 409
313 964 313 708
871 571 871 571
792 735 792 479
296 50 296 50
49 792 49 792
209 418 -47 162
917 227 661 -29
549 595 549 595
967 348 711 348
259 664 259 408
960 670 704 414
399 901 143 645
66 49 66 49
677 406 421 150
859 129 859 -127
170 464 -86 208
827 214 827 -42
269 975 269 719
289 452 289 196
281 798 281 798
402 745 146 489
477 681 221 425
201 97 -55 97
804 511 804 255
397 232 141 -24
918 967 662 711
33 162 33 -94
678 342 422 342
949 76 693 76
743 155 487 -101
388 318 132 318
463 463 207 207
43 483 43 227
816 183 816 -73
274 632 274 632
756 571 500 571
242 421 -14 165
964 3 708 3
201 500 -55 244
657 560 401 560
291 890 291 890
814 530 814 530
705 155 449 -101
903 802 647 802
274 111 274 111
946 335 690 335
125 490 125 234
555 331 555 331
272 463 272 207
394 451 138 195
692 144 436 -112
369 462 369 206
708 283 452 283
734 991 478 735
242 97 -14 97
226 622 -30 622
830 869 830 869
262 465 262 209
132 231 -124 -25
249 441 -7 185
7 285 7 285
20 961 20 705
164 590 -92 590
492 381 236 381
797 813 797 813
678 602 422 602
588 303 588 303
320 437 320 181
967 158 711 -98
511 941 255 685
503 400 247 144
870 699 870 443
914 738 658 482
0 199 0 -57
214 987 -42 731
218 807 -38 807
168 678 -88 422
778 512 778 512
372 509 372 253
118 594 118 594
Press any key to continue...

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧