Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

writing bytes (int val 0-255) to file

Thread view: 
chuck - 20 Oct 2007 06:39 GMT
Hello,

I am trying to write integer values, 0 - 255 to a file and am having problems.

I am having problems writing any value greater than 127 (out of ascii
range), i get random values.

the full test code is here
http://pastebin.ca/743174

ok, now what i am trying to do
I break up a java long variable into 4 bytes.  so first i convert the
value to a binary string with left padded 0s. (i will just show the
first byte)
Writer outfile = new FileWriter("testOutput");

String encryptedString = padString(Long.toBinaryString(value[i]), -32, "0");
String bs0 = encryptedString.substring(0,   7+1);
int eByte0 = Integer.parseInt( bs0, 2);
outfile.write( eByte0 );

here is an example
5955599
00000000010110101110000000001111
00000000 01011010 11100000 00001111
0 90 224 15

expected value in hex editor
00 5a e0 0f

actual value  value in hex editor
00 5A 88 0F

Any help is appreciated.
Chuck
Gordon Beaton - 20 Oct 2007 06:52 GMT
> Writer outfile = new FileWriter("testOutput");
[...]
> outfile.write( eByte0 );
>
[quoted text clipped - 11 lines]
>
> Any help is appreciated.

A FileWriter is for writing *text*.

Use an OutputStream (e.g. a FileOutputStream) to write binary data
such as raw bytes.

/gordon

--
Roedy Green - 20 Oct 2007 08:20 GMT
>I am having problems writing any value greater than 127 (out of ascii
>range), i get random values.

See http://mindprod.com/jgloss/unsigned.html

bytes are -127..+127.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lew - 20 Oct 2007 14:29 GMT
chuck <noemail@gmail.com> wrote, quoted or indirectly quoted someone who said :

>> I am having problems writing any value greater than 127 (out of ascii
>> range), i get random values.

> See http://mindprod.com/jgloss/unsigned.html
>
> bytes are -127..+127.

Also,
>> String encryptedString = padString(Long.toBinaryString(value[i]), -32, "0");
>> String bs0 = encryptedString.substring(0,   7+1);
>> int eByte0 = Integer.parseInt( bs0, 2);

Let's say value[i] is 33L.  Long.toBinaryString() of that returns "100001".
You failed to show us padString(), but we'll assume that the result of that
operation is
"0000000000000000000000100001" (not long enough to represent a Long, BTW).

Now you parseInt() that String in base 2, which should yield the int value
corresponding to the binary string, which is the original value of 'value[i]'
if the latter is within the int range, an error if it's not.

Despite the name, eByte0 is an int.

So if value[i] is, say, 1023, then eByte0 when it's done will be 1023.

>> outfile.write( eByte0 );

will then write the string "1023" to the file.

You might as well have just said, "outfile.write( value[i] )".  That at least
would handle the long values larger than Integer.MAX_VALUE.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.