Converting a byte[] into a char[]

Hi All,
what's the best way to convert a byte[] into a char[]?
Any advice appreciated. Thanks in advance.

you will need a encoding schema...
byte is binary data, char is interpret from byte by the system...
easiest way without extra coding...
new String(byte[], encoding).toCharArray()
if you don't specify encoding... system default is used.

Similar Messages

  • Converting a byte[] into an int

    I am using the MD5 hash function to create a message digest. When the hash function is complete it returns a byte[]. I want to turn this byte [] into an int, so that I can exclusive or (^) it to a node id according to this traceback algorithm I am trying to implement.
    Basically, I need to turn a byte[] into an int, and then from an int back into a byte[].
    I've looked throughout the forums at different implementations of this, but some do not return the same byte[] as the original, other's have said to use the ByteBuffer, but how do I use it to turn a byte[] into an int?
    Any help would be great, thanks!!!

    Unfortunatly that will not work. As a previousposter
    indicated, you must mask off the lower 8 bits ofeach
    byte after converting to int (or the equivilentmask
    after shifting).Um? Why would the "sign" bit be getting shifted INTO
    the lowest bit?D'oh! Sorry. My bad. When bytes[n] gets "implicitly" cast to do the calculation. Gak! Nevermind.
    Corrected would be:
    iBytesVal = ((bytes[3] & 0xFF) << 24);
    iBytesVal |= ((bytes[2] & 0xFF) << 16);
    iBytesVal |= ((bytes[1] & 0xFF) << 8);
    iBytesVal |= (bytes[0] & 0xFF);

  • Converting a byte[] into Sound

    Hi,
    This is my first post so please be kind. I am an honours student studying Software Engineering and currently working on my dissertation project.
    Briefly I am transmitting sound, image and text over a network but send the image and sound as byte arrays.
    I started to use the sun.audio package to play the sound BUT my code needs to work on a PDA on version 1.3 and my code is in 1.6 so compilation causes an ID mismatch and therefore an error.
    Does anyone know of a way to successfully use the java sound API to successfully transfer a byte array into audio. I originally tried it using SourceDataLine etc but got a muffled/distorted noise. I cannot save anything onto the PDA.
    Any advice or information would be much appreciated.
    Regards,
    Lea

    Hi Again,
    I thought JMF might be able to help and was hoping it would offer a suitable alternative but does not have the handy J2ME style of:
    Player p = Manager.createPlayer(inputstream, "WAV") (Not perfect code I know)
    Does anyone know how you would do this in JMF?
    Regards,
    Lea

  • Byte[] into StringBuffer ?

    This is what I've got:
    new StringBuffer(new String(myByteArray));
    Is this the best way of getting a byte[] into a StringBuffer, or will it suffer from converting it to a String first ? (likely to be between 1,000 and 50,000 bytes in the array).
    Annoyingly, there is no append(byte[]) method in StringBuffer, and you can't cast the byte[] into a char[]. grr.

    Either create a char[] array of the same size of the byte[] array and copy the elements:
    char[] c = new char[b.length];
    for (int i = 0; i < b.length; i++) {
      c[i] = (char)(b[i] & 0xFF);
    }Or convert it to a String (takes a little longer, and StringBuffer will get the chars from it). Just use the right encoding, or you might get "false" String values:
    String s = new String(b, "ISO8859_1");The character encoding ISO 8859-1, will make a one to one mapping between bytes and characters.

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

  • Conver bytes into UIImage

    Hello all,
    I get image data in bytes(0xFFD8....) from Sql Server 2005.
    Now i want to convert that data into UIImage.
    For that i first convert that bytes into NSData then NSData into UIImage.
    But i get null in UIImage.
    Thank you..

    BalusC wrote:
    stevoo wrote:
    Basicly this is what i want to do:
    Convert an image into a byte[]I would rather use an InputStream.Why ? is there any sort of difference ?
    >
    byte[] buffer = new byte[4096];
    // response.addHeader("", "");
    DataInputStream in = new DataInputStream(new FileInputStream("/home/stevoo/Desktop/Net/a.jpg"));
    in.readFully(buffer);
    in.close();i can do it with this, but i am not sure this is the best way.Bad code. DataInputStream is not needed here. Also if that image was larger than 4096 bytes, then your code is simply ignoring the remaining bytes.Yes it is very bad. I have changed everything. This was mostly trial.
    Using ejb, creating an object and persisting the picture in the database. this is working correctly i think.Huh? You thinks? Don't do that. Be sure.
    Well offcourse i think. I can put input stuff into the db but i am not sure if they are correct since i cant retrieve them to see if they were entered correctly.
    and My trouble is retrieving the image.
    I can retrieve them using a simple query, but how will i manage to turn then into an image again and display it?
    I am working with JSP and EJB.Write a servlet which takes the image ID as parameter, gets the stream from the database (by EJB, if need be) and writes it to the outputstream of the response along the correct response headers. Then call that servlet with the image ID as parameter in a HTML <img> element in JSP.
    I have already began work on the servlet. If i have more problem on this one
    In the future, please post Servlet related questions in the Servlet forum.i will post a new thread in the servlet section.
    Thanx :)

  • How to convert a integer into bytes!!!

    I am trying to pack the length of the data transamitted over a stream into 2 bytes (Each byte is a unsigned char in terms of 'C' language). When i am using the bytes in java any value greater than 127 is a negative number (I understand the very definition of 'byte' in java), my question is how to pack the length of the data the 'C' way still following Java rules??
    Example :: Length of the data is 168 say!
    the 2 bytes would be 0x00, 0x80 (the respective ascii values would be NULL, P) but with Java i get some thing different as we know??
    byte 1 = 00 and byte 2 = -88 ( i am not sure what would be the ascii equivalent of this??)
    Please help!

    For what it's worth:package ca.adaptor.util;
    * $Id: ByteUtil.java,v 1.6 2003/03/28 18:24:47 mike Exp $
    * Copyright (c) 2002 by Michael Coury
    *  This library is free software; you can redistribute it and/or
    *  modify it under the terms of the GNU Lesser General Public
    *  License as published by the Free Software Foundation; either
    *  version 2.1 of the License, or (at your option) any later version.
    *  This library is distributed in the hope that it will be useful,
    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    *  Lesser General Public License for more details.
    *  You should have received a copy of the GNU Lesser General Public
    *  License along with this library; if not, write to the Free Software
    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    * @author Michael Coury (mdc)
    * @created Sep 20, 2002
    * @version $Revision: 1.6 $, $Date: 2003/03/28 18:24:47 $
    public final class ByteUtil {
      private static final int  __NUM_BYTES_IN_LONG_   = 8;
      private static final int  __NUM_BYTES_IN_INT_    = 4;
      private static final int  __NUM_BYTES_IN_SHORT_  = 2;
      private static final int  __NUM_BITS_IN_BYTE_    = 8;
      private static final long __BYTE_MASK_           = 0xFF;
      /** PRIVATE CONSTRUCTOR */
      private ByteUtil() {}
                ///// ENCODE /////
       * Converts a number to its binary equivalent and returns that as a byte array
      public static final byte[] toBytes(final long aNum) {
        final byte[] ret = new byte[__NUM_BYTES_IN_LONG_];
        for(int i = 0; i < __NUM_BYTES_IN_LONG_; i++)
          ret[i] = (byte) (aNum >>> ((__NUM_BYTES_IN_LONG_ - 1 - i) * __NUM_BITS_IN_BYTE_));
        return ret;
       * Converts a number to its binary equivalent and returns that as a byte array
      public static final byte[] toBytes(final int aNum) {
        final byte[] ret = new byte[__NUM_BYTES_IN_INT_];
        for(int i = 0; i < __NUM_BYTES_IN_INT_; i++)
          ret[i] = (byte) (aNum >>> ((__NUM_BYTES_IN_INT_ - 1 - i) * __NUM_BITS_IN_BYTE_));
        return ret;
       * Converts a number to its binary equivalent and returns that as a byte array
      public static final byte[] toBytes(final short aNum) {
        final byte[] ret = new byte[__NUM_BYTES_IN_SHORT_];
        for(int i = 0; i < __NUM_BYTES_IN_SHORT_; i++)
          ret[i] = (byte) (aNum >>> ((__NUM_BYTES_IN_SHORT_ - 1 - i) * __NUM_BITS_IN_BYTE_));
        return ret;
       * Converts a number to its binary equivalent and returns that as a byte array
      public static final byte[] toBytes(final double aNum) {
        return toBytes(Double.doubleToRawLongBits(aNum));
       * Converts a number to its binary equivalent and returns that as a byte array
      public static final byte[] toBytes(final float aNum) {
        return toBytes(Float.floatToRawIntBits(aNum));
                ///// DECODE /////
       * Converts a byte array representing a long back to a long
      public static final long toLong(final byte[] aNum) {
        try {
          long ret = 0L;
          for(int i = 0; i < __NUM_BYTES_IN_LONG_; i++) {
            ret += ((long) aNum[i] & __BYTE_MASK_) << ((__NUM_BYTES_IN_LONG_ - 1 - i) * __NUM_BITS_IN_BYTE_);
          return ret;
        catch(Exception e) {
          throw new NumberFormatException("Invalid number of bytes for long.  Found: " + aNum.length
                                          + " Required: " + __NUM_BYTES_IN_LONG_);
       * Converts a byte array representing an int back to an int
      public static final int toInt(final byte[] aNum) {
        try {
          int ret = 0;
          for(int i = 0; i < __NUM_BYTES_IN_INT_; i++)
            ret += (aNum[i] & __BYTE_MASK_) << ((__NUM_BYTES_IN_INT_ - 1 - i) * __NUM_BITS_IN_BYTE_);
          return ret;
        catch(Exception e) {
          throw new NumberFormatException("Invalid number of bytes for int.  Found: " + aNum.length
                                          + " Required: " + __NUM_BYTES_IN_INT_);
       * Converts a byte array representing a short back to a short
      public static final short toShort(final byte[] aNum) {
        try {
          short ret = 0;
          for(int i = 0; i < __NUM_BYTES_IN_SHORT_; i++)
            ret += (aNum[i] & __BYTE_MASK_) << ((__NUM_BYTES_IN_SHORT_ - 1 - i) * __NUM_BITS_IN_BYTE_);
          return ret;
        catch(Exception e) {
          throw new NumberFormatException("Invalid number of bytes for short.  Found: " + aNum.length
                                          + " Required: " + __NUM_BYTES_IN_SHORT_);
       * Converts a byte array representing a double back to a double
      public static final double toDouble(final byte[] aNum) {
        return Double.longBitsToDouble(toLong(aNum));
       * Converts a byte array representing a float back to a float
      public static final float toFloat(final byte[] aNum) {
        return Float.intBitsToFloat(toInt(aNum));
                ///// SHIFTING /////
      public static final byte shiftRightUnsigned(final byte b, final int shift) {
        return (shift > __NUM_BITS_IN_BYTE_) ? 0x00 : (byte) ((b & 0xFF) >>> shift);
      public static final byte shiftRight(final byte b, final int shift) {
        return (byte) (b >> shift);
      public static final byte shiftLeft(final byte b, final int shift) {
        return (shift > __NUM_BITS_IN_BYTE_) ? 0x00 : (byte) (b << shift);
    }

  • FM to convert double byte chars

    Hi All,
    Is there anyone know what are the function module to convert double-byte chars? Thanks.

    Seems like Blue Sky is not clear
    You want to convert what into what?
    Whats the purpose of this requirement, kindly give more details.
    Regards
    Karthik D

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

  • C#: How to convert Keyboard.key (Unicode char as number) into correct char?

    Our application uses barcode scanner.
    when scanning a barcode and convert Keyboard.key (as ushort with value 16, it represents char ':') by using
    1)(char)(Keyboard.key)
    2) or Convert.ToChar(Keyboard.key),
    both way convert into symbol '+'.
    How to convert into ':'?
    What is a correct way to convert Keyboard.key as ushort into correct char? Thx!
    JaneC

    Hi Dan Nemec,
    It is normal '+'. It is special '+' symbol.
    We search a little bit and find out Unicode ':' is 3A and value is not 16.
    So during the process barcode scanner, the value has been changed in some way.
    We are trying to find out where the value has been changed.
    JaneC

  • How to convert bytes[] into File object

    hi
    how to convert byte array into File object
    pls.. help me
    Regards
    srinu

    rrrr007 wrote:
    Hi,
    How to convert bytes[] into multipage File object?? ]There's no such thing as a "multipage File object." You ought to re-read this thread closely, and read the [API docs for File|http://java.sun.com/javase/6/docs/api/java/io/File.html] to clear up your confusion about what a File object is.
    I used the java.io.SequenceInputStream to concatenate two input streams (basically .pdf files) into a single input stream. I need to create a single multipage pdf file using this input stream. Then you need a pdf API, like iText or fop. You can't just concatenate pdf files, word docs, excel sheets, etc., like you can text files. Google for java pdf api.

  • How to convert byte into string

    can any tell me how to convert byte into string
    when im an debugging thid code in eclipse it shows the result in integer format instead of string but in command prompt it is showing result in string format..........plz help
    package str;
    import java.io.*;
    public class Testfile {
    public static void main(String rags[])
    byte b[]=new byte[100];
    try{
    FileInputStream file=new FileInputStream("abc.txt");
    file.read(b,0,50);
    catch(Exception e)
         System.out.println("Exception is:"+e);
    System.out.println(b);
    String str=new String(b);
    System.out.println(str);
    }

    Namrata.Kakkar wrote:
    errors: count cannot be resolved and Unhandled exception type Unsupported Encoding Exception.
    If i write an integer value instead of "count" then Unhandled exception type Unsupported Encoding Exception error is left.This is elementary. You need to go back to [http://java.sun.com/docs/books/tutorial/|http://java.sun.com/docs/books/tutorial/] .

  • How to convert bytes[] into multipage File object

    Hi,
    How to convert bytes[] into multipage File object??
    I used the java.io.SequenceInputStream to concatenate two input streams (basically .pdf files) into a single input stream. I need to create a single multipage pdf file using this input stream.
    Thanks for you help in advance..

    Only text format allows you to concatenate two files together to get a longer files.
    Most formats have a header and a footer and so you cannot simply add one to the other.
    You need to use a PDF API which will allow you to build the new document (if one exists)

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

  • Convert byte[] into int

    Hey!
    How to convert a byte vector to an int?
    Byte[] to int?
    Thanks
    Mikael

    If you want to build an integer value out of four bytes, then I guess you'll need the left shift operator (<<) and bitwise inclusive OR operator (|)

Maybe you are looking for

  • EJB3 - More than one remote interface for Session Bean?

    Hi, Is it possible in EJB3 to have more than one remote interface for a SessionBean? I have seen it posetd on here that it is possible but never been able to find any other information. Using JBoss as my App Server, I have deployed an App, where a Se

  • Purchase order clearing

    Hi Experts, We have some purchase orders for all those users has not done migo postings they have done miro. now they want to clear those purchase orders is it possible? there should be corresponding posting right for clearing? is it possible to clea

  • Best Soundcard for KT3 Ultra ??

    Hi Can you give me your experiences of the most compatible sound cards for this motherboard. I am having problems with my tv tuner card and my SB Live 5.1. I never had these problems in my ECS K75SA. Cheers Fozzee

  • Can you do this???

    I have a SELECT statement that needs a SUM action. However, do get the correct SUM, I have to do a sub-query inside of the SUM statement. Is this possible? I am somewhat a novice to oracle/sql, so any help would be appreciated.Thanks

  • Inserting non-serializable objects

    Is there any way to insert non-serializable objects into a database? (Or to convert them to bytes?)