Array of bytes..

hi..
how can i convert array of bytes to string and vise versa
thanks

You can convert the string to a char array with String.toCharArray(), but if you want them as a byte array you have to first decide what character encoding should be used with the conversion. You can e.g. use "8859_1" if you only work with characters in the range 0 to 255. Then it would be String.getBytes("8859_1"). You have to add a try-catch block. It will throw an exception if you use an unsupported encoding. ISO 8859-1 is one of the basic supported encodings, so you will not get any exceptions when you use this.
To go the other way, you use one of the String constructors.

Similar Messages

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

  • Array of bytes containg punctuatio​n

    Hi, I have an array of bytes that I would like to export using the spreadsheet VI. The spreadsheet creates a file with the extension .res, which logs the array as hex values. The problem is, all my data is separated with a punctuation mark, which I guess LabView inserts into the array to separate the numbers.
    So my question is, is there any way to convert an array of U8's into a string of pure hex or just a stream of binary data?
    Thanks

    q-bertsuit wrote:
    Thank you for your answers, I'll try your link. The first suggested solution didn't work.
    You are not giving enough information. Did the second suggestion work?
    q-bertsuit wrote:
    I want to use the spreadsheet VI to make a .res file, saving as a plain text file would cause alot of extra work in MatLab.
    A "spreadsheet" in LabVIEW is nothing more than a plain text file with certain delimiters for columns and newlines delimiting rows. "Write to spreadsheeet file" is a simple tool for typical use. All the formatting can be done explicit and then written to a plain text file, the outcome is exactly the same. There is no extra work involved in Matlab.
    q-bertsuit wrote:
    I might not have explained the problem sufficiently in the first post. If my buffer containing U8s is passed in to the spreadsheet VI, the .res file saves for example the number 255 as ASCII character 2 then character 5 then 5. So 255 would be saved as 32 35 35 instead of FE.
    Again, write to spreadsheet file is not the right tool then, so don't use it.
    It is still not clear. So you have an array of U8. How should the file look like? Should there be any delimiters?
    Example: If you only write 255 to the file, should it contain
    one binary, nonpritable character (xFF)
    two charcters (two instances of the letter F)
    Three characters (letter 2 followed by two letters 5)
    If the array has two elements, how should the second character be stored? Should there be a delimiter (space, comma, tab, etc) or nothing separating it from the first value?
    Once we have the specifications, the rest will be trivial.
    LabVIEW Champion . Do more with less code and in less time .

  • Read array of bytes

    using what do i read array of bytes that is on a stream...
    i send an array of bytes to the invoking stream using void write(byte buffer[])....
    how do i read it from the other side....
    uzair

    Attempts to read up to buffer.length bytes into
    buffer, returning the number of bytes that were
    sucessfully read. -1 is returned when the end of the
    file is encountered.I wasn't asking you to post what the API says it does. We know what it does. Now, do you understand how it reads the bytes into the buffer that you pass to it?

  • How to send array of bytes to a servlet and store it in a file

    Hi,
    How to send array of bytes to a servlet and store it in a file.
    I am new to Servlets. if possible for any one, please provide the code.
    Thanks,
    cmbl

    Through HTTP this is only possible with a POST request of which the encoding type is set to multipart/form-data. You can use Apache Commons FileUpload to parse such a multipart form data request into useable elements. From the other side (the client) you can use a HTML <input type="file"> element or a Java class with Apache Commons PostMethod API.
    You may find this article useful: http://balusc.blogspot.com/2007/11/multipartfilter.html

  • Managing bit flags in an array of bytes

    In my program, I have a large array of bytes. Some arbitrarily long groups of bytes in this array act as groups of bit flags. I need to be able to retrieve and manipulate these bit flags. I read that the best way to do this is with bitwise operations; something I have never learned before. I wrote methods that seem to work, but because I have never done anything like this before, can someone check my work?
    Here is an example program, where the only contents of the byte array is a single two-byte grouping of 16 bit flags:
    public class test
        static byte[] bytes = new byte[2];
        static byte[] pow2 = {1, 2, 4, 8, 16, 32, 64, -128};
        static byte[] pow2i = {-2, -3, -5, -9, -17, -33, -65, 127};
        public static void main(String[] args) throws Exception
            writeBitFlag(0, 6, true);
            for (int i = 0; i < 16; i++)   
                System.out.println("Flag " + i + ": " + getBitFlag(0, i));
            System.out.println();
            writeBitFlag(0, 12, true);
            invertBitFlag(0, 0);
            invertBitFlag(0, 0); 
            invertBitFlag(0, 1);
            for (int i = 0; i < 16; i++)   
                System.out.println("Flag " + i + ": " + getBitFlag(0, i));  
        }//end main
        public static boolean getBitFlag(int startAddress, int flag)
            return (bytes[startAddress + flag / 8] & pow2[flag %= 8]) == pow2[flag];
        }//end getBitFlag
        public static void invertBitFlag(int startAddress, int flag)
            bytes[startAddress + flag / 8] ^= pow2[flag % 8];
        }//end invertBitFlag
        public static void writeBitFlag(int startAddress, int flag, boolean flagVal)
            if (flagVal)
                bytes[startAddress + flag / 8] |= pow2[flag % 8];
            else
                bytes[startAddress + flag / 8] &= pow2i[flag % 8];
        }//end writeBitFlag      
    }//end class testDoes this look like the right way to do what I am trying to do?

    You could try BitSet which provides these function for you.
    public class test { 
        public static void main(String[] args) throws Exception {   
            byte[] bytes = new byte[2];
            writeBitFlag(bytes, 6, true);
            for (int i = 0; i < bytes.length*8; i++)   
                System.out.println("Flag " + i + ": " + getBitFlag(bytes, i));
            System.out.println();
            writeBitFlag(bytes, 12, true);
            invertBitFlag(bytes, 0);
            invertBitFlag(bytes, 0); 
            invertBitFlag(bytes, 1);
            for (int i = 0; i < bytes.length*8; i++)   
                System.out.println("Flag " + i + ": " + getBitFlag(bytes, i));  
        }//end main
        public static boolean getBitFlag(byte[] bytes, int flag) {
            return ((bytes[flag >> 3] >> flag) & 1) != 0;
        public static void invertBitFlag(byte[] bytes, int flag) {
            bytes[flag >> 3] ^= (1 << (flag & 7));
        public static void writeBitFlag(byte[] bytes, int flag, boolean flagVal) {
            if (flagVal)
                bytes[flag >> 3] |= (1 << (flag & 7));
            else
                bytes[flag >> 3] &= ~(1 << (flag & 7));
    }//end class test

  • How to Convert array of int into array of byte - please help

    I have a 2-dim array of type int and I want to convert it to an array
    of byte. How can I do that? Also, if my 2-dim int array is one dimention
    can I use typecast as in the example below?
    private byte[] getData()
    byte []buff;
    int []data = new int[5];
    //populate data[]
    buff = (byte[]) data; <---- CAN I DO THIS??????
    return buff;

    hi, I suggest you make an array of byte arrays
    --> Byte[][] and use one row for each number
    you can do 2 things,
    take each cipher of one number and put one by one in each column of the row correspondent to that number. of course it may take too much room if the int[] is too big, but that is the easiest way I think
    the other way is dividing your number into bitsets(class BitSet) with sizes of 8 bits and then you can save each bit into each column of your array. and you still have one number in each row. To put your numbers back use the same class.
    Maybe someone has an easier way, I couldnt think of any.

  • 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

  • Converting an array of bytes to an image

    I think this is the right forum for my question.
    This code comes from the J2ME side of my application. It reads in an int from my server, and if its over a certain length, I can tell its an image so i can decode it from an array of bytes back into an image, but it throws an IllegalArgument Exception on image = Image.createImage(b,0,length);
    I've decoded the byte array before it gets sent and convereted it back into an image and that works, and ive run the debugger and the array is full of numbers on the J2ME side of the app, but the call to createImage, just keeps throwing this exception and i can't find an answer. If you need anymore code, I'll post it.
    int l = in.readInt();
    imageArray = new ImageItem[l];
    for(int i = 0; i < imageArray.length; i++){
    byte[] b = null;
    int length = in.readInt();
    System.out.println("length = " + length);
    if(length < 10) {
    imageArray[i] = null;
    System.out.println("null image");
    } else {
    b = new byte[length];
    in.readFully(b,0,length);
    System.out.println("image not null");
    Image image = Image.createImage(b,0,length);
    System.out.println("hit");
    ImageItem imageItem = new ImageItem("null", image, 0, "null");
    imageArray[i] = imageItem;
    If anyone can tell me how to indent the code, i would appreciate it, it looks indented when i post it my side.
    Message was edited by:
    trisonetwo
    Message was edited by:
    trisonetwo

    If it works before sending then check your code for sending the image.
    Also you can compare the byte array before sending with the array which was received at the other end.
    For indent and syntex highliting use [ c o d e ] and [ / c o d e ] tags (without spaces)
    Ex:-
    int l = in.readInt();
    imageArray = new ImageItem[l];
    for(int i = 0; i < imageArray.length; i++){
       byte[] b = null;
       int length = in.readInt();
       System.out.println("length = " + length);
       if(length < 10) {
          imageArray = null;
          System.out.println("null image");
       } else {
          b = new byte[length];
          in.readFully(b,0,length);
          System.out.println("image not null");
          Image image = Image.createImage(b,0,length);
          System.out.println("hit");
          ImageItem imageItem = new ImageItem("null", image, 0, "null");
         imageArray = imageItem;
    }

  • Reading an array of byte with special delimiter....help me!!

    Hi guys,
    i've developed a java application that has in input a txt file with a standard format
    string string string
    string double double
    string double double
    and stores it into an array of byte with a fixed format,
    that is byte(whitespace)byte(whitespace)byte(;)byte(whitespace)byte(whitespace)byte(;)byte(whitespace)byte(whitespace)byte(;)
    that mean introducing byte whitespace value and byte ; value to separate different columns and different rows.
    Now i have to develop inverse routine,that has to read the array of byte and rebuild its original format.
    Can you help me with some idea ord code?
    I'm inexepert...please help me with clear suggest...
    I post you my routine that converts txt file into an array of byte
    P.S.In my txt file number of columns and rows change for each file,what is standard is a firtst headline that is an array of string and others row with the same format string array of doubles....
    public byte[] getBytes(){
              byte middlerow=' ';
              byte endrow=';';
              Vector temp=new Vector(10000000);
              int i=0;     
              String g=null;
              Riga r;
              Double val[];
              while(i<intest.length){
                   //copio tutto nell'array di byte
                   byte []bytes = intest.getBytes();
    //               memorizza in byte un elemento del vettore alla volta
                   for( Byte aByte : bytes ) {
                   temp.addElement( aByte );
                   temp.addElement( Byte.valueOf( middlerow ) );
                   i++;
              temp.addElement(Byte.valueOf(endrow));
              System.out.println("Intestazione convertita in byte");
              for(int l=0;l<rows.size()-1;l++){
                   r=(Riga)rows.get(l);
                   g=r.getgeneid();
              temp.addElement(g.getBytes());
              temp.addElement(Byte.valueOf(middlerow));
              val=r.getvalues();
              for(int e=0;e<=val.length-1;e++){
              temp.addElement(Byte.valueOf(val[e].byteValue()));
              //val[e].byteValue() fa il casting double in byte
              temp.addElement(Byte.valueOf(middlerow));
         temp.addElement(Byte.valueOf(endrow));
              byte [] b=new byte[temp.size()];
              for (int k=0;i<temp.size();i++){
                   b[k]=(((Byte)temp.elementAt(k)).byteValue());
              return b;
    Thanks for your help...

    Ahh, how is the "array of bytes" being populated, Java code?
    If so which encoding are you using? When you create the bytes (from a string at a guess) the other end which encoding do you use?
    If you are using a db, why not use a db? Why this blob of data?
    If I really was banned from using the DB as a DB, and HAD (on pain of death) to use this wonky blob thing, I would do:
    Writing:
    ByteArrayOutputStream daos = new ByteArrayOutputStream() ;
    DataOutputStream dos = new DataOutputStream( daos );
    dos.writeUTF( "me String" );
    dos.writeDouble( 4.2 );
    dos.close;
    byte[] stickInUglyBlob = daos.toByteArray() ;
    Reading:
    DataInputStream dis = new DataInputStream( new ByteInputStream( blobOfUckyness ) );
    String meString = dis.readUTF();
    double meDouble = dis.readDouble();

  • Converting a PNG image to an array of bytes and vice versa..

    hi all,
    i need to convert a PNG image to an array of bytes ,then converting back this array of bytes to The PNG image again ,i read this can be done using output streams but i feel like a dump and i can't fix the whole thingy out ! ,can anybody help me in this ,by explaining how can this be established????
    Regards,
    D.Roth

    hi all,
    i need to convert a PNG image to an array of bytes ,then converting back this array of bytes to The PNG image again ,i read this can be done using output streams but i feel like a dump and i can't fix the whole thingy out ! ,can anybody help me in this ,by explaining how can this be established????
    Regards,
    D.Roth

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

  • How do I convert an array of BYTES (where each BYTE represents a bit) into a single Hex number?

    I am reading a signal from a USB-8451. This signal is stored as an array where each element represents a bit in the signal, but is stored in the array as a byte. How do I convert this array into a single Hex number. I attatched what I have so far, there are a few extra things to help me see what ia going on. One code uses Queue and the other uses arrays, let me know if you can help.
    Attachments:
    845x_EEPROMarrays.vi ‏27 KB
    845x_EEPROM.vi ‏26 KB

    mkssnwbrd wrote:
    ... so we can't introduce any other forms of signals or power into the circuit other than what the circuit already has. Trithfully I don't really know how I2C devices work, but my mentor here says that we can't use an I2C method becuase it will introduce voltage into the circuit and may damage out TCON chip.
    That makes absolutely no sense. What do you think is happening when you write the digital lines? You're setting a pin high. That voltage is being generated by the 8451x. I think you're not understanding what your mentor is saying. If it's an I2C device then you should be able to use the I2C function to simply talk to it. You still have not indicated what the device is, so there's little more I can say about that aspect of it.
    As far as the conversion is concerned, you basically need loop through your array of "bits", taking 16 at time since you said you have 16-bit values. It's not clear from your code whether your eventual goal is to get a numeric value or a string. This does not appear to be a subVI, so a simply numeric indicator formatted to display in hex format should be quite adequate. The array you are generating is an array of rings, whose datatype is I32, but they will have values of 0 or 1. You can use the example just posted, or you can use the attached variation.
    Attachments:
    Bits to Hex 2.vi ‏17 KB

  • Inserting an array of byte[ ] in  a database

    Hi,
    In my program I have to insert a bynary data in my database. To do that, I created in the database a column, called "data", and I set the type of the column to MEDIUMBLOB. I receive the data that I have to store from a serial port, so they are stored in a byte[ ] array. How can I give that array to my database?

    I'd try this:
    http://java.sun.com/javase/6/docs/api/java/sql/PreparedStatement.html#setBytes(int,%20byte[])
    Alternatively, change it to an InputStream (see ByteArrayInputStream) and set data as a stream.

  • Configuring array of byte in dll call

    Dear all,
    I need to send the data 2010010100H to the dll at the question mark(U8) i mentioned in the attachment. Actually the data has to be transferred at a time not in bits and pieces. How to do this? I just put an array constant. Is it correct? Kindly give suggestions asap.
    Thanks,
    Mathan
    Attachments:
    1.PNG ‏10 KB

    Hi mathan,
    the dll call in your pic only accepts single bytes (U8) - so no way to wire an array!
    why do you create so many new threads, when the topic doesn't change?
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome

Maybe you are looking for

  • Will ATV work with a Sont KE-P42SX1

    I have asked something in this line before but want to do a final check before I go out and hand over money for a ATV. I have a NON-HD Widescreen Sony P42SX1 Plasma TV. It has Component Video Y. PB/CB, Pr/Cr input and 576p signal input. I understand

  • Safari, iChat, Installer Problems!

    I tried the update manager, and it said to update a few things. After the updates, I realized I can't use some things such as safari, ichat, and the installer. At least I downloaded Firefox, so I can use the internet. I'm running a Mac OS X 10.4.11.

  • Hide address bar from iexplorer

    hi, I made this code: String urlPath =   "http://maps.google.com/maps?q="+wdThis.wdGetContext().currentIndirizzoModificaElement().getGeocodifica();      IWDMessageManager msgMgr = wdComponentAPI.getMessageManager();      wdComponentAPI.getComponentIn

  • How to drag and drop to desktop

    Hi experts, please see the following scenario: - We created a report using xCelsius that is technically a Flash file (SWF) - We included this into an iView Is there a possibility to drag and drop it to the Windows Desktop (e.g. Pressing the left mous

  • Will JHS work with JDev 10.1.3.4

    Hi, JDeveloper 10.1.3.4 is scheduled to be released by 15th May. Will JHS work with this version of JDev? If not are there any plans to release a compatible version? Regards Barry