Results 1 to 13 of 13

Thread: Steer Clear, Binary in here

  1. #1
    Commander Keen
    Join Date
    Nov 2003
    Location
    217.27.240.214
    Posts
    624
    Thanks
    0
    Thanked
    0 times in 0 posts

    Steer Clear, Binary in here

    Feeling like a dumb ass. But thinkning in binary has never been my strong suit. The problem is I have to write a java prog that spits out a series of bytes - to "AN" output stream. This is fine. WriteBytes(x) would suffice. My problem is getting the correct definition of data inside x.

    x is probably going to be a byte array (or whatever if you guys can suggest something better). x[0] has to be = 0x47 in hex. So this part is fine. The place where - for some reason - my head waves a flag is at x[1]. At this point I need to define only 3 binary digits - which are flags. Then the start of a 13 bit address (so only the first 5 bits of this are in x[1] then rest are in x[2]).

    How ?? Do you write in binary in java ? It really appears as though it is hidden VERY well.

  2. #2
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    Have a look at the bit shift operators (>>>,>> and <) for moving bits around and and the bitwise OR operator( |) for combining values.

  3. #3
    Commander Keen
    Join Date
    Nov 2003
    Location
    217.27.240.214
    Posts
    624
    Thanks
    0
    Thanked
    0 times in 0 posts
    I have played about with those bitwise operators a 'bit' back (excuse that little pun, keeps me amused).

    I believe my thinking has become too obscure for this problem... I know that in my mind I think of the data as 3 bits (flags) followed by 13 bits (addressing). What this equates to is 2 entries in the byte array (3+13 = 16). So I just don't know what to do because the flags are variable.

    How to combine these ?

    I have knocked up some code for printing out a string representation of a given byte or integer. So I can see how the shifting moves the 1's....

  4. #4
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    Heres an example, syntax may be incorrect because I'm a c++ programmer not a Java programmer...

    short sAddress = 0x1234; // Var to hold 13 bit address just fill bottom 13 bits)
    short sBytes = 0; // var to hold 2 bytes to write to stream

    // Copy the address to the bottom 13 bits
    sBytes = nAddress & 0x1FFF;

    // Set each flag to 1 as necessary, omit for 0
    sBytes |= 0x2000 // Set bit 13 to 1
    sBytes |= 0x4000 // Set bit 14 to 1
    sBytes |= 0x8000 // Set bit 15 to 1

    // Append the data to the stream object
    DataOutputStream dos;
    dos.writeByte(0x47);
    dos.writeShort(sBytes);

    .. keep on appending the bytes, shorts or ints until you have finished.

    Obviously you dont need sAddress and sBytes, I did this to make it easier to comprehend.

    Mart

  5. #5
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    Now without the typos...

    Heres an example, syntax may be incorrect because I'm a c++ programmer not a Java programmer...

    short sAddress = 0x1234; // Var to hold 13 bit address just fill bottom 13 bits
    short sBytes = 0; // var to hold 2 bytes to write to stream

    // Copy the address to the bottom 13 bits
    sBytes = sAddress & 0x1FFF;

    // Set each flag to 1 as necessary, omit for 0
    sBytes |= 0x2000 // Set bit 13 to 1
    sBytes |= 0x4000 // Set bit 14 to 1
    sBytes |= 0x8000 // Set bit 15 to 1

    // Append the data to the stream object
    DataOutputStream dos;
    dos.writeByte(0x47);
    dos.writeShort(sBytes);

    .. keep on appending the bytes, shorts or ints until you have finished.

    Obviously you dont need sAddress and sBytes, I did this to make it easier to comprehend.

    Mart

  6. #6
    Commander Keen
    Join Date
    Nov 2003
    Location
    217.27.240.214
    Posts
    624
    Thanks
    0
    Thanked
    0 times in 0 posts
    I hacked that code into java... flung it through my lovely numbers to String method... And it pumped out this. Assuming that sAddress = 0x1234.

    00000000 00000000 00000000 01000111 //0x47
    00000000 00000000 00010010 00110100 //sAddress & 0x1FFF
    00000000 00000000 00110010 00110100 //sBytes | 0x2000
    00000000 00000000 01110010 00110100 //sBytes | 0x4000
    00000000 00000000 11110010 00110100 //sBytes | 0x8000

    Whenever I am satisfied with what I get... I will trust it to just write the bytes somewhere. But I personally can't read those lovely object memory addresses that get spat out... They are not for human consumption. Just the JVM.

    p.s. you can edit your messages on these boards ! Rather than make another post... bottom right corner of your 'box'
    Last edited by killgORE; 20-11-2003 at 11:32 PM.

  7. #7
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    Your last post is a little confusing? The binary looks fine to me.

    I'm not trying to be funny but do you understand how to convert from hex to binary + vice versa?

    Mart
    Last edited by Mart; 21-11-2003 at 12:48 AM.

  8. #8
    Commander Keen
    Join Date
    Nov 2003
    Location
    217.27.240.214
    Posts
    624
    Thanks
    0
    Thanked
    0 times in 0 posts
    yes.... That was just for your information ! I am at a point where I should be able to run with this prog... It is nothing major.

    My proper project is the reverse of the problem. Where I read in this data (2,805,000 188byte packets worth) and perform some tricks and magic.

    What made you think i can't read binary... is it this ?
    " I personally can't read those lovely object memory addresses that get spat out.."

    Java spits out its horrible representation of an object if you ask it to.

  9. #9
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    Yep " I personally can't read those lovely object memory addresses that get spat out.." was the one.

    Good to hear your sorted now.

    Thanks for the edit tip. I did try to edit my post by pressing the button above my post couldn't understand why I was getting permission denied...doh!

    Mart

  10. #10
    bored out of my tiny mind malfunction's Avatar
    Join Date
    Jul 2003
    Location
    Lurking
    Posts
    3,923
    Thanks
    191
    Thanked
    187 times in 163 posts
    • malfunction's system
      • Motherboard:
      • Gigabyte G1.Sniper (with daft heatsinks and annoying Killer NIC)
      • CPU:
      • Xeon X5670 (6 core LGA 1366) @ 4.4GHz
      • Memory:
      • 48GB DDR3 1600 (6 * 8GB)
      • Storage:
      • 1TB 840 Evo + 1TB 850 Evo
      • Graphics card(s):
      • 290X
      • PSU:
      • Antec True Power New 750W
      • Case:
      • Cooltek W2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Dell U2715H
    Originally posted by Mart
    Yep " I personally can't read those lovely object memory addresses that get spat out.." was the one.

    Good to hear your sorted now.

    Thanks for the edit tip. I did try to edit my post by pressing the button above my post couldn't understand why I was getting permission denied...doh!

    Mart
    You need to use the edit button *below* your post.

  11. #11
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    Yep, I figured that out hence the doh! Its taken me long enough though

  12. #12
    bored out of my tiny mind malfunction's Avatar
    Join Date
    Jul 2003
    Location
    Lurking
    Posts
    3,923
    Thanks
    191
    Thanked
    187 times in 163 posts
    • malfunction's system
      • Motherboard:
      • Gigabyte G1.Sniper (with daft heatsinks and annoying Killer NIC)
      • CPU:
      • Xeon X5670 (6 core LGA 1366) @ 4.4GHz
      • Memory:
      • 48GB DDR3 1600 (6 * 8GB)
      • Storage:
      • 1TB 840 Evo + 1TB 850 Evo
      • Graphics card(s):
      • 290X
      • PSU:
      • Antec True Power New 750W
      • Case:
      • Cooltek W2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Dell U2715H
    Originally posted by Mart
    Yep, I figured that out hence the doh! Its taken me long enough though
    Erm... Well allow me to say:

    Doh!

    too...

  13. #13
    Commander Keen
    Join Date
    Nov 2003
    Location
    217.27.240.214
    Posts
    624
    Thanks
    0
    Thanked
    0 times in 0 posts
    D'ho.... Like a pair of clean undies. I felt left out.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •