In C++ char is one byte long, so memcpy works ok. But one-byte char is a real pain when it comes to internationalization. Java's jchar is like wchar_t in C++ and 2 bytes long. If you are serious about this conversion and there is no way to avoid it, and it is a real bottleneck, I suggest you to write JNI lib.
Dmitry On Sep 2, 9:18 am, WoodManEXP <[email protected]> wrote: > Hi Dianne, > > Thank you for replying. > > Frankly, I have been unable to figure out how to copy from a byte[] to > a char[] except with round-about methods like > > 1. Forming 2 bytes into a char using bit shifting and or'ing > 2. Using the java.nio classes (which are S L O W) if you need to do > lots of transfers like say 200,000 or more. > > In any event the Android seems really slow becasue a block like > > for(int i=0; i<1000000;i++) { > c[i] = b[i] > > } > > takes like 20-25 seconds to execute (On Intel Pentium, eq #, such a > thing executes in a flash). > > In C++ or even C# you could simply code > > char[] c = new char[1000000]; > .read((char *)c, 2*1000000); > > And it would just lay in the binary data in the array. > > How can such a thing be accomplished for the Android/Java? > > ANY ideas you can pass along are greatly appreciated! > > On Sep 1, 8:44 pm, Dianne Hackborn <[email protected]> wrote: > > > How long does it take to just copy from a byte[] to your own char[] in code? > > > On Tue, Sep 1, 2009 at 5:13 PM, WoodManEXP <[email protected]> wrote: > > > > This is sort of a java question too… > > > > How to read in a large array of chars, quickly? > > > > I have a file on the /sdcard/ with a million chars (2 bytes each) that > > > I would like to read in quickly. It is a binary file written from > > > another system and I cannot change it. > > > > It can be read into a byte[] array extremely quickly with a statement > > > like > > > > // dInStream is an open DataInoutStream > > > // This is a really, really fast operation on Android > > > byte[] rBuffer = new byte[2*1000000]; // Yes I know this is a lot for > > > Android > > > numBytesRead = dInStream.read(rBuffer, 0, 2*1000000); > > > > But then to get the data into a char[] array is evading me. > > > > I have tried lots of stuff from java.nio like ByteBuffers converting > > > to CharBuffers and the get methods and they will do the conversion but > > > they are so very S L O W. > > > > It takes < 1 sec to do the dInStream.read call and then like 30 > > > seconds to walk over the wraped ByteArray.asCharBuffer using the > > > various get methods. > > > > For a sec I thought ByteBuffer.asCharBuffer.array() was going to save > > > the day. But it exceptions out because the wrap of the byte[] array is > > > owned by the ByteBuffer, not the CharBuffer. > > > > Is there any way just to do the dInStream.read directly into the char > > > array or maybe “cast” the byte[] array to a char[] array? > > > > Thanks in advance for any help! > > > -- > > Dianne Hackborn > > Android framework engineer > > [email protected] > > > Note: please don't send private questions to me, as I don't have time to > > provide private support, and so won't reply to such e-mails. All such > > questions should be posted on public forums, where I and others can see and > > answer them.- Hide quoted text - > > > - Show quoted text - --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

