怎么都没人阿?
这是一个简单的转换:这个程序仅仅是把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...
这是一个简单的转换:这个程序仅仅是把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...
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货