• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trying to get a byte[] length using covered quotient

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm trying to read from a text file into a byte[] array, and then convert that byte array into hex code.
The problem i have is assigning the length of my array. At the moment my code looks as such;
----------------------------------------
FileInputStream fis = new FileInputStream(file);
// R E A D
byte[] ba = new byte[1024];
// -1 means eof
int bytesRead;
bytesRead = fis.read( ba, 0 /* offset in ba */, ba.length /* bytes to read */ );
return ba;
}
-------------------------------------------
i've set my byte to the value of 1024. Which gives me alot of zeros, but i get the right hex input into the start of the array.
i need to make the length only as long as the next greatest power of 64bit (8 bytes). (and pad to complete the fields)
The covered quotient looks as follows
(dividend >= 0) ? ( (dividend + divisor - 1 ) / divisor) : (dividend / divisor );
however i do not know how to implement that ..
Any help would be much appreciated. .
John
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I notice that you open the file with a file object. That file object has a length () method you can use to set the length of your array.
 
John Gable
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately i cannot .. As i'm converting to a hex string the file.length() is too small .. I originally had it as that .. and only approx half of the file would be read into the byte array . .
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ummm, that doesn't make sense to me. The hex conversion is something you do after reading the bytes, right? You should be able to read all the bytes into an appropriately-sized array, and then convert to hex.
Are you aware that the read() method is not guaranteed to read all the bytes you request, and therefore you should usually read in a loop, checking return values as you go? E.g.
 
John Gable
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, i'm unsure where you attained buffer from ??
should it be filename instead ? ?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, "bytes" and "buffer" were supposed to be the same thing. If you look at the API for read(), there's only one version that takes three arguments, and the first argument has to be a byte[]. So there's no way filename would be involved.
 
Danger, 10,000 volts, very electic .... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic