Changing a byte in a byte array???? HELP

Hey I have the following code
     String plainString = new String();
     byte[] byt = new byte[0];
     byte change;
     try
          int length = (int)(keyf.length());
          byt = new byte[(length++)];
          keyf.readFully(byt);
          keyf.seek(0);
     }catch(IOException e){System.err.println(e);}And I want to edit the byt array at position (length--) and add the character "," there. Also changing the byt array at its current position to eof. Anyone know what to do???

Make a new array with a size of length+1.byt = new byte[(length++)];
keyf.readFully(byt);
byte[] b2 = new byte[byt.length+1];
System.arraycopy(byt, 0, b2, 0, byt.length);
b2[b2.length - 1] = (byte)',';

Similar Messages

  • How do you calculate the number of bytes in a hexidecimal array?

    Hi can anybody help me calculate the number of bytes in a hexidecimal array ie:
    04h + 12h + 4Eh + 20h = 84h
    Thanks

    Hi,
    I'm not sure if you want to count the bytes or sum the array. But here's an example of both:
    Hope this helps,
    Paulo
    Attachments:
    ByteCount.vi ‏18 KB

  • Converting bytes to shorts using arrays and NIO buffers - is this a bug?

    I'm benchmarking the various methods for converting a sequence of bytes into shorts. I tried copying a byte array, a direct NIO buffer and a non-direct NIO buffer to a short array (using little-endian byte order). My results were a little confusing to me. As I understand it, direct buffers are basically native buffers - allocated by the OS instead of on the runtime's managed heap, hence their higher allocation cost and ability to perform quicker IO with channels that can use them directly. I also got the impression that non-direct buffers (which have a backing array) are basically just regular Java arrays wrapped in a ByteBuffer.
    I didn't expect a huge difference between the results of my three tests, though I thought the two NIO methods might perform a bit quicker if the virtual machine was smart enough to notice that little-endian is my system's native byte order (hence allowing the chunk of memory to simply be copied, rather than applying a bunch of shifts and ORs to the bytes). Conversion with the direct NIO buffer was indeed faster than my byte array method, however the non-direct NIO buffer performed over ten times slower than even my array method. I can see it maybe performing a millisecond or two slower than the array method, what with virtual function call overhead and a few if checks to see what byte order should be used and what not, but shouldn't that method basically be doing exactly the same thing as my array method? Why is it so horribly slow?
    Snippet:
    import java.nio.*;
    public class Program {
      public static final int LOOPS = 500;
      public static final int BUFFER_SIZE = 1024 * 1024;
      public static void copyByteArrayToShortArrayLE(byte[] buffer8, short[] buffer16) {
        for(int i = 0; i < buffer16.length; i += 2){
          buffer16[i >>> 1] = (short) ((buffer8[i] & 0xff) | ((buffer8[i + 1] & 0xff) << 8));
      public static void main(String[] args) {
        short[] shorts = new short[BUFFER_SIZE >>> 1];
        byte[] arrayBuffer = new byte[BUFFER_SIZE];
        ByteBuffer directBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE).order(ByteOrder.LITTLE_ENDIAN);
        ByteBuffer nonDirectBuffer = ByteBuffer.allocate(BUFFER_SIZE).order(ByteOrder.LITTLE_ENDIAN);
        long start;
        start = System.currentTimeMillis();
        for(int i = 0; i < LOOPS; ++i) copyByteArrayToShortArrayLE(arrayBuffer, shorts);
        long timeArray = System.currentTimeMillis() - start;
        start = System.currentTimeMillis();
        for(int i = 0; i < LOOPS; ++i) directBuffer.asShortBuffer().get(shorts);
        long timeDirect = System.currentTimeMillis() - start;
        start = System.currentTimeMillis();
        for(int i = 0; i < LOOPS; ++i) nonDirectBuffer.asShortBuffer().get(shorts);
        long timeNonDirect = System.currentTimeMillis() - start;
        System.out.println("Array:      " + timeArray);
        System.out.println("Direct:     " + timeDirect);
        System.out.println("Non-direct: " + timeNonDirect);
    }Result:
    Array:      328
    Direct:     234
    Non-direct: 4860

    Using JDK1.6.0_18 on Ubuntu 9.1 I typically get
    Array: 396
    Direct: 550
    Non-direct: 789
    I think your tests are a little too short for accurate timings because they are likely to be significantly influenced by the JIT compilation.
    If I change to use a GIT warmup i.e.
    public static final int LOOPS = 500;
        public static final int WARMUP_LOOPS = 500;
        public static final int BUFFER_SIZE = 1024 * 1024;
        public static void copyByteArrayToShortArrayLE(byte[] buffer8, short[] buffer16)
            for (int i = 0; i < buffer16.length; i += 2)
                buffer16[i >>> 1] = (short) ((buffer8[i] & 0xff) | ((buffer8[i + 1] & 0xff) << 8));
        public static void main(String[] args)
            short[] shorts = new short[BUFFER_SIZE >>> 1];
            byte[] arrayBuffer = new byte[BUFFER_SIZE];
            ByteBuffer directBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE).order(ByteOrder.LITTLE_ENDIAN);
            ByteBuffer nonDirectBuffer = ByteBuffer.allocate(BUFFER_SIZE).order(ByteOrder.LITTLE_ENDIAN);
            long start = 0;
            for (int i = -WARMUP_LOOPS; i < LOOPS; ++i)
                if (i == 0)
                    start = System.currentTimeMillis();
                copyByteArrayToShortArrayLE(arrayBuffer, shorts);
            long timeArray = System.currentTimeMillis() - start;
            for (int i = -WARMUP_LOOPS; i < LOOPS; ++i)
                if (i == 0)
                    start = System.currentTimeMillis();
                directBuffer.asShortBuffer().get(shorts);
            long timeDirect = System.currentTimeMillis() - start;
            for (int i = -WARMUP_LOOPS; i < LOOPS; ++i)
                if (i == 0)
                    start = System.currentTimeMillis();
                nonDirectBuffer.asShortBuffer().get(shorts);
            long timeNonDirect = System.currentTimeMillis() - start;
            System.out.println("Array:      " + timeArray);
            System.out.println("Direct:     " + timeDirect);
            System.out.println("Non-direct: " + timeNonDirect);
        }then I typically get
    Array: 365
    Direct: 528
    Non-direct: 750
    and if I change to 50,000 loops I typically get
    Array: 37511
    Direct: 57199
    Non-direct: 73913
    You also seem to have a bug in your copyByteArrayToShortArrayLE() method. Should it not be
    public static void copyByteArrayToShortArrayLE(byte[] buffer8, short[] buffer16)
            for (int i = 0; i < buffer8.length; i += 2)
                buffer16[i >>> 1] = (short) ((buffer8[i] & 0xff) | ((buffer8[i + 1] & 0xff) << 8));
        }Edited by: sabre150 on Jan 27, 2010 9:07 AM

  • Best way to append null bytes to a byte array?

    I have a fixed length byte array and need to add two null bytes to its end. I have an implementation that I wrote to do this, though I was wondering whether someone could improve on what I have:
    public byte[] appendNullBytes( byte[] bytes ) {
       byte[] copyBytes = new byte[bytes.length+2];
       System.arraycopy(bytes, 0, copyBytes, 0, bytes.length);
       copyBytes[copyBytes.length-1] = 0;
       copyBytes[copyBytes.length-2] = 0;
       return copyBytes
    }

    ajmasx wrote:
    jverd wrote:
    ajmasx wrote:
    jverd wrote:
    Those aren't null bytes, they're zero bytes. A bytes can never have the value null in Java.I was using the term null interchangeably with 'zero', Which is not generally applicable in Java.Well it all depends what you are doing:
    - A null is a zero value.Not in general in Java. Null is null and zero is zero.
    - char c = '\0' defines a null character.Yes, that's the only use of "null" for primitive types.
    - A null pointer is where the address is zero and not pointing to some location in memory.Nope. A null pointer is simply one that doesn't point to any value. Nothing in the spec says it's zero.
    - A null reference is more or less the same thing (there are certain difference in the details).Nope. A null pointer and null reference in Java are the same thing.
    - A null terminated string is one where null defines the end of the string.This does not exist in Java. As the above smartass pointed out, you can put the null character at the end of a String, but that character is part of the String, not a terminator.
    >
    If you are dealing with files and other types of I/O then this concept most certainly exists. Not in Java.
    Just because you are limiting yourself to high-level use of Java, does not mean they aren't present in Java.The fact that null is precisely defined in the JLS and does not include these things means they aren't present in Java.

  • Converting bytes to unsigned bytes

          int u=0;
          int i=32;
    byte []  data1 = new byte[1024];
    int[] unsignedValue=new int[1024];
    while(u<100){
                        unsignedValue[u] =data1[i] & 0xff;
                          u++;
                          i++;
                       } from input stream iam storing bytes in data1.now i want to convert specific bytes to unsigned bytes.
    here i tryed in the above way but i want to convert
    2 to 8
    12 to 18
    22 to 28
    bytes to unsigned and i want to store how can i do this?

    nanda_java wrote:
    in my fist post only i multiplyes with 0xff .and i gave code ...my question is how to multiply only specific
    2 to 8
    12 to 18
    22 to 28
    bytes with 0xff .the bytes are in data1.I have no idea what you're saying, and I have no idea what the significance of those ranges is.
    Look, you've already been told this twice: Say you get the byte 0xFE. That byte is neither signed nor unsigned. Get that into your head. It's simply a byte. The concept of signed/unsigned only comes into play when you decide to treat that as an integer value. You might want it to represent -2, or you might want it to represent 254. If you store it in a Java byte, it will represent -2. You cannot change that. If you want 0xFE to mean 254, then mask it with 0xFF and store it in an int.
    byte b = -2;
    System.out.println(b & 0xFF); // prints 254Furthermore, as already stated, in many cases it doesn't matter if it's signed or unsigned. If you do addtion, subtraction, multiplication (I think), AND, OR, XOR, you'll get the same byte result, regardless of whether you consider it signed or unsigned, and again, signed/unsigned only matters if you care about the numerical value of that signed result, rather than simply its bit pattern. 0xFE - 0x01 = 0xFD, and you can take that as -2 -1 = -3 or as 254 - 1 = 253. The bytes are the same regardless.
    What part are you not understanding? Don't just keep repeating the same thing about how it's unsigned and 2 to 8 and all that nonsense.
    So please, define your problem more clearly.
    Edited by: jverd on Jan 10, 2008 8:53 AM

  • Form English Char ( Single Byte  ) TO Double Byte ( Japanese Char )

    Hello EveryOne !!!!
    I need Help !!
    I am new to Java ,.... I got assignment where i need to Check the String , if that string Contains Any Non Japanse Character ( a~z , A~Z , 0 -9 ) then this should be replaced with Double Byte ( Japanese Char )...
    I am using Java 1.2 ..
    Please guide me ...
    thanks and regards
    Maruti Chavan

    hello ..
    as you all asked Detail requirement here i an pasting C code where 'a' is passed as input character ..after process it is giving me Double Byte Japanese "A" .. i want this to be Done ..using this i am able to Convert APLHA-Numeric from singale byte to Doubel Byte ( Japanse ) ...
    Same program i want to Java ... so pleas guide me ..
    #include <stdio.h>
    int main( int argc, char *argv[] )
    char c[2];
    char d[3];
    strcpy( c, "a" ); // a is input char
    d[0] = 0xa3;
    d[1] = c[0] + 0x80;
    printf( ":%s:\n", c ); // Orginal Single byte char
    printf( ":%s:\n", d ); // Converted Double Byte ..
    please ..
    thax and regards
    Maruti Chavan

  • High bytes and low bytes?

    What are high bytes and low bytes? Thanks for helping.

    This may not be enough information to answer your question. How and where are the terms High bytes and Low bytes used?
    Often, a data type is represented using multiple bytes. For example, short is 2 bytes. Sometimes the most significant byte is the high byte while the least significant byte is the low byte.

  • Working set, virtual bytes and private bytes

    Hello everyone,
    I am using Windows Server 2003 Performance Counter tool to monitor the memory consumed by my process.
    The interested terms are working set, virtual bytes and private bytes. My questions are,
    1. If I want to watch the real physical memory consumed by current process, which one should I monitor?
    2. If I want to watch the physical memory + swap file consumed by current process, which one should I monitor?
    3. Any more clear description of what these terms mean? I read the help from Performance Counter tool, but still confused
    which one(s) identifies the real used physical memory, and which one(s) identifies the real used physical memory + page swap file, and which one(s) identifies the required memory (may not be really allocated either in physical memory or in swap page file).
    If there are any related learning resource about the concepts, it is appreciated if you could recommend some. :-)
    thanks in advance,
    George

    And for further benefit:
    "Virtual bytes" is the total size of the not-free virtual address space of the process. It includes private committed, mapped, physically mapped (AWE), and even reserved address spaces. 
    "Private committed" is the portion of "virtual bytes" that is private to the process (as opposed to "mapped", which may be shared between processes; this is used by default for code). This normally includes program globals,
    the stacks (hence locals), heaps, thread-local storage, etc. 
    Each process's "private committed" memory is that process's contribution to the system-wide counter called "commit charge". There are also contributions to commit charge that aren't part of any one process, such as the paged pool. 
    The backing store for "private committed" v.m. is the page file, if you have one; if you have (foolishly in my opinion) deleted your page file, then all private committed v.m., along with things like the paged pool that would normally be backed
    by the page file, has to stay in RAM at all times... no matter how long ago it's been referenced (which is why I think running without a page file is foolish). 
    The "commit limit" is the size of RAM that the OS can use (this will exclude RAM used for things like video buffers) plus the total of the current size(s) of your pagefile(s). Windows will not allow allocations of private committed address space
    (e.g. VirtualAlloc calls with the COMMIT option) that would take the commit charge above the limit. If a program tries it, Windows will try to expand the page file (hence increasing the commit limit). If this doesn't succeed (maybe because page file expansion
    is disabled, maybe because there's no free disk space to do it) then the attempt to allocate committed address space fails (e.g. the VirtualAlloc call returns with a NULL result, indicating no memory was allocated). 
    The "Working set" is not quite "the subset of virtual pages that are resident in physical memory", unless by "resident" you mean "the valid bit is set in the page table entry", meaning that no page fault will occur
    when they're referenced. But this ignores the pages on the modified and standby page lists. Pages lost from a working set go to one of these lists; if the modified list, they are written to the page file and then moved to the standby list. From there they
    may be used for other things, such as to satisfy another process's  need for pages, or (in  Vista and later) for SuperFetch. But until that happens, they still contains the data from the process they came from and are still associated with that process
    and with that processes' virtual pages. Referring to the virtual page for which the physical page is on the standby or modified list will result in a "soft" page fault that is resolved without disk I/O. Much slower than referring to a page that's
    in the working set - but much faster than having to go to disk for it. 
    So the most precise definition of "working set" is "the subset of virtual pages that can be referenced without incurring a page fault." 

  • How can I change the precision of a numeric array indicator using property nodes ?

    I want to change the precision of a numeric array indicator programatically using property nodes. I tried to see all the array properties but I couldn't find it. URGENT !!!

    If you want to change the precision of an element in an array you must first get the reference to the array element. Then raise the reference to the more specific class of a digital numeric. Use a property node connected to that reference to set the precision. See the attached vi for an example.
    Brian
    Attachments:
    ArrayElementPrec.vi ‏27 KB

  • While trying to change password getting ora-00600 [4842] - Plz help

    while trying to change password getting ora-00600 [4842] - Plz help
    database : 9.2.0.4
    o/s : Linux

    Hi,
    when getting an ORA-600 you always should contact Oracle support please ;-)
    To diagnose an ORA-600 the stack trace will be necessary - see the trace file mentioned in the alert.log when the ORA-600 did happen.
    But FIRST OF ALL you should definitely apply patch set 9.2.0.8 and see if this error happens again (metalink.oracle.com ==> PATCHES ==> Quick Links ==> <your platform>). Support will ask you to do the same before they start diagnosing anything in this case.
    And please keep in mind that 9.2.0 has reached end of Premier Support more than 1 year ago.
    Kind regards
    Mike

  • My iSight is not recognized by my mac, even in the picture changing of my profile. Can anyone help?

    My iSight is not recognized by my mac, even in the picture changing of my profile. Can anyone help?

    http://discussions.apple.com/docs/DOC-6431

  • After installing CC, The font on some websites has changed in my browser Google Chrome. Help!

    After installing CC, The font on some websites has changed in my browser Google Chrome. Help!
    take for example facebook & ebay, the fonts have changed to a distorted block type font, very un-readable, Only after I installed adobe CC, this took place...
    I am using google chrome
    Can any one help me?
    I have tried to fix it by tinkering with some settings in google chrome but had no luck!
    here is a screenshot of the problem:

    Unless a web page uses a “web font” that it supplies itself, web pages typically provide a list of fonts, in priority order, of fonts to be used to render text. Typically, this list of fonts represents “system fonts” of MacOS, Windows, iOS, and Android - fonts that can be counted on being installed. Helvetica Neue is a MacOS system font that is not typically found on Windows systems. And even if a user licenses recent versions of Helvetica Neue OpenType fonts and installs same on Windows systems, they are in the “Helvetica Neue Std” family which would not match “Helvetica Neue” which is native to MacOS. Thus, I suspect that the Helvetica Neue you have installed on Windows is an older font offering.
    There are three issues here:
    The first is that web pages provide a priority list for font usage. I suspect that the web pages you are seeing Helvetica Neue actually used on your system are requesting fonts in the order of Helvetica Neue and then Arial. The typical MacOS system would display the content with Helvetica Neue and the typical Windows system would display the content with Arial. Since you have Helvetica Neue on your system, you are getting rendering similar to what a MacOS system would have.
    The second issue is that there is no way to force a web page to reverse the order of priority of the fonts it lists.
    And finally, the third issue is that there is no way to hide a font selectively by application.
    The only way you can avoid Helvetica Neue being used on web pages preferring Helvetica Neue to let's say Arial or any other font on its list for particular text rendering is to uninstall Helvetica Neue.
    That having been said, other than perhaps personal type design preferences, are there any other issues with regards to use of Helvetica Neue for display of these web pages?
            - Dov

  • How can I change all Inf elements in an array to a specific value?

    I would like to change all Inf elements in my array to -1. Basically, I am looking for something similar to the range and coerce function, except that I want all values above a certain value to be a specific negative value, rather than go to that value.
    I could use a case structure and go through each element in the array, but I am hoping to find something more efficient.Any ideas?
    Solved!
    Go to Solution.

    P@Anand wrote:
    I would do that in this way.
    Here's a simpler version...
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines
    Attachments:
    Replace in Array.png ‏4 KB

  • In a document with several sections, in section VIII and IX one cannot select the text of the page foot nor set the pointer in it; so, one cannot write nor change the page foot text. Please help!

    in a document with several sections, in section VIII and IX one cannot select the text of the page foot nor set the pointer in it; so, one cannot write nor change the page foot text. Please help!

    Question already asked and answered several times.
    It's a bug striking in long documents.
    Select a word somewhere higher in the page then use the arrows to reach the wanted insertion point.
    Yvan KOENIG (VALLAURIS, France) mardi 23 août 2011 15:44:24
    iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0
    My iDisk is : <http://public.me.com/koenigyvan>
    Please :
    Search for questions similar to your own
    before submitting them to the community
    To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

  • Encountered an error when attempting to open Lightroom for the first time. Went to Lightroom Help and searched for the error "An error occurred when attempting to change modules". Non of the solutions helped. what now?

    Encountered an error when attempting to open Lightroom for the first time. Went to Lightroom Help and searched for the error "An error occurred when attempting to change modules". Non of the solutions helped. What now?

    I am getting this error message as well. I have just downloaded Lightroom recently. I try to click on Lightroom / preferences....and nothing happens. I tried to uninstall and it is greyed out so I can't do that either. Suggestions? Please help?

Maybe you are looking for

  • IMAGEIO IS WORKING BUT BYTEARRAY IS NOT WORKING:

    Hi all I am getting images from gmail contacts and trying to store it in DB using my http code:InputStream is=urlconnection.getInputStream(); i could able to store the inputstream in file using: BufferedImage filebuffer=ImageIO.read(is); FileOutputSt

  • User Profile Service Application - My Site Suggestions Email Job

    User Profile Service Application - My Site Suggestions Email Job is sending duplicate emails. It runs 15th of every month at 10 pm. On 15th July, it ran from 10 pm to 11 pm and sent two emails during this interval. Can anyone please let me know what

  • Can autorize my own music I bought

    I had bought a song off itunes a while back and registered under my AOL account, I have since reformatted my computer and my aol account is gone so when I try to authorize my song again, I do it under this apple ID. I put in my credit card info and i

  • Satellite L650-0K5 - How to adjust screen contrast?

    Hi guys and gals, I've searched the L series thread before asking this question, so apologies if I should have looked harder! The default contrast settings are making blacks appear grey and also whites have a slight blue caste to them. My searches ha

  • Can I use Web Fonts in Illustrator or Photoshop?

    I'd like to use a few of the web fonts for a clients project but I need to be able to have the fonts available for me in Illustrator and Photoshop as well as Dreamweaver and Muse. I am a full Creative Cloud Subscriber and need to have complete integr