File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Trouble Converting from Int Array to Byte Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Trouble Converting from Int Array to Byte Array" Watch "Trouble Converting from Int Array to Byte Array" New topic
Author

Trouble Converting from Int Array to Byte Array

Mitch Robinson
Ranch Hand

Joined: Oct 29, 2009
Posts: 30
Hi,

Hoping someone can help a java newbie out, firstly I have a AFP file which has been read as a byte array(signed). I then need to search this byte array for a specific pattern of HEX Code. Originally I had this as an integer array which made it pretty straightforward to do what I was doing but due to changes in the way the file is read I now have to deal with the signed byte array.

int endPattern[] = {0x5a,0x00,0x10,0xD3,0xA9,0xCD,0x00,0x00,0x00,0xFF,0xFF,0x40,0x40,0x40,0x40,0x40,0x40}; OLD
byte endPattern[] = {0x5a,0x00,0x10,0xD3,0xA9,0xCD,0x00,0x00,0x00,0xFF,0xFF,0x40,0x40,0x40,0x40,0x40,0x40}; CURRENT

This is the line of code i'm having difficulty with, previously the HEX i was looking for would convert to the Int array, but when I change this to Byte Array I get the loss of precision error which I believe is down to the bytes being signed in Java.

So my question is how would I convert the hex I have above into a byte array, which I can then use to search the byte array of the whole file that I currently get as input.


Any help would be greatly appreciated
Thanks in advance,

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Can you show us the code which that error message applies to?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Welcome to JavaRanch

I can never remember how byte arrays work, but remember that 0xff is read as unsigned. If you are converting an int to a byte you have to cast it

int i = 0xff;
byte b = (byte) i;

In some instances you have to do a bitwise "AND" with 0xff. Note that arithmetic etc tends to convert everything to an int, so you might need a cast again.

byte b = (byte) (i & 0xff);

This is too difficult a question for "beginning"; moving to another forum. You might find somebody there who can actually remember how these arrays work
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Campbell Ritchie wrote:
If you are converting an int to a byte you have to cast it

int i = 0xff;
byte b = (byte) i;

In some instances you have to do a bitwise "AND" with 0xff. Note that arithmetic etc tends to convert everything to an int, so you might need a cast again.

byte b = (byte) (i & 0xff);


It is actually the other way around. Casting an int to a byte will just chop of the three high bytes. There is no reason to do the bitwise AND. On the other hand, casting a byte to an int (whether explicit or implicit) is done via "sign extension", so you should do the bitwise AND.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Thank you, Henry. I told you I had forgotten the details.
Mitch Robinson
Ranch Hand

Joined: Oct 29, 2009
Posts: 30
Thanks for the quick replies,

The error relates to this bit of code,the error is possible loss of precision
byte endPattern[] = {0x5a,0x00,0x10,0xD3,0xA9,0xCD,0x00,0x00,0x00,0xFF,0xFF,0x40,0x40,0x40,0x40,0x40,0x40};

I think this is due to some of the hex values being above 127 as a byte in java is a value between -127 and 127 but for example FF as decimal is 255.

Henry, if I cast these integers will they be converted correctly to the signed byte value between -127 and 127, the context is that I use this byte array as the pattern to search for in the byte array called data, which is basically the whole afp file read as a signed byte array.

Hope i'm being clear as this seems quite tricky to me.

Thanks again for the help
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Mitch Robinson wrote:if I cast these integers will they be converted correctly to the signed byte value between -127 and 127

-128 and 127, but yes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mitch Robinson
Ranch Hand

Joined: Oct 29, 2009
Posts: 30
Thanks for that so now I have my Int array int endPattern[] = {0x5a,0x00,0x10,0xD3,0xA9,0xCD,0x00,0x00,0x00,0xFF,0xFF,0x40,0x40,0x40,0x40,0x40,0x40};

cast into bytes, being relatively new to java I thought I could run this simple for loop to cast the ints to bytes with which to populate my byte array.

for (int j = 0; j<17; j++) {

int i = endPattern[j];
byte b = (byte) i;
byte array[j] = b;

}

It seems that putting the converted bytes into the byte array is more difficult than expected, is this due to it not knowing the size of the byte array so do I need to define this above?

Again thanks for the help so far, just trying to get my head around why it's so difficult to populate the byte array with bytes in the loop?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Remember you can get the size of an array from its length field.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble Converting from Int Array to Byte Array