Solved

java unicode to ascii

Posted on 2001-06-06
4
900 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapBully challenge 6 89
firstChar challenge 13 85
wordappend challenge 8 85
Cipher Configuration on Apache HTTPD 4 22
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now