Byte to bit and bit to byte conversion

Hello,
Does anyone know how to covert a byte into an array of booleans and vice versa? I think it has something to do with bitwise operations like >>.
Thanks.

If this right:
    public static byte bitsToByte(boolean[] bits)
          return bitsToByte(bits, 0);
    public static byte bitsToByte(boolean[] bits, int offset)
          int value = 0;
        for (int i = 0; i < 8; i++)
               if(bits[i] == true)
                    value = value | (1 << i);
        return (byte)value;
     }

Similar Messages

  • Byte [] to Object and Object to byte[]

    Hi,
    I am quite new to java and need some help. My question is how can I convert an Object to byte [] and convert back from byte [] to Object.
    I was thinking of something like:
    From object to byte []
    byte [] object_byte=object.toString().getBytes();
    When I try to do:
    String obj_string=object_byte.toString();
    But obj_string is not even equal to object.toString().
    Any suggestions?
    Thanks

    well nnneil, if you try to convert and Object into a byte[] in that way you only convert the string representation of the object in bytes.
    If you remenber an object is more than a simple bytes stream, an object have Class, Methods, Attributes and other things, if you wanna, you can use serializable to convert your object in a bytes stream.
    Or if your problem if pass something like {0x01,0x02,0x0f,...} in an object you can use a simple casting over your variable.
    try with this:
    public class Main
    public static void main(String[] args)
    try
    byte[] bs = {0x00,0x00,0x00};
    Object o = (Object)bs;
    System.out.print("Object:[" + o.toString() + "]");
    System.out.print("<- That does not equals to ->");
    int i = 0;
    while(i < bs.length)
    System.out.print("[" + bs[i++] + "]");
    catch(Exception e){
    e.printStackTrace();
    output is: Object:[[B@df6ccd]<- That does not equals to ->[0][0][0]
    if you see [B@df6ccd is the internal represent of bs object.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Help !! Bits and Bytes - Come get these Dukes

    Can someone please shed some light on this ... I come from a C background and had no problems reading files in bits. I can't seem to find a way to read bit by bit in Java.
    I have a file which is written in an unknown format. All I have been told is that it could be either 4 / 5 / 6 etc bits per byte.
    I want to write a program that can create the above byte sizes and then print the ascii equivilent.
    So any help or an example on how to read a file bit by bit would be most appreciated.
    Robin

    I come from a C background and had no problems reading files in bitsInteresting!
    I can't seem to find a way to read bit by bit in Java.That's because there isn't any bit reader in Java but there is nothing to stop you from reading a byte at a time and pick the bits you want..... It seems to me you're not going to get very far without having more info on what's in the file. BigEndian/LittleEndian could compound your problem.
    Have fun!
    ;o)
    V.V.

  • Bytes and bits in Lookout

    This is for a Lookout 5.0 application. The OPC Client Object reads a integer value (between 0 and 32767 plus a sign bit) that represents a 16 bit word. What I want is knowing the integer value, unpack the word into 16 bits. How can I do this in Lookout?"

    I needed to do this and found a quick, mathematical way. Here it is:
    Lets say you have a 16 bit word with Bit#1 being the least significant bit- LSB- (farthest right) and Bit #16 being the most significant bit -MSB- (farthest left). As I am sure you know each bit has a decimal equivalent in value: Bit #1=1; Bit #2=2, Bit #3=4; Bit #4=8....Bit #15=16384, Bit#16=32768. The formual for a bit's value in decimal is (assuming the LSB satrts at 1 and not zero) 2^(Bit# - 1).
    So to parse a 16 bit word here is the formula to use in an expression:
    mod(int(Word/2^(Bit# - 1)),2) Ofcourse LSB bit# =1
    Since the 16th bit is a sign bit you only need to look at the word's sign:
    If(Word<0,1,0)
    Regards,
    Tommy Scharmann

  • Binary file read with bit offset..not byte offset.

    hi,
               i want to read a binary file with some offset ..not in bytes...  offset in bits..my data is a single bit data stream size of 1GB ,after skipping particular bit offset i want to read the data in byte order..
    Indrajit
    | [email protected] | [email protected] .

    hi , Gerd W 
                       i am not able to follow your answer.... here with i attached a file to describe my query more clearly.   Actually two channels of  1 bit data stream stored in two different files ,and the file size will be more than 10 GB. 
     i want to delay the channel B by 3 bits and i want to perform bitwise operation between channel A and shifted version of channel B..till the end of the data..
    now i am doing this without applying any delay by choosing a small block by block which corresponds to 1 sec of data .
    Indrajit
    | [email protected] | [email protected] .
    Attachments:
    bitwsie.png ‏16 KB
    bitwsie.jpg ‏52 KB

  • Concat 4 8-bit bytes into 1 32-bit word

    I am just getting started up with LabView. I have an FPGA that is sending a signed (2's Comp) 32 bit vector to the PC serial port in four 8-bit chunks. I am having trouble figuring out how to recombine the four bytes into the original 32-bit word. It is a simple task in any programming language using concatenation but I am really having problems with this.
    Thanks
    Jeremy

    Two ways come to mind immediately.
    1) Go from a string (VISA returns a string from a read) to a byte array then loop and shift each byte the correct amount while OR'ing the result with that from each loop iteration.
    2) Use the join numbers vi to combine multiple bytes into a single 32-bit word.
    I've attached a vi that demonstrates these two solutions.
    Attachments:
    MergeBytesExample.vi ‏28 KB

  • Byte data extraction and interpretation from a WAVE sound file

    Hello,
    I'm trying to extract the signal data from a WAVE-file for further processing. Suppose the WAVE-file is in 16 bit/stereo format - then I get (8 bit) low-byte and (8 bit) high-byte for the left channel first, and then the low- and high-order bytes for the right channel (I hope I've got it right this far).
    Now since I'm interested in doing some frequency analysis on the signal, I need to have the amplitudes for every sample, but since each byte is signed I can't just compose the complete amplitude value from the two bytes for each channel. A simple way to solve this would be (I think) to check if a byte is negative, and if it is, store the absolute value of the byte + 127 in an integer. For example, the byte 11111111 would be interpreted as -128, but taking the absolute value and adding 127 produces 255 which would be the maximum "unsigned" value for an 8 bit binary number.
    This seems like a quite "ugly" solution to me, and I don't really like ugly things. I haven't worked that much with binary math in Java, and my general knowledge on the subject is not very thorough in the first place. I would be very grateful for any suggestions.

    Ok, I went to buy some food and came up with another solution:
    Suppose I have a sample from one of the channels in my byte array "signal" and I want to compose a (signed) total amplitude of the two bytes in the array. Code:
    byte[] signal = ... ; //Get the data
    int amp = 0x0000;
    amp = ((signal[1] & 0xFF) << 8) | (signal[0] & 0xFF);
    Here I assume that the data is aquired in little-endian format. If you have a better solution, please let me know.

  • How do I swap 64-bit and 32-bit floats from little-endian to big-ending

    I am trying to read a file that could contain a list of 64-bit floats or 32-bit floats that were written on a PC so they are little-endian.
    I need to convert the float values to big-endian so that I can process the values. I know that straight swapping each byte with the adjacent byte doesn't work (especially since their floating point values). I've tried swapping them end for end (i.e., byte 15 from the file becomes byte 0 in my array) and that didn't work either.
    I know that if I were to read the little-endian float into the big-endian float type (float or double) that the format is pretty much lost (from little I understand about how floating point values are stored in memory).
    So, what I need is a way to read in a series of little-endian floating point values (64-bit and 32-bit) into a big-endian array of floating point values.
    Anyone have any ideas on how to do this? Any help would be much appreciated.

    A 64-bit double is represented by the sign bit, an 11-bit (biased) exponent
    followed by a 52-bit mantissa. Both x86 and SPARC use the exact same
    representation for 64-bit double. The only difference is the endianness
    when stored to memory, as you observed.
    A 32-bit float is represented by the sign bit, an 8-bit (biased) exponent
    followed by a 23-bit mantissa. Again, both x86 and SPARC use the exact
    same representation for 32-bit float modulo endianness.
    As you can see, a 64-bit double is not merely a pair of 32-bit float.
    You need to know exactly how the floating-point data was written:
    if a 32-bit float was written, you must endian-swap it as a 32-bit float;
    if a 64-bit double was written, you must endian-swap it as a 64-bit double.

  • Separating 16 bits of data into one 12 bit and one 4 bit channel

    I have 16 bits of binary data and i'm trying to split these into one 12 bit and one 4 bit channel. I know that I could use something like:
    Dim Channel1 : Set Channel1 = Block.Channels.Add("Channel1", eI16)
    Channel1.Formatter.Bitmask = 4,095 'xxxx 1111 1111 1111
    but, i'm not sure how to grab those 4 bits out of the 16 bit channel1 and make channel2. It's like i almost need to say something like:
    Dim Channel2 : Set Channel2 = Block.Channels.Add("Channel2", eI16)
    Channel2 = Channel1.Formatter.Bitmask = 61,440 '1111 xxxx xxxx xxxx
    Can anyone help out???? thanks.

    OK...This is still straightforward. Her is what you want to do :
    CurrPos = File.Position ' remember where the block starts
    Set Block = File.GetBinaryBlock()
    ' set up all channels which can be read directly
    Dim Channel1 : Set Channel1 = Block.Channels.Add("Channel1", eU16)
    Dim Channel2 : Set Channel2 = Block.Channels.Add("Channel2", eByte)
    Dim ChannelPH : Set ChannelPH = Block.Channels.Add("Dummy", eU16) ' just a placeholder
    Dim Channel5 : Set Channel5 = Block.Channels.Add("Channel5", eU16)
    Dim Channel6 : Set Channel6 = Block.Channels.Add("Channel6", eByte)
    ' Now we need a block which has one channel only. This channel will be used to extract the bits.
    ' The block needs to start where the third schannel of the above block starts :
    File.Position = CurrPos + 2+1 ' 2 for the U16 channel, 1 for the byte channel
    Set Block = File.GetBinaryBlock()
    ' Set the scan width. This defines the gaps between the values of a channel
    Block.BlockWidth = 2+1+2+2+1 ' U16 + Byte + U16 + U16 + Byte
    Dim Channel3 : Set Channel3 = Block.Channels.Add("Channel3", eU16)
    ' .. apply bitmask and scaling (see my last example)
    ' Do the same for the second channel
    Set Block = File.GetBinaryBlock()
    ' Set the scan width. This defines the gaps between the values of a channel
    Block.BlockWidth = 2+1+2+2+1 ' U16 + Byte + U16 + U16 + Byte
    Dim Channel4 : Set Channel4 = Block.Channels.Add("Channel4", eU16)
    .. apply bitmask and scaling (see my last example)
    Do the same for the second channel
    I hope this works as expected. Let me know if you need more help...
    Andreas

  • Best way to combine 8-bit and 16-bit image files?

    I produce large photographic art prints by combining my own edited 16-bit RAW photo images with 8-bit stock photo images and 8-bit photoshop artwork. I used to just reduce my 16-bit photos to 8-bit and then edit my composite image as 8-bit, but I ran into banding and other problems in the color gradients I created later in my composite image.
    I want my composite image to be 16-bit to get more accurate colors and better gradients. What is the best way to bring 8-bit image files into my composition and maintain best quality? Is it good to convert 8-bit images to 16-bit in PS before importing? I also heard about bringing 8-bit images in as Smart Objects? Thanks for any help on this.

    *Photonic wrote:
    Thanks Jack and Noel. My workflow usually starts w. a 16-bit RAW image brought into Photoshop as the background layer of the master file. Color space is AdobeRGB. Then 8-bit stock photo and art elements (objects) are brought in by dragging the open 8-bit image up to the master image file tab, and then dropping it back into the image window after the file opens.
    Should I use the "Paste In" command instead? I usually avoid copy/paste since some of my element files are usually over 100MB and that seems to challenge my Mac after a while.
    Could I convert the 8-bit image files prior to import, like Noel suggests (Image>Mode>16bits)?  Thanks.
    There is no need to convert 8bit open documents to 16bit mode Photoshop will convert the copied layers to 16bit bit during the drag copy.  Converting the open document would just increase Photoshop RAM usage every layer pixels bytes used  would double in size.
    If you element files are over 100MB they are most likely layered files.  If all you need is a composite you should avoid using Place for that will needlessly inflate your document size. Instead open the element file create a new layer that is a stamp visible layer (Shift+Alt|Opt+Ctrl|Cmd+E) and drag that layer to you document then close nosave your element file. Copy only copies the targeted layer or there is a Copy Merge that copies the composit merge of all targeted layers. It a bit hard to create an action that would be able to use Copy or Copy Merge. Using the above stamp visible way can be made into an action. You also do not want to drag all the layers to your document if you just need a composit layer.
    You could also automate the element file copy with an action.
    Action Step 1 Insert menu into File>Open this will be an interactive step you must select a file to open. When the action is played.
    Step 2 (Shift+Alt|Opt+Ctrl|Cmd+N) Create a new empty layer This will always work
    Step 3 (Shift+Alt|Opt+Ctrl|Cmd+E) stamp visible layers into the new layer
    Step 4 (Ctrl|Cmd+A) select all
    Step 5 (Ctrl|Cmd+C) Copy
    Step 6 Close current document no save
    Step 7 (Ctrl|Cmd+P) Paste into the document your working on Photoshop will convert to 16bit if the document is 16bit mode and the clipboard content is 8bit.
    When you use Photoshop and do something.  Think to yourself how did Photoshop do what you asked it to do. Thing about what is involved what layers must be used etc. How is the feature/function done.
    Message was edited by: JJMack
    Message was edited by: JJMack

  • Binary, Bits and Hexidecimal

    I am developing code to decode handphone PDU.
    I need to decode the hexidecimal in the PDU into binary bits first.
    Then I need to take every 7 bits and form them into a character.
    E.g. A201 will give 1010 0010 0000 0001
    so the code will have to return 0100010 as ASCII for the first character, 1100000 as ASCII for the second character.
    How can I go about doing that in the most efficient method.
    I was thinking about converting to string first (0's and 1's), then manually extract out every 7 bits and do the processing. But that looks quite tedious.
    Can anybody comment on this. Thanks!

    Can't see where the 1100000 comes from.
    Have you really got characters on 7 bit boundaries? That would be very unusual and inconvenient.
    This kind of bit manipulation is best tackled with shift and mask operations of which this will be a particularly tiresome example. You need to load your hex data into an array of, I would suggest, ints, loading two hex digits at a time. Work in blocks of 7 bytes (7 bytes becomes 8 characters).
    int[] codes = new int[7];
    String hex = "a7882312228823"; // say
    for(int i = 0; i < 7; i++)
         codes[i] = Integer.parseInt(hex.substring(2 * i, 2 * i + 2);Then you need to extract the characters by combining the appropriate bits from each byte of data.
    char output = new  char[8[;
    output[0] = (codes[0] >> 1);   // first 7 bits of code 0
    output[1] = (codes[0] << 7) | (codes[1] >> 2); // bottom bit of first and 6 bits from second
    output[2] = (codes[1] << 6) | (codes[2] >> 3); // bottom 2 bits of second and top 5 from third
    ... etc ...Of course it's a lot messier than this because your data might not be a multiple of 7 bytes and the data supply is obviously more complicated.

  • What are the best data and bit rate setting for uploading from final cut express to Youtube?

    Can anyone suggest the best data rate and bit rate presets for uploading footage from final cut express 4 to Youtube? What settings will provide the best resolution, quality, and match the current youtube requirements?
    Thank you in advance for your help,
    Susan Kayne

    It depends on whether you are using aspect ratios of 4:3 or 16:9.
    Below is some simple guidance that will provide good quality with reasonably small file sizes.
    The first part is for 4:3 video:-
    1. File>Export Using QT Conversion.
    2. The "Format" window should say, "QT Movie".
    3. In "Use" select "LAN/Intranet" from the dropdown menu.
    4. Click "Save" and when it has finished encoding, upload it to YouTube.
    If you are making 16:9 video (Standard or High Definition) do steps 1 to 3 above.
    Then when you have selected "LAN/Intranet" press the "Options" button and in the new
    window that opens press the  "Size"  button and change the  "640x480" to  "853x480"
    To do this you will have to click on the  640x480 and a dropdown menu appears.
    Select "Custom" from  the bottom of the menu and in the window that opens
    you will see 2 boxes.
    Put  853  in the first box and  480  in the second.
    Click OK.
    Then Save it.

  • Still major problems syncing with iTunes 9.1.1, Windows 7, 64-bit and iPhon

    Downloaded the iTunes, 64-bit, 9.1.1 and installed from scratch. Attached my iPhone 3GS, 16GB with 8,75 GB free space!!! Still after three and a half hours it is still syncronizing, and have only completed two thirds... This is really below good enough, Apple!

    same crap here and i only have 3gbs of space used out of a 16gb iphone 3gs...never a lick of problems until now.. i don't bother with music under 256 so i wouldn't dream of cheacking 128 conversion for that lousy sounding stuff.
    windows 7 professional 64 bits and no issues at all until last update which added ipad...i wish i could go back but what a hassle as i keep my music on another drive and use pointers so not wanting to rebuild library other wise i would go back i have older version. today i did update as i have 2 issues slow syncing and BONJOUR!!!!
    the bonjour will fill up event viewer with errors so i leave it disabled in services... no need for it but this shouldn't be happening.
    what a clunker apple did to rush this out for lousy ipad. **** lucky i like phone otherwise i would blow apples software off for another phone.

  • Creation of Add-on package for 64 bit and 32 bit SAP Business One Client

    Please help me creating package for 64 bit and 32 bit SAP Business One. If Add-on executable is compiled with x86 option then there is no issue of connecting Add-on with 32 bit SAP Business one and if Add-on executable is compiled with Any CPU option then there is no issue of connecting Add-on with 64 bit SAP Business one. Problems are mentioned as below
    1. My Add-on uses MS ACCESS connection and the connection is read using Jet OLEDB 4.0 provider. It works fine for x86 compile mode but not when compile mode is Any CPU. I searched for the issue on Google and found a solution to change MS ACCESS connection provider to ACE.OLEDB.12.0 but again ACE provider is installed according to the Office version i.e. 32 bit or 64 bit office suite. If I install 32 bit ACE provider then Add-on executable compiled with x86 version is working fine but with Any CPU option throws error as provider is not registered on current machine.
    2. Secodly, our license validation activex dll is build using vb 6.0 and therefore when Add-on executable is compiled using Any CPU option, Add-on EXE cannot read from license dll. Any suggestion, how the activeX dll can be compiled whose component can be created from for X86 compiled exe and any cpu compiled exe.
    Please help because we are stuck in this issue and cannot move forward.
    Thanks.

    Hi Alejandro,
    That is one solution but did your add on used ms access connection? if you used Jet oledb to connection string or ace oledb connection string? what if 32 bit office is installed and what if 64 office is installed? how did you found the office version in install script? after determining the office version how did you installed Microsoft redistributable for ace oledb (32 bit or 64 bi )?
    I am sorry that I asked many questions but these are the problems where I am stuck with the addon installer?
    Thanks

  • Install sqldeveloper on windows 7 64 Bit and Oracle 11.2

    Hi,
    how can I find the rigth jdk. I installed JDK on C:\Program Files\Java\jdk1.6.0_23.
    I installed Oracle 11.2 including sqldeveloper. But I cannot find the correct folder to start sqldeveloper.
    Where can I find this path?
    When I include a path with java.exe I get the error "Unable to find a Java Virtual Machine. To point to a locacion of a Java Virtual Machine, pleaser refer to the Oracle9i Jdeveloper Install guide (jdev\install.html)" Where can I find this?
    Thank you for your help
    Siegwin

    I am on Windows 7 64 bit. I see that installing Oracle 11.2 database installs SQL Developer also but that doesn't work. I get the .bat file not found crap. What I then did was do a seperate install of SQL Developer for 64 bit, but that does NOT install the jdk.
    I then did the java install from:
    http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u25-download-346242.html
    I selected: jdk-6u25-windows-x64.exe
    I then point to the java.exe where this install loaded it (c:\program files\Java\jdk1.6.0_25\bin when I start up sql developer and it still gives the same error. I delete the SetJavaHome value in the sqldeveloper.conf file and try again over and over but it still gives the same error.
    So, I have windows 7 - 64 bit operating system. I have Oracle 11.2 database installed. I have a seperate installation of SQL Developer 64 bit and I have the java jdk for 64 bit installed.
    Nothing is working. Can anyone please steer me in the correct direction? Should I delete the second install of SQL Developer 64 bit and to an install of SQL Developer 32 bit?
    Please help

Maybe you are looking for

  • [SOLVED] Grub Problems Can boot Arch w/ grub edits only

    I had 3 Linux OS's installed on this computer, Arch and 2 versions of Ubuntu on one hard drive. The MBR belonged to Ubuntu, using grub2. I decided to get rid of the older Ubuntu (sda8 and 9) and add it's HDD space to my Arch home (sda12). Used the cu

  • Can't Open Some PDFs

    I have the most recent version of Adobe Reader and can't open/read some PDFs that I could open with OS10.6.

  • Stock Audit Report vs Balance Sheet

    I am having problems matching the balance sheet to our stock. I presume that the stock audit report is the best report for evaluating our stock but this does not match our balance sheet. The balance sheet stock looks at 3 accounts, Finished Product,

  • How can I lock the file in use

    Dear All, I transfer the large text file to Solaris 10 server and copy the this file with transfer not success to another path. but I can open the file with end of file. How can I lock the file with transfer not successfully.

  • Problems with populating Drop Down List (WD ABAP)

    Hi, I am trying to populate two Drop Down fields CARRID and CONNID (Type Table SPFLI) on an Adobe Interactive Form in a Web Dynpro ABAP Application. In the WD Context I have a node "Flights" with those attributes. In the WDDOINIT I populate the Conte