Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

java unicode to ascii

Posted on 2001-06-06
4
918 Views
Last Modified: 2010-03-31
hi all,
i am writing a program in java
and within this program i want
to send a string to a dll (wich i call from the java code)
the problem occure when i show the string within the
dll it shows rubbish because the dll
doesnt support in unicode ( and in this dll it cant be changed)
so my question is :
how can i convert from unicode to ascii?

tnx :-)
0
Comment
Question by:simple979
  • 2
4 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 6158882
Convert your String to an array of bytes and pass this to your dll.

byte[] bytes = string.getBytes();

Hope this helps :)

0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 6159146
Assuming that you are using JNI to mediate between your Java code and the DLL, you can use the following C++ code to convert from jstring (java.lang.String) representation to native representation.

Although this code uses the String.getBytes method to retrieve the platform-default encoded byte[] form of the String content, this is not the same as a native byte[] containing the data. Remember that Java arrays are objects, not just hunks of memory. There is no requirement that a JVM implementation store primitive arrays in contiguous memory within the Java object. But your native code will almost certainly presume that a "string" occupy consecutive bytes in memory. That's one reason why the code appears to go through some gyrations to achieve the desired effect. Also note that the byte[] returned from String.getBytes is not 0-terminated as your native code will also likely expect. So 0-termination is added as well.

// Borrowed unabashedly from the JNI Programmer's Guide
char *JNU_GetStringNativeChars(JNIEnv *env, jstring jstr) {
  static jmethodID midStringGetBytes = 0;
  jbyteArray bytes = 0;
  jthrowable exc;
  char *nstr = 0;
 
  if (midStringGetBytes == 0) {
    // Lookup and cache the String.getBytes() method id.
    jclass stringClass = env->FindClass("java/lang/String");
    if (stringClass == 0) {
      // An exception will have been thrown.
      return 0;
    }
    midStringGetBytes = env->GetMethodID(stringClass, "getBytes", "()[B");
    env->DeleteLocalRef(stringClass);
    if (midStringGetBytes == 0) {
      // An exception will have been thrown.
      return 0;
    }
  }

  bytes = (jbyteArray)env->CallObjectMethod(jstr, midStringGetBytes);
  exc = env->ExceptionOccurred();
  if (exc == 0) {
    // Attempt to malloc enough room for the length of the Java
    // string plus one byte for the 0-terminator.
    jint len = env->GetArrayLength(bytes);
    nstr = (char *)malloc(len + 1);
    if (nstr == 0) {
      // malloc failed -- throw an OutOfMemoryError
      JNU_ThrowByName(env, "java/lang/OutOfMemoryError", 0);
      env->DeleteLocalRef(bytes);
      return 0;
    }
    // copy to the malloc'd array and 0-terminate
    env->GetByteArrayRegion(bytes, 0, len, (jbyte *)nstr);
    nstr[len] = 0;
  } else {
    // cleanup
    env->DeleteLocalRef(exc);
  }
  // cleanup
  env->DeleteLocalRef(bytes);
  return nstr;
}

// Borrowed unabashedly from the JNI Programmer's Guide
void JNU_ThrowByName(JNIEnv *env, const char *name, const char *msg) {

  jclass classForName = env->FindClass(name);
  // If cls is null, an exception has already been thrown.
  if (classForName != (jclass)0) {
    env->ThrowNew(classForName, msg);
  }
  // cleanup
  env->DeleteLocalRef(classForName);
  return;
}

Best regards,
Jim Cakalic
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 8898969


No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


--  Split points between objects and jim_cakalic


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 8902915
Hmm. Well OK.
Jim
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
simple java question 3 59
check java version using powershell 13 177
ejb example issues 3 27
jsp login check 12 43
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question