Bits n bytes

hi
i need to grab a byte out of a byte array and then use that as an object either by casting or creating one but for the life of me i have not got a clue how to do it
if anyone can point me in the right direction it would be most appriciated
MM

BigDaddyLoveHandles wrote:
jbish wrote:
BigDaddyLoveHandles wrote:
jbish wrote:
BigDaddyLoveHandles wrote:
byte b = ...
Byte object = Byte.valueOf(b);
Just out of curiosity, is there a reason you chose
Byte object = Byte.valueOf(b);over
Byte object = new Byte(b);?Yes.What is it? (tried to formulate this question to avoid the next yes/no answer)From the API for valueOf
<quote>
Returns a Byte instance representing the specified byte value. If a new Byte instance is not required, this method should generally be used in preference to the constructor Byte(byte), as this method is likely to yield significantly better space and time performance by caching frequently requested values.
<./quote>Got it, thanks.
I should have read the API a little farther.

Similar Messages

  • 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.

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

  • Bits and Bytes

    Hello,
    As much as I know Java can only stream bytes and cannot save a single bit to a file. However is there any place where this is actually documented? and am I right to state this?
    Thanks and Regards,
    Sim085

    Manfolium2 wrote:
    Single bits or even bytes are impractical because of addressing. ( since you have to store the addresses somewhere on the drive, too ) If you save a one bit file you still use the the whole address bin. It's like you rent a huge P.O. box and fill it with one letter. You still have the whole box reserved for you. Most drive formatters will allocate 4Mb to one address. This is common in all computing devices and all laguages. But, if you are referring to the java bytecode,yes when you compile a .java file it becomes encoded in an intermediate code (bytecode) like C#. This way it can run on any machine that has the Java Runtime Environment since it will be compiled into machine code (binary) when it is run by the Java Virtual Machine. This is the foundation of Java and distributed programming. So, yes it is very documented. I'm not sure if this helps. If you want to know more about bytecode and its advantages/disadvantages go to: [http://java.sun.com/docs/white/langenv/Security.doc3.html]
    I like that post-box analogy. Thanx for the fish.
    Most drive formatters will allocate 4Mb to one address.Ummm... 4meg sounds big ???? I thought the block size was 1k on most modern IDE drives.

  • Bits and Byte Manipulation

    Hi,
    Can anyone help?
    I have and array of 4 bytes - hostIP[].
    01000010
    11110001
    11011000
    00011010
    [Just for info, they come from InetAddress.getByName(host).getAddress()].
    I tried to hook them all together into a 32-bit int with this code:
    byte[] hostIP;
    try{
       hostIP = InetAddress.getByName(host).getAddress();
    } catch(Exception e){
       return
    int serverCode = 0;
    for(int n = 0; n < 4; n++){
       serverCode <<=8;
       serverCode |=hostIP[n];
    }The first iteration to add hostIP[0] works fine and leaves serverCode =
    00000000000000000000000001000010
    the <<8 works fine and gives
    00000000000000000100001000000000
    but then -- serverCode |= hostIP[1] -- leaves
    11111111111111111111111111110001
    i.e. I lost hostIP[0] when I added hostIP[1].
    It seems that when the left bit of the 8-bit hostIP[n] is a 1-bit, JAVA casts hostIP[n] to a 32-bit int with 1-bits propogated to fill the left 24 bits before doing |=.
    Then I lose hostIP[0] that I previously shifted into the 9th to 16th bit of serverCode.
    Note: I got this code from an example in the O'Reilly Java Servlet Programing book where it was suggested that the 4 bytes could be hooked together to make a 32-bit key, but it sure doesn't seem to work.
    Any help.

    It seems that when the left bit of the 8-bit
    hostIP[n] is a 1-bit, JAVA casts hostIP[n] to a
    32-bit int with 1-bits propogated to fill the left 24
    bits before doing |=.This is because it needs to cast the byte value to an integer. When it does this if the first bit is 1 it is a negative number, and the keep the same 2's complement value it has to fill the other bits with a one.
    I would just use something like "serverCode |=(hostIP[n] & 0x00FF)" to mask off the unnecessary bits.

  • Bit permutation inside a byte array

    Hi everyone,
    I'm trying to implement DES algorithm, i want to permute specified bits inside an 8byte array.
    I tought about extract every bits from the byte array inside a 64 byte array and then do the permutation.
    But i don't see how to put these bits back into an 8byte array to return the result of encryption/decryption.
    This is what i've done so far (inspired by sources i found on the internet) :
    // extends 8 byte array into a 64 byte array
    public static byte[] extendsByteArray(byte[] bytes) {
    byte[] tab = bytes;
    short len = (short)(desBlockSize * (short)8);
    byte[] bits = new byte[len];
    short i,j;
    for (i = (short)(desBlockSize - (short)1); i >=0; i--) {
    for (j = (short)0; j < desBlockSize; j++)
    bits[--len] = (byte) ((tab[i] >> j) & 0x01);
    return bits;
    // 64 byte array to 8 byte array
    public static byte[] transformByteArray(byte[] bits){
    byte[] tab = bits;
    byte[] bytes = new byte[desBlockSize];
    short i,j = (short)0;
    for ( i = (short)0; i < (short)8; i++){
    bytes[i] = (byte)((bits[j] * (short)2^7) + (bits[j+(short)1] * (short)2^6)+
                   (bits[j+(short)2] * (short)2^5) + (bits[j+(short)3] * (short)2^4)
                   + (bits[j+(short)4] * (short)2^3) + (bits[j+(short)5] * (short)2^2)
                   + (bits[j+(short)6] * (short)2) + (bits[j+(short)7]));
    j +=(short)7;
    return bits;
    This implementation doesn't seems to work, do u have some ideas why it doesn't work or is there an easier solution to permute bits as i'm stuck with this fo the moment.
    Thanks in advance for your answers,
    Julien

    If you are using JavaCard please do not use multiplications as you used in your code. Integer multiplications are very slow in any platform that does not implement them in hardware, and the virtual machine that runs in JavaCards usually does not do any optimizations or Just-In-Time compiling - simply interprets the codes. Memory and CPU power are at a premium in Java Cards. Even if your Java Card has a hardware multiplier, usually it is reserved for RSA operations - it is not available for general use for Java programs.
    Instead of using x * 2 raised to the sixth power, use x << 6. It is faster and generates fewer bytecodes.

  • How to write data from 24 bit card DAQ card unscaled

    Hello
    I have a PXI 4462 Data acquistion card to record data. It is 24 bit card. I have two questions about writing data from this card.
    1) When acquiring the analog signal the card changes the analog to digital and when writing it scales back to analog signal. I want to write the analog signal in the digital form. How can i do that.
    2) PXI 4462 is a 24 bit card, but there is either 16 bit or 32 bit when writing it. So if i write it in 32 bit a byte is not used. only 3 bytes will be used to write the data and one byte wont have any data in it. It takes space on the hard disk. Is there any way to save this one byte so that I will be able to write just 24 bit data without loosing that one byte which would save me plenty of space.
    The sampling rate is usually 96Ks/sec.
    Thanks in advance
    Regards
    Nitin

    If you want to write 24 bits instead of 32, then you have to change the hi(x) to 8 bits instead of 16. The lo(x) will stay at 16 so the 8 plus 16 equals 24 bits. Simple arithmetic. You asked how to get rid of the upper 8 bits and if you leave it at 16, then you haven't gotten rid of anything.
    Why do you think you have to write to separate files? I said do a write of the 16 bit and then a write of of the 8 bit component. It's just two Write File functions to the same file. You could also bundle them together and do a single write but I think there would be a little additional overhead with that.
    Message Edited by Dennis Knutson on 04-06-2006 07:15 AM
    Attachments:
    write binary.JPG ‏5 KB

  • Reading bytes from a server

    Ok,
    I want to connect to a server and recieve a data from it.
    the data is broken up as follows:
    int flag;
    int x, y;
    String name;
    the server i'm communicating with is written in C, so i'm trying to figure out how i'm going to recieve this data.
    What is the size that java uses to store an integer value?
    IF java uses unicode, that's two bytes per character right?
    so if name was 30 characters long it would be 60 bytes?
    i'm thinking of trying to either read the whole transmission into one string then separate, or separate as it comes in?
    could someone suggest how to proceed>

    Java holds (primitive) integers in 32 bits.
    Java holds characters in 16 bits (two bytes)
    30 characters will occupy 30*16 = 480 bits (60 bytes)
    Java provides the java.net package for networking. In there you can find a class called Socket that identifies the clients's communication endpoint, and encapsulates a host and an application address (port).
    First you open a socket to a server:
    Socket s = new Socket( serverName, serverPort );
    then you just read an input stream from the socket.
    DataInputStream dis = new DataInputStream( s.getInputStream() );
    String message = dis.readUTF(); //wait for server to send
    s.close() // close the socket
    Now, you can do with message whatever you like. It's that simple!
    For more information you can see the related tutorials from java.

  • Convert binary bytes to Labview Extended Precision type

    Hello All:
    I am reading a binary file that was written in Windows. One of the types used in the data structure
    is the Delphi extended precision type, which is 10 bytes. When reading this in using Labview's
    "read binary file" VI, there is only one choice, and that is to read it in as an array of 10 bytes
    (Labview's extended precision is 16 bytes.) I have tried using the type cast with no results.
    So....how to convert the data in the 10 byte array to a 16 byte extended precision format in
    Labview? I guess part of the problem is not really understanding how the extended precision
     type is stored in IEEE format.
    Has anyone performed this conversion before that could help out? I'm sure this must have
    been done by someone....somewhere....I have no hair left....
    Any help would be appreciated!
    Thanks,
    Gary.

    Uhm, something must have gone wrong, the link TST sent,
    mentions that LabVIEW stores extended data as 80 bit (10 bytes) on Intel platform.
    Complex double is stored as two doubles (=16 byes). My guess is that Altenbach's latest note mentioning the endianess should help you.
    If the endiannes is right you should be able to just typecast the data:
    Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
    Nederlandse LabVIEW user groep www.lvug.nl
    My LabVIEW Ideas
    LabVIEW, programming like it should be!

  • Not enough bytes in image stream

    Hi
    I'm developing a program that reads/modifies/saves PDF files. I found a strange imagestream in the PDFReference Document:
    2385 0 obj
    <</Length 255/Filter /FlateDecode/Width 155/Height 76/BitsPerComponent 8/ColorSpace 36619 0 R/Type /XObject/Subtype /Image>>
    stream
    [Binary Data]
    endstream
    endobj
    36619 0 obj
    [/Indexed /DeviceCMYK 153 2378 0 R]
    endobj
    2378 0 obj
    <</Length 264/Filter/FlateDecode>>
    stream
    [Binary Data]
    endstream
    endobj
    The decoded stream of object 2385 is 5080 bytes long.
    The decodes stream of object 2378 is 616 bytes long.
    Strange:
    Height/Width*BitsPerComponent = 76*155*8 = 94240 bits = 11780 bytes!
    The length of the stream is only 5080 bytes long.
    Aren´t there to less bytes for the image?
    Or did i misunderstand something?
    PDF viewers can display this PDF correctly, so there must be something I dont know...
    What is the secret?
    Thanks for your help!

    I've found the error now. It was my FlateDecode algorythm which had a too small buffer for some streams, which had a very good compression rate. So they were cut. There aren´t much streams which such a compression rate, so I didn´t realize this a long time.
    Thanks anyway!
    Have a good day!

  • 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 to know the no. of bytes in a String Control Function?

    Hi,
    I would like to know the no. of bytes for a certain String Control Data.
    For instance, if i have a String Length Function, on the left is a String Control and on the right is a Length Indicator.
    If i will type coordinates, i.e. 9.25,4.25 on the String Control it will show 9 on the Length Indicator. I think 9 do not mean 9 bytes but
    only a length. Is there any function in LV that will tell me the total bytes of the data on the string control?
    Appreciate for any advise.
    Thanks a lot.

    Thanks tst for your kind reply.
    Here is what i am trying to find/goal.
    I will be using wireless receiver for my system. The wireless receiver have some signal strength ranges; 95dbm @ 1Mbps (mega bit per sec), 90dbm @ 11Mbps, 82 dbm @ 24 Mbps and 75dbm @ 54 Mbps. Ofcourse i prefer to have lower data transfer rate so that my signal strength is best.
    I will be sending coordinate data/s via wireless communication using string controls. But i do not know how big/heavy is my coordinate data
    If i can know how many bytes or mega bytes the data i want to send then i will be able to transfer it to bits or mega bits(8 bits = 1 byte) thus i will know which setting i can use for the wireless receiver. And if the data is too big then i have a basis to redesign my data transfer to suite a better signal strength.
    Thanks for your help.

  • Byte and Character Streams

    Hi
    I have an insane doubt, wish U can help me
    Java has two classes (FileInputStream and FileOutputStream) which allow u to read one single byte from a stream or to write One single byte out to a stream.
    A classic example provided by Sun has a loop which reads one byte at a time and writes it out until there is no byte left.
    What I dont get is: if char in Java is Sixteen bits (2 bytes) how can such streams read characters if they read one single byte?
    Tnx in advnce

    import java.io.FileReader;
    char[] read(FileInputStream in, int limit) {
      FileReader reader = new FileReader(in);
      char[] array = new char[limit];
      for (int i = 0; i < limit; i++) {
        int value = reader.read();
        if (value == -1) break;
        array[i] = (char)value;
      return array;
    }This code is not ideal, but it should give you an idea of how to get an character from an input stream.

  • Setting bits question

    I have a bunch of bytes of data I want to build in a byte array.
    I looked at BitSet but I dont' think does what I want.
    I'd like to be able to specify a start byte/bit, end byte/bit, along with the data I want to add.
    Ex.
    I'd like to be able to set the first 4 bits of the first byte or
    specify stariting at byte 2, for the next 24 bits write this integer.
    I know I could do a bunch of fanagling with and/ors,etc. but I was wondering if there is another way.
    Thanks

    A few weeks ago, I was playing around with something similar after reading this ACM article (http://queue.acm.org/detail.cfm?id=1563874).
    To illustrate his point, the author created a bit-based record format used to hold U.S. Census data. Each record held information on one person. So, the first bit was the sex, the next 7 bits was the person's age, the next 32 bits was the person's income, etc. For fun, I created a Java program which could read and write these records. It was tricky doing this, but I got it to work.
    One thing to remember that bothered me was that Java doesn't support the concept of "unsigned" like C does. So, when you read all your bytes into a byte array, you must deal with the fact that if the "left-most" bit of the byte is 1, the byte integer value will be negative. And you'll have to know a lot about bit shifting and AND. I looked into BitSet as well, but it didn't help me either.

  • Firefox and slow JavaScript in 64 bit

    Hello!
    I've installed 64 bit Archlinux on my laptop. I noticed poor JavaScript performance with firefox. I had 32 bit linux before. I ran benchmark from this site: http://www2.webkit.org/perf/sunspider-0 … pider.html and I got these results (approximately) - Less means better:
    firefox 3.0.11 - 3200 ms
    firefox 3.5rc2 - 1500 ms
    midori 0.1.7 - 1400 ms
    But now on 64 bit, firefox is slown down:
    firefox 3.0.11 - 3800 ms
    firefox 3.5rc3 - 3200 ms
    midori 0.1.7 - 860 ms
    I tried several builds including self compiling, but results are the same
    Well, it's not that big issue, because browsing is fine. I'm just curious why there is such difference. I thought 64 bit would be even faster...

    Same thing here.
    Firefox is significantly slower than midori.
    Here "FROM" is firefox 3.0.11, "TO" is midori 0.1.7.  Both are from extra.
    TEST COMPARISON FROM TO DETAILS
    =============================================================================
    ** TOTAL **: 3.01x as fast 3470.6ms +/- 6.6% 1152.4ms +/- 10.0% significant
    =============================================================================
    3d: 2.28x as fast 389.6ms +/- 8.4% 171.0ms +/- 17.1% significant
    cube: 3.04x as fast 138.8ms +/- 10.2% 45.6ms +/- 45.3% significant
    morph: 1.73x as fast 121.8ms +/- 9.9% 70.4ms +/- 42.9% significant
    raytrace: 2.35x as fast 129.0ms +/- 21.9% 55.0ms +/- 5.3% significant
    access: 4.34x as fast 474.8ms +/- 13.3% 109.4ms +/- 32.1% significant
    binary-trees: 4.15x as fast 56.4ms +/- 19.8% 13.6ms +/- 44.6% significant
    fannkuch: 4.93x as fast 208.2ms +/- 14.6% 42.2ms +/- 43.2% significant
    nbody: 3.94x as fast 137.2ms +/- 17.9% 34.8ms +/- 15.7% significant
    nsieve: 3.88x as fast 73.0ms +/- 39.8% 18.8ms +/- 56.2% significant
    bitops: 6.19x as fast 412.4ms +/- 7.4% 66.6ms +/- 12.1% significant
    3bit-bits-in-byte: 6.55x as fast 64.2ms +/- 19.9% 9.8ms +/- 48.6% significant
    bits-in-byte: 5.30x as fast 120.8ms +/- 17.8% 22.8ms +/- 11.2% significant
    bitwise-and: 9.86x as fast 116.4ms +/- 7.6% 11.8ms +/- 46.2% significant
    nsieve-bits: 5.00x as fast 111.0ms +/- 11.8% 22.2ms +/- 8.3% significant
    controlflow: 6.34x as fast 67.2ms +/- 24.1% 10.6ms +/- 58.4% significant
    recursive: 6.34x as fast 67.2ms +/- 24.1% 10.6ms +/- 58.4% significant
    crypto: 3.24x as fast 232.0ms +/- 11.4% 71.6ms +/- 18.5% significant
    aes: 2.45x as fast 82.4ms +/- 17.6% 33.6ms +/- 40.7% significant
    md5: 2.77x as fast 60.4ms +/- 18.2% 21.8ms +/- 11.0% significant
    sha1: 5.51x as fast 89.2ms +/- 15.7% 16.2ms +/- 44.2% significant
    date: 2.14x as fast 353.4ms +/- 15.6% 165.0ms +/- 16.0% significant
    format-tofte: 2.42x as fast 204.6ms +/- 14.0% 84.4ms +/- 17.8% significant
    format-xparb: 1.85x as fast 148.8ms +/- 20.4% 80.6ms +/- 25.5% significant
    math: 2.66x as fast 319.8ms +/- 8.7% 120.2ms +/- 27.4% significant
    cordic: 4.61x as fast 122.6ms +/- 13.9% 26.6ms +/- 46.2% significant
    partial-sums: 1.89x as fast 134.8ms +/- 20.7% 71.2ms +/- 32.1% significant
    spectral-norm: 2.79x as fast 62.4ms +/- 16.9% 22.4ms +/- 32.5% significant
    regexp: 5.46x as fast 288.4ms +/- 16.1% 52.8ms +/- 34.8% significant
    dna: 5.46x as fast 288.4ms +/- 16.1% 52.8ms +/- 34.8% significant
    string: 2.42x as fast 933.0ms +/- 16.6% 385.2ms +/- 11.1% significant
    base64: 2.78x as fast 124.6ms +/- 22.6% 44.8ms +/- 44.8% significant
    fasta: 1.90x as fast 171.8ms +/- 16.6% 90.2ms +/- 9.6% significant
    tagcloud: 2.32x as fast 189.4ms +/- 9.0% 81.6ms +/- 28.5% significant
    unpack-code: 2.99x as fast 323.8ms +/- 41.1% 108.4ms +/- 8.8% significant
    validate-input: 2.05x as fast 123.4ms +/- 11.0% 60.2ms +/- 29.4% significant

Maybe you are looking for

  • How to use getContent() method in custom JSP Provider to display a HTML Pag

    Hi, If anybody knows how to use getContent() method to use in custom jsp providers (developed by ourselves) so that it can be used to retrieve a jsp page (a simple html page) .. I want the code in the provider java file to for the getContent method..

  • IPad/Airplay/ itunes download vs import issue

    A movie imported into itunes will play but a downloaded purchase will not, when I choose airplay on the ipad video screen in both cases it says the video is now playing on your Apple_TV. Only in the case of the itunes downloaded content it does not a

  • Delivery date and loading date is same

    Hello , Issue decription: The system calculating delivery=loading date , though the route is maintained for 9 days trasit days. This problem is with only one routeA. When i have created SO for material M123 with route "B" shipping Point 0001, the sys

  • ARRRG!!! Still Getting This Stupid Error When Quitting...

    Ever since I "upgraded" to iTunes 7, every time I quit iTunes I get an error message that "iTunes has unexpectedly quit". I had hoped that 7.1 would make it stop but it did not. Nor did trashing all of the plist files. Finally, I trashed the app itse

  • 11 Days in download still the same

    So i signed up to BT infinity 2, been thru the Stabilisation period and my download is about the same as standard infinity, Have turned hub on and off to see if that makes a difference and it doesn't.  Upload speed has doubled just wish my download h