Byte into bit array?

I want to implement a little compression programm, and I have to convert the byte array, in which I read a file into a bit array, because I have to be able to change single bits in that array.
1.Is there a wrapper class?
2.How many bits does the class Byte contain?
If it is 8, then i can convert a single byte into 8 bit by using a simple algorithm, right?
The byte simply displays it�s value (0-255)in hex, right?
thanks for your reply!
tim

There is a class named BitSet that seems to be close to what you are looking for (at least in SDK 1.3). But if you can manipulate bytes, it might be more efficient to write your own code.

Similar Messages

  • Is there an easier way to convert bytes into bit(boolean) arrays?

    I am currently using this method to convert bytes into bit arrays:
    /*convert byte to int such that it is between 0-255 this is the bytes[] array
                        if ((bytes/128)==1)
                             bit[c+0]=true;
                        else
                             bit[c+0]=false;
                        if ((bytes[i]-bitInt[c+0]*128)/64==1)
                             bit[c+1]=true;
                        else
                             bit[c+1]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64)/32==1)
                             bit[c+2]=true;
                        else
                             bit[c+2]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32)/16==1)
                             bit[c+3]=true;
                        else
                             bit[c+3]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16)/8==1)
                             bit[c+4]=true;
                        else
                             bit[c+4]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8)/4==1)
                             bit[c+5]=true;
                        else
                             bit[c+5]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8-bitInt[c+5]*4)/2==1)
                             bit[c+6]=true;
                        else
                             bit[c+6]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8-bitInt[c+5]*4-bitInt[c+6]*2)==1)
                             bit[c+7]=true;
                        else
                             bit[c+7]= false;

    You can loop through and use a bitwise operator instead. Here is an example without the loop.
    byte b = 6 ;
    if( b & Math.pow( 2, 0 ) == Math.pow( 2, 0 ) ) ;
    // the 2^0 bit is on
    if( b & Math.pow( 2, 1 ) == Math.pow( 2, 1 ) ) ;
    // the 2^1 bit is onetc...
    You should get something like 110 when you're done.
    If you're wonder what & does (no, its not a boolean &&), it takes the bits in the two numbers you give it and returns a number with all the bits on that are on for each of them.
    For example:
    10011011 &
    11001101 =
    10001001
    So if we take
    110 (6) &
    010 (2^1, or 2) =
    010 (2 again)
    Which means that the number (6) has the 2^1 bit on.

  • 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;
         }

  • How to copy bytes to an array byte variable....??

    Hi everyone....can anyone help me out...i want to store byte into an array-byte variable,(byte by byte), which are returning from the readByte() function of DataInputStream.
    Thank you

    int filesize = request.getContentLength();
    byte dataBytes[] = new byte[filesize];
    int read=0;
    while( read <filesize)
    bytesRead[read++] =  in.readByte();  // *in* is object of DataInputStream
    }Change all that to this:
    int count;
    byte[] buffer = newbyte[8192];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while ((count = in.read(buffer)) > 0)
      baos.write(buffer, 0, count);
    byte[] data = baos.toByteArray();Of course, like your code, this assumes that the data will fit into memory. It is preferable to process the data read each time as you go rather than accumulating it all first and then processing. This saves both time and space.

  • How to convert an array of bits into an array of bytes?

    If I have an array of 1s and 0s, how can I pack them
    into an array of bytes (8 bits)?
    Thanks.

    NI recommends to get rid of 4.x mode (Quote from the help file: "National Instruments recommends reworking any application that uses the Convert 4.x Data mode as a long term solution."). For this reason, I have attached an alternative example to give you some ideas.
    (The previous example (using 4.x type cast) is actually slightly flawed, because for some reason it only produces even sized output arrays (why???) so you would need to trim the resulting array as a function of the input array size.)
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    OnesAndZeroesU8v2.vi ‏43 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

  • How can I convert an array off byte into an Object ?

    Hi folks...
    I�m developing an application that comunicates a PDA and a computer via Wi-Fi. I�m using a DataStream ( Input and Output ) to receive / send information from / to the computer. Most off the data received from him is in the byte[] type...
    How can I convert an array off byte ( byte[] ) into an Object using MIDP 2.0 / CLDC 1.1 ?
    I found on the web 2 functions that made this... but it uses a ObjectOutputStream and ObjectInputStream classes that is not provided by the J2ME plataform...
    How can I do this ?
    Waiting answers
    Rodrigo Kerkhoff

    There are no ObjectOutputStream and ObjectInputStream classes in CLDC. You must know what you are writing to and reading from the DataStream. You should write the primitives like int, String to the DataOutputstream at one end and read those in exactly the same sequence at the outher end using readInt(), readUTF() methods.

  • Getting file into an array of byte..

    Hi guys,
    i first thank you for all the help you've given in my precedent posts.
    Now i've to ask you a question.
    I need to convert some lines of a txt file(obtained from an excel table) into an array of byte in the best possible manner.
    My file can have a various number of rows and columns.
    With this code
    BufferedReader br = new BufferedReader(new InputStreamReader(myFile.getInputStream()));
    String line = null;
            while ((line = br.readLine()) != null) {
                    line = line.replace (',', '.');
                    StringTokenizer st = new StringTokenizer(line);
                    numberOfNumericColumns = (st.countTokens()-1);
                    col=(numberOfNumericColumns+1);I read the file, i change , with . and line to line i calculate the number of value(that have to be the same,i do the control for showing exception if necessary).
    Now i know from my file what are the number of values in a row(columns of my old excel table).
    I have to convert this file into an array of byte.
    How can i do it?
    Suppose we have only 3 lines, the first line is ever only string,the others lines have a string and other double values.....
    nome giuseppe carmine paolo
    valerio 23.3 34.3 43.3
    paolo 21.2 34.5 12.2
    each value is separated from another one by one or more whitespaces..
    The most important thing is that in a second moment i have to read this array of byte and showing its original content,so i have to put in the array some different value and different line symbol delimiter.
    Can you help me giving me some idea or code?(please be clear,i'm inexpert of java...)
    Thanks very much,i need your help
    Message was edited by:
    giubat

    thanks for your reply...
    my file has about 50000 rows..........and i have to develop an application that allows the upload of about 6000/7000 files....
    so i have to optimize everythings......i want to use whitespace to separate different value and ;different line
    so in my array of bytes i want
    nome(converted in byte) whitespace(in byte) giuseppe (in byte) whitespace(in byte)carmine(in byte) whitespace(in byte) paolo (in byte) ;(in byte);
    valerio(in byte) whitespace(in byte) 23.3 (in byte) whitespace(in byte) 34.3 (in byte) whitespace(in byte)43.3 (in byte) ;(in byte) etc.....So i should have an array of byte lower than the array of byte obtained converting each line as a string into bytes.......
    do you understand what's my problem?
    How can i create an array of byte without fixing its initial dimension that is able to grows up for each line read?
    Excuse for my poor english..
    In the past i've used a vector to temporary store the data while reading the file and at the end i copied the vector in the array of byte but when i do upload in my application i had java heap size error.
    So someone has suggested me to improve my storing method and i've decided to eliminate vector....
    can you help me......????
    please....i need your help..

  • Converting bytes to character array problem

    I am trying to convert a byte array into a character array, however no matter which Character Set i choose i am always getting the '?' character for some of the bytes which means that it was unable to convert the byte to a character.
    What i want is very simple. i.e. map the byte array to its Extended ASCII format which actually is no change in the bit represtation of the actual byte. But i cannot seem to do this in java.
    Does anyone have any idea how to get around this problem?

    Thanks for responding.
    I have however arrived at a solution. A little un-elegant but it seems to do the job.
    I am converting each byte into char manually by this algorithm:
              for (int i=0;i<compressedDataLength;i++)
                   if (((int) output)<0)
                        k=(127-((int) output[i]));
                        outputChr[i]=((char) k);
                   else
                        k=((int) output[i]);
                        outputChr[i]=((char) k);
                   System.out.println(k);
    where output is the byte array and outputChr is the character array

  • How can I get to read a large file and not break it up into bits

    Hi,
    How do I read a large file and not get the file cut into bits so each has its own beginning and ending.
    like
    1.aaa
    2.aaa
    3.aaa
    4....
    10.bbb
    11.bbb
    12.bbb
    13.bbb
    if the file was read on the line 11 and I wanted to read at 3 and then read again at 10.
    how do I specify the byte in the file of the large file since the read function has a read(byteb[],index,bytes to read);
    And it will only index in the array of bytes itself.
    Thanks
    San Htat

    tjacobs01 wrote:
    Peter__Lawrey wrote:
    Try RandomAccessFile. Not only do I hate RandomAccessFiles because of their inefficiency and limited use in today's computing world, The one dominated by small devices with SSD? Or the one dominated by large database servers and b-trees?
    I would also like to hate on the name 'RandomAccessFile' almost always, there's nothing 'random' about the access. I tend to think of the tens of thousands of databases users were found to have created in local drives in one previous employer's audit. Where's the company's mission-critical software? It's in some random Access file.
    Couldn't someone have come up with a better name, like NonlinearAccessFile? I guess the same goes for RAM to...Non-linear would imply access times other than O(N), but typically not constant, whereas RAM is nominally O(1), except it is highly optimised for consecutive access, as are spinning disk files, except RAM is fast in either direction.
    [one of these things is not like the other|http://www.tbray.org/ongoing/When/200x/2008/11/20/2008-Disk-Performance#p-11] silicon disks are much better at random access than rust disks and [Machine architecture|http://video.google.com/videoplay?docid=-4714369049736584770] at about 1:40 - RAM is much worse at random access than sequential.

  • Converting data pointed to by a pointer into an array

    The vi I use returns a pointer to a block of data. The data are
    unsigned 16-bit integers. Without using a for..do loop, how do I
    convert the data into an array for input into an intensity graph?
    (Note that I presently use a for...do loop that works by incrementing
    the pointer by 2 bytes each time. But it  takes much time and I
    want to eliminate using a loop in labview. I suspect built-in
    functions/using DLLs would be much faster. But I do not know how to go
    about doing it.)
    Thank you for your help.

    Hi Balakumar,
    > The vi I use returns a pointer to a block of data.
    If by "pointer" you mean a memory address, then I think you're DLL idea is right on track.  You could efficiently cast (sort of) the pointer into an array.
    The attached .zip contains some VIs that may give you some ideas.  In LabVIEW, open the application builder and load CVTDLL.bld.  It compiles CVTDLL.vi into CVTDLL.dll.  CVTDLLCaller calls the DLL - passing it the pointer and returning an array.  The magic will happen in the way the pointer parameter is passed from the VI to the DLL.  I think we need the CLF to think it's supposed to be passing a U32 value, where the DLL thinks it receiving a pointer to an array.  Then it's just a matter of passing the data back out.  There are a bunch of forum contributors who could get this right the first try - "smercurio" and "RolfK" come to mind.
    I hope this isn't yet another case where I'm way off track!
    cheers.
    When they give imbeciles handicap-parking, I won't have so far to walk!
    Attachments:
    CVTDLL.zip ‏20 KB

  • 8 bit Array onto 10 bit Array

    Hi
       I am trying to convert a array of U8 into a 10 bits array (take the first 8 bits then add the next 2 bits of the next byte then the remaning 6 bits with the next 4 bits and so on). The ram we are using is 8bit but the
    CCD image it actually 10 bit, i have attached a couple of jpegs to show what i have done and i was wandering weather there is a expert around to say if this is the most efficient way of doing this. I have a large
    arrray to convert and would like to make it as quick as possible.
    Thanks for any help Gary
    Attachments:
    Front Panel.jpg ‏57 KB
    Block Diagram.jpg ‏56 KB

    Gary,
    Reshaping an array of bits is quite easy and (imho) the block diagram code is much easier to understand.
    For arrays of a different size you will just have to calculate the first dimension for the resize operation. 
    Regards
    Anke 
    Message Edited by AnkeS on 11-19-2008 10:16 AM
    Attachments:
    8to10.png ‏4 KB

  • DSC - Write Trace - Bit Array or Logical

    Hi,
    we are trying to write digital signals to a citadel database. The polymorphic function "Write Trace.vi" has the types logical and bit array.
    First of all I have created a new database and tried to write 10 digital signals of type boolean to the database. Unfortunately I get an error: "SPW_WriteBool.vi:2". I only can write the data into the citadel database if I convert the boolean to 0 and 1 values and write them via the numeric type. But this causes an overhead of factor 32.
    Is there a sample vi of using the "Write Trace.vi" function of type Logical or Bit Array. I've noticed that the trace which I have created is a analog one of type double. In another database of our company I've found a discrete trace of type double. Is this the problem?
    Kind Regards
    Joachim

    Many thanks for your help. MAX would be a nice and easy way to view the data, by the way, I try to view them with the Mixed Signal Graph. I am very new to LabVIEW and I am fighting now in correct reading of my data. I have 32 digital channels - each value of them is packed in an U32. When making a loop of my example 2 times for each channel should contain 2 values. But how can I program that the first boolean of the U32 is part of digital channel 1 - second boolean is part of digital channel 2 and so on? I've read that I can only transpose 2D arrays. Enclosed I've programmed the visualization of 2 digital channels containing 32 datapoints. But the result should be 32 channels with 2 datapoints.
    Kind Regards,
    Joachim
    Attachments:
    read from citadel - bit array.png ‏8 KB

  • Convery byte[] into a object

    i have a byte[] which I have got from DB. I want to convert it into a object before returning it from a method. There is a wraaper class for byte i.e. Byte. But I am not sure tha how to convert a byte[] into an Object.
    Thanks!!

    georgexu316 wrote:
    corlettk wrote:
    georgexu316 wrote:
    If you know what you are doing, you can perform an implicit conversion by casting the byte.
    byte byteData = new byte();
    Object newObj = (Object) byteData;
    Umm.. Dude, a byte array is-an Object. In Java you don't ever need to explicitly up-cast... but (in order to actually use it as a byte array) you do have to explicitly down-cast it.
    For example:
    package forums;
    class AByteArrayIsAnObject
    public static void main(String[] args) {
    try {
    Object bytes = "Hello World!".getBytes(); // returns a reference to a newly created byte-array-object
    System.out.println( new String((byte[])bytes) );
    } catch (Exception e) {
    e.printStackTrace();
    Well, the person asked to convert it into an Object, maybe he needs it as a object to pass it through another parameter of another method. Even though byte is an extension of Object and you can use all of Object's methods, it wouldn't pass through a parameter of a method that specifies an object.Ummm... Wanna bet?
    package forums;
    class AByteArrayIsAnObjectTest
      public static void main(String[] args) {
        try {
          println(new String((byte[])getBytes("Hello World!")));
        } catch (Exception e) {
          e.printStackTrace();
      private static Object getBytes(String s) {
        return s.getBytes();
      private static void println(Object o) {
        System.out.println(String.valueOf(o));
    }

  • How does this program group and cluster these button values into an array?

    In the attached program, there are 6 buttons in a cluster and 1 individual button. These buttons control a bunch of different relays to control power to different components of a single device. Right now the program works in that I can control the 6 buttons, the LED turns on in the board and the power is being transmitted. What I do not understand is how it is doing this.  I ran the program with the highlight execution on and at the start of the read from array component, if say I pressed button 6 and hit ok, it reads #6, where did these get defined and labled? and if i wanted to add another button/relay how do i do this? If someone could explain step by step how this is grouping the relays on the board into an array and then controling them I would really appreciate it. Thanks.
    Solved!
    Go to Solution.
    Attachments:
    Cluster to Array.vi ‏85 KB

    The #6 your seeing is the number of elements in the array not a value for the array. 
    Now for some deeper explainations.   From the code it appears that exactly 1 of 6 valves may be used and an additional valve may be in one of two possible states.
    When the program is run the cluster of buttons is initiallized to all False.  and we enter the outer while loop where a sequence structure starts.  In the first frame of the sequence we initiallize a shift register to hold the current value of "Buttons." and this loop runs unthrottled (Add a wait for next ms multiple to this loop to prevent using 100% of the CPU!) 
    For each iteration the buttons are read and if a change has occured to any value buttons that were previously True are reset to False to prevent opening more than one valve.
    When the user presses OK or stop- the current "Buttons" value is passed to the next frame.  This frame convertsthe 7 boolean values to a integer where each valve is controlled by a seperate bit of the integer (Isolation is bit 6 and bits 0-5 each control a mixing valve)
    Now I'd strongly recommend reworking the DAQmx calls- it is pointless to initiallize the task each time you want to use it- Create the task in the initialzation case and wait until the user exits to destroy the task.  And well a sequence structures are frowned on- (there are better ways to do this)
    As far as adding a relay- right now the relays are associated to the hardware by their index position in the cluster (element 1 = bit 0 etc....) to add a new valve you would need to decide what bit you would use to drive it and code in that bits' value to write a 0 in that bit.  (hint the 40 constant is realy 0x40 right-click>visable items>show radix )
    Let me know if you need further elaboration
    Jeff

Maybe you are looking for

  • Lack of clarity in community guidelines

    As I was going through the language learning section, I noticed that a moderator had edited out the skype name in someone's post, but not the blaringly obvious gender specific contact and the request for a relationship.  I then went and checked the c

  • TO confirmation for picking: difference between RF and non RF transactions

    Hello, I am confirming one TO for picking in a outbound delivery process. My storage location is WM and HU managed and materials are batch managed. I noticed if I confirm TO through RF transaction (LM61), I have the following results per material on

  • How to use a CTE in a CASE statement?

    Hi everyone, I'm trying to use a CTE in a CASE statement and getting error. In fact, I'm not sure how to use a CTE in this case and this is the first time I'm using a CTE. I managed to build a CTE successfully but, failed to incorporate it in the CAS

  • Convert a color pdf to BW

       How do I convert a color PDF to black and white? I am using Adobe Acrobat X version 10.1.7. Is there an easy way to do this?

  • Realise n:m table structure

    Hello, i want to build up an editable n:m table structure with apex. can someone give me a hint, which is the best way to do it? I've worked myself through all tutorials and examples, but i can't find anything about this topic. Here is a description