Once again: how convert byte[] to int please !

S

Spendius

Hi,
I'm back again with my array of bytes conversion procedure
because I've noticed that, using:
int val;
for (int k=b.length-1, j=0; k>=0; k--, j++)
val += (b[k] & 0xff) << (8*j);
System.out.println("val: "+val);

I DON'T GET THE SAME RESULTS whether I read the bytes from
a file, or manually check it, i.e. explicitly write the code:
byte b[] = new byte[2];
b[0] = 0x85; // 7e
b[1] = 0xf4; // f6
I get the following:
1rst case:
b[0]: 0x85
b[1]: 0xf4
val: 34292 // <= OK this is what I expect
(BUT ! 2389 returned when read from a file but I do see
"00 00 85 f4" with 'od')
2nd case:
b[0]: 0x7e
b[1]: 0xf6
val: 32502 // <= OK this is what I expect
(BUT ! 646 returned when read from a file but I do see
"00 00 7e f6" with 'od')

Can *please* someone tell me WHY the result differs when I test
my procedure with an array fed from an file InputStream and when
fed manually with the code 'b[x] = 0x..' ??
Thanks.
 
M

Michael Borgwardt

Looks to me like you're doing something wrong when you read from the
file, maybe related to the fact that Java bytes are signed.

Post the code where you read from the file.
 
S

Steve Horsley

Hi,
I'm back again with my array of bytes conversion procedure
b[0]: 0x7e
b[1]: 0xf6
val: 32502 // <= OK this is what I expect
(BUT ! 646 returned when read from a file but I do see
"00 00 7e f6" with 'od')

Can *please* someone tell me WHY the result differs when I test
my procedure with an array fed from an file InputStream and when
fed manually with the code 'b[x] = 0x..' ??
Thanks.

0d is an ASCII carriage return - end of line sort of thing.
Makes me wonder whatthe exact file format is.

Try using a loop to print the entire file contents byte by byte and see
for yourself.

Steve
 
P

Paul Lutus

Spendius said:
Hi,
I'm back again with my array of bytes conversion procedure
because I've noticed that, using:
int val;
for (int k=b.length-1, j=0; k>=0; k--, j++)
val += (b[k] & 0xff) << (8*j);
System.out.println("val: "+val);

I DON'T GET THE SAME RESULTS whether I read the bytes from
a file, or manually check it, i.e. explicitly write the code:

Line endings. Some Java classes convert internal line endings into system
line endings, some do not. Windows line endings are not the same as Linux
line endings, which are not the same as Mac line endings.
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Spendius said:
Hi,
I'm back again with my array of bytes conversion procedure
because I've noticed that, using:
int val;
for (int k=b.length-1, j=0; k>=0; k--, j++)
val += (b[k] & 0xff) << (8*j);
System.out.println("val: "+val);


I DON'T GET THE SAME RESULTS whether I read the bytes from
a file, or manually check it, i.e. explicitly write the code:

You read four bytes from the file. An int is 32 bits. What do you think
will happen when you shift bits 56 steps to the left?
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Daniel said:
Spendius said:
Hi,
I'm back again with my array of bytes conversion procedure
because I've noticed that, using:
int val;
for (int k=b.length-1, j=0; k>=0; k--, j++)
val += (b[k] & 0xff) << (8*j);
System.out.println("val: "+val);



I DON'T GET THE SAME RESULTS whether I read the bytes from
a file, or manually check it, i.e. explicitly write the code:


You read four bytes from the file. An int is 32 bits. What do you think
will happen when you shift bits 56 steps to the left?

Oops, I mean 48, not 56.
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Daniel said:
Daniel said:
Spendius said:
Hi,
I'm back again with my array of bytes conversion procedure
because I've noticed that, using:

int val;
for (int k=b.length-1, j=0; k>=0; k--, j++)
val += (b[k] & 0xff) << (8*j);
System.out.println("val: "+val);




I DON'T GET THE SAME RESULTS whether I read the bytes from
a file, or manually check it, i.e. explicitly write the code:



You read four bytes from the file. An int is 32 bits. What do you
think will happen when you shift bits 56 steps to the left?


Oops, I mean 48, not 56.

Uhm, ignore my posts. I seem to have read the post way too fast (and my
math wasn't very good either :) But anyway, what I pointed out will
certainly be a problem sooner or later.
 
S

Spendius

Try using a loop to print the entire file contents byte
by byte and see for yourself.
I did this, just a loop on System.out.print(b[x]+" ")
(on every byte) and add a carriage return every
16 characters to get something looking like the
'od' output (or xxd which comes with vim under Windows)
to see I if got the same output, and I do. So I don't
think there's a problem with the way I traverse my file.
I use
FileChannel roCh = new FileInputStream(file).getChannel(); and
ByteBuffer rMap = roCh.map(FileChannel.MapMode.READ_ONLY, 0, size); and the method
byte b;
int pos = rMap.position();
rMap.position(index);
b = rMap.get();
rMap.position(pos);
return b;
to get every byte I need.

Thanks.
 
S

Spendius

OK my mistake I'm *SORRY* it was a bug in my code.
In fact the method used below perfectly works.
Thanks.

Hi,
I'm back again with my array of bytes conversion procedure
because I've noticed that, using:
int val;
for (int k=b.length-1, j=0; k>=0; k--, j++)
val += (b[k] & 0xff) << (8*j);
System.out.println("val: "+val);

I DON'T GET THE SAME RESULTS whether I read the bytes from
a file, or manually check it, i.e. explicitly write the code:
byte b[] = new byte[2];
b[0] = 0x85; // 7e
b[1] = 0xf4; // f6
I get the following:
1rst case:
b[0]: 0x85
b[1]: 0xf4
val: 34292 // <= OK this is what I expect
(BUT ! 2389 returned when read from a file but I do see
"00 00 85 f4" with 'od')
2nd case:
b[0]: 0x7e
b[1]: 0xf6
val: 32502 // <= OK this is what I expect
(BUT ! 646 returned when read from a file but I do see
"00 00 7e f6" with 'od')

Can *please* someone tell me WHY the result differs when I test
my procedure with an array fed from an file InputStream and when
fed manually with the code 'b[x] = 0x..' ??
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top