Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Can anyone point me in the right direction dealing with bytes

  1. #1
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Can anyone point me in the right direction dealing with bytes

    does anyone know any good tutorials or information on working with bytes like taking a file putting the bytes into an array, searching through the bytes, displaying them etc? any info of this kind is greatly appreciated thanks

    also anything to do with signed and unsigned integers and such
    Last edited by derekxec; August 14th, 2012 at 03:32 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Can anyone point me in the right direction dealing with bytes

    This will explain file I/O, this or this will explain how to search the array once it is loaded.

  3. The Following User Says Thank You to Tjstretch For This Useful Post:

    derekxec (August 18th, 2012)

  4. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Can anyone point me in the right direction dealing with bytes

    Quote Originally Posted by derekxec View Post
    ...also anything to do with signed and unsigned integers and such
    The bottom line is that the creators and maintainers of Java decided (waaaay back when) that it's just too confusing to have to explain the difference between treatment of signed integer data types and unsigned integer data types to new (and some not-so-new) programmers, so: In Java there are no unsigned integer data types. (Really: no unsigned integer fundamental data types.) Period. Full Stop.

    If you read (carefully) tutorial or other reference material you will see that the low-level read() functions that grab a single byte from a file return an int, not a byte. That way, an end-of-file condition (trying to read past the last byte in the file) can be indicated to the calling function by returning an integer value of -1, whereas all valid file byte values (0-255) are returned in the lower eight bits of the integer return value.

    That's a fine point (well, maybe not really a fine point, but it is fundamental) that somehow gets overlooked by lots of people. If the file just contains ASCII characters (0-127), it doesn't really make much difference, but when you are reading binary files (or any other kind of file that may contain byte values that would be greater than 127), it can make a huge difference in behavior.

    Also note...

    For the write() functions that put out a single character to the disk file, the argument when calling write() is an int. If you call it with a byte argument, the byte gets "promoted" to a signed int, but that's OK, since the function itself will ignore the upper 24 bits, and the lower eight bits will be sent to the file, as you undoubtedly intended.

    A simple non-file example:
    public class Z
    {
        public static void main(String [] args)
        {
            byte x = 127;
            ++x;
            int ix = (int)x;
            int iy = (int)x & 0xff;
            System.out.printf(" x = %d\n", x);
            System.out.printf("ix = %d\n", ix);
            System.out.printf("iy =  %d\n", iy);
        }
    }

    Output:



     x = -128
    ix = -128
    iy =  128


    Of course having bypassed any meaningful exposition about signed integer data types vis-à-vis unsigned integer data types, now someone has to explain why incrementing a byte value of 127 gives -128. Oh, well...



    Cheers!


    Z
    Last edited by Zaphod_b; August 14th, 2012 at 06:33 PM.

  5. The Following User Says Thank You to Zaphod_b For This Useful Post:

    derekxec (August 18th, 2012)

  6. #4
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Can anyone point me in the right direction dealing with bytes

    thanks a lot for that explaination seems working with bytes will be a bit harder than i thought guess would probably be easier in like C# or so but im going to give it a go anyway

  7. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Can anyone point me in the right direction dealing with bytes

    Just to clarify, there is only one unsigned data-type in Java: char (well, I suppose you could consider boolean to be unsigned). It's a 16-bit unsigned variable.

    Also, it's possible to perform some operations on signed data and have the results be the same as if it were unsigned. These operations are addition, subtraction, and all bit-wise operators except >> (signed shift right). Displaying the correct value may be tricky, but it can be done using bitwise operators instead of casting:

    // display an "unsigned byte"
    byte val = -100;
    short display = 0xFF & val;
    System.out.println(display); // prints 156, the unsigned bit equivalent of -100
    Last edited by helloworld922; August 18th, 2012 at 12:39 PM.

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    derekxec (August 18th, 2012)

  9. #6
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Can anyone point me in the right direction dealing with bytes

    ill be decrypting and encrypting with xor so guess thats a plus then...i can do what i want manually but translating that into a program that does it for me is proving to be a little more tricky...the values go higher than 127 which makes it a bit more pain lol

Similar Threads

  1. Have trouble dealing with negative numbers.
    By Arick Cheng in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 29th, 2012, 07:41 AM
  2. HI WORLD! Can anyone point a complete novice in the right direction??
    By Irish-novice in forum Member Introductions
    Replies: 4
    Last Post: June 26th, 2012, 05:16 PM
  3. Replies: 1
    Last Post: April 14th, 2012, 01:45 PM
  4. Help with code dealing with parallel arrays.
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2011, 07:43 PM
  5. [SOLVED] Someone point me to the right direction..
    By ineedhelp in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: June 30th, 2011, 10:03 PM