Convert jstring to char array or int array

Hi all, please help with the following code. I've tested this code with hard coded char array and it works. Now I have to get the char array from parameter "param1" which is passed from Java.
The following code compiled with error message:
error C2223: left of '->GetStringUTFChars' must point to struct/union
JNIEXPORT void JNICALL Java_DongleSet_DongleWrite(JNIEnv *env,jobject obj,jstring param1){
char HaspBuffer[500]=(char)env->GetStringUTFChars(param1,NULL);
int Service=MEMOHASP_WRITEBLOCK;
int SeedCode=300;
int LptNum=0;
int Pass1=pass1;
int Pass2=pass2;
int p1=0;
int p2=12;
int p3=0;
int p4=(int)HaspBuffer;
hasp(Service,SeedCode,LptNum,Pass1,Pass2,&p1,&p2,&p3,&p4);
I've tried this:char HaspBuffer[500]=env->GetStringUTFChars(param1,NULL);
And this:char HaspBuffer[500]=(*env)->GetStringUTFChars(param1,NULL);
All give me errors.
Please help.

char * HaspBuffer=(*env)->GetStringUTFChars(param1,NULL); When writing pure C, you have to supply the JNIEnv as first arg to every JNIEnv function pointer.char * HaspBuffer=(*env)->GetStringUTFChars(env, param1, NULL);Look here: http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/jnistring.html

Similar Messages

  • Converting jstring to char*

    Hi,
    I would like to convert a jstring (which is an argument in a method) to a C++ char*. How to do that?
    I think it should be something like:
    JNIEXPORT void JNICALL
    Java_HelloWorld_displayHelloWorld(JNIEnv* env, jobject obj, jstring str)
    char* text= ???;
    Thank you for your answer :-)

    const char * stringa = env->GetStringUTFChars(_jstringa,null);

  • Converting jstring to char in c

    I'm trying to convert a jstring to const char (a pointer), but I keep getting an exception.
    is there some kind of defenition I'm missing????
    here's part of the code:
    jmethodID methID;
    jstring jstr;
    const char *str;
    /* Get a reference to obj2 class */
    jclass cls = (*env)->GetObjectClass (env, obj2);
    if (cls == NULL){
    return; /*return in case the object couldn't be found */
    methID = (*env)->GetMethodID (env, cls, "getPgmId", "()Ljava/lang/String;");
    jstr = (jstring) (*env) -> CallCharMethod(env, obj2, methID);
    if (jstr == NULL){
    printf (" \nthe result String is null \n");
    fflush(stdout);
    return;
    str = (char)(*env)->GetStringUTFChars (env, jstr, 0);

    your method signature shows that your method returns a string but you are using "CallCharMethod"...you should be using CallObjectMethod since a string in java is actually an object not a datatype.
    this is an example of getting a jstring and converting it to a char*. i need the char* so i do a strdup and release...
    jstring desc = (jstring)env->CallObjectMethod(obj, mid, failType);
    if(desc == NULL){
         cout << "desc == null" << endl;
         return (char*)errorHandler('c', JNI_NO_METHOD);
    char* cDesc = strdup(env->GetStringUTFChars(desc, 0));
    env->ReleaseStringUTFChars(desc, cDesc);

  • Converting "jstring" to "char *" in VC++

    Hello,
    I have a native method whose input parameter is a string. I used the below lines of code to get a char *. These code i got it from the JAVA TUTORIALS only.
    JNIEXPORT jint JNICALL Java_SpringDLL_createExtensionSpringDXF
    (JNIEnv *env, jobject obj, jstring jsprData)
    const char sprData = (env)->GetStringUTFChars(env, jsprData, 0);
    printf("%s", sprData );
    (*env)->ReleaseStringUTFChars(env, jsprData, sprData);
    I am using VC++ Application Wizard to generate the DLL.
    The error i am getting is :
    SpringDLL.cpp(72) : error C2819: type 'JNIEnv_' does not have an overloaded member 'operator ->'
    C:\JDK1.3\INCLUDE\jni.h(746) : see declaration of 'JNIEnv_'
    SpringDLL.cpp(72) : error C2227: left of '->GetStringUTFChars' must point to class/struct/union
    SpringDLL.cpp(100) : error C2819: type 'JNIEnv_' does not have an overloaded member 'operator ->'
    C:\JDK1.3\INCLUDE\jni.h(746) : see declaration of 'JNIEnv_'
    SpringDLL.cpp(100) : error C2227: left of '->ReleaseStringUTFChars' must point to class/struct/union
    Can anybody help me out how to get rid of this error. Please its an very urgent....
    if you can send me an email then please don't hesitate to send it to "[email protected]".
    Thanks,
    Patro

    The code from the tutorials id for C not C++. As you are using C++, use 'env' like this
        jclass     cls=env->GetObjectClass(obj);because when using C++ env is a pointer to an object.

  • Is there an example vi for converting an array of boolean to list an array of Int based on a selected T/F state?

    I would like to use arrays to minimize code.
    I am trying to generate an array of int base on an array of boolean randomly selected.

    You can try using 'Boolean to 0,1.vi', found in the Boolean palette: this vi converts a scalar, array or cluster of booleans to a scalar, array or cluster of word (I16 integers).
    To use it, simply wire an array of booleans (indicator) to its left, and an array of integers (control) to its right et voilà.
    Roberto
    Proud to use LW/CVI from 3.1 on.
    My contributions to the Developer Zone Community
    If I have helped you, why not giving me a kudos?

  • 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 int array to string array?

    Hi,
    Can anyone please help me with this question?
    int number={4,6,45,3,2,77};
    I like to convert this into a list and sort
    List mylist = Arrays.asList(number);
    Collections.sort(mylist);
    But first I need to convert the int array to String array.
    Please advise. Thanks.

    If you want to convert your int array to a String array, you have no choice but doing a conversion element by element in a for loop.
    However, if the sort method doesn't behave as desired, you can use the one with 2 parameters, the second parameter being a comparator class.
    Check the javadoc for more information : http://java.sun.com/j2se/1.3/docs/api/java/util/Collections.html
    /Stephane

  • How to convert String array into int.

    void getSoldSms(Vector vecSoldSms)
         String str[]=new String[vecSoldSms.size()];
         String words[]=new String[str.length]; // String array
              for(int i=0;i< vecSoldSms.size();i++)
                   str=(String)vecSoldSms.get(i);
              }               //End for
              for(int i=0;i<str.length;i++)
              words = str[i].split("\\|\\|");
              System.out.println();
              for(int j=0;j<1;j++)     
              int count[str.length]=Integer.parseInt(words[i]);
              System.out.print(count[j]*advance_count);
              } // end inner for loop
              }          //End for
         }          //End function getSoldSms
    how do i convert words which is a string array into int type. i kno string can be converted into int using interger.parseint. but wat abt string arrays??? plz help me out with the above code.

    i did tht its still giving the same errorFor Heaven's sake, what about taking a second to try to understand the code you're copying first? If you really can't fix the error yourself, you have a more serious problem than just convertingStrings to ints.
    And if you want { "1", "2", "3" } to be 123:
    StringBuffer b = new StringBuffer();
    for (int i = 0; i < array.length; i++) {
      b.append(array);
    int result = Integer.parseIn(b.toString());

  • How do i convert an image object to a byte array ?

    Hi
    how do i convert an image object into a byte array
    early reply apperciated

    Oh sorry my method and the other method need to have the pixels from the Image passed to them which you get my using pixelgrabber:
    //create width and height variables from image
              int w = img.getWidth(this);
              int h = img.getHeight(this);
              //retrive picture from image
              int[] pix = new int[w * h];
              PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pix, 0, w);
              try{ pg.grabPixels();
              } catch (InterruptedException ioe) {
                   System.err.println("Interrupted");
              if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
              System.err.println("image fetch aborted or errored");
              }

  • How to display an Array of ints in an Array of TextFields

    I'm trying to create an array of textfield's, and then create an array of ints and display one of those ints in each textfield. Can anyone please help me?

    Yea after you create the array of textfields and ints, do a for loop with textfield.length limit.
    for(int i = 0; i < textfield.length; i++){
    look in the JTextField API for how to put int into
    a text. I know, but if this is for a class, you have
    figure it out. (You might have to use the Integer object to convert it to a string
    I hope this helps. If you still can't get it, post some code and I'm sure someone will help you through it.
    gl
    kimoS

  • How to conver vector to Int array

    Hi
    I know toArray method can be used to convert vector to an array but what if I want to convert it to an int array?
    Vector v = new Vector();
    int r [] = new int [1];
    v.add(2);
    r = v.toArray()//gives errorHow can I cast it to return int Array rather than object array?

    Vector v = new Vector(10);
    for(int i = 0; i < 10; i++) {
        v.add(i);
    int r[] = new int[v.size()];
    for(int i = 0; i < r.length; i++) {
        String value = v.elementAt(i).toString();
        r[i] = Integer.valueOf( value ).intValue();
        System.out.println(r);

  • To convert an ArrayList into a 2-D array

    Is there any efficient and good technique to convert an ArrayList into a 2-D array? Suppose I have an ArrayList that contains objects of my defined class and then I would like to convert this ArrayList into a 2-D array.
    Can someone please help me out here? Your help will be really appreciated!
    Edited by: ti_ma on Mar 18, 2008 4:59 PM

    i think what some people are trying to say is that it would just be fine to use a for loop; i don't know about any magically fast ways of doing it, but it probably won't matter that much unless you're trying to optimize by like nanoseconds...
    it would just be easy and good to do something to the effect of:
    //assuming your ArrayList is called list
    Object[][] arr = new Object[list.size()][3];
    int i = 0;
    for (Book b : list) {
       arr[0] = b.name;
    arr[i][1] = b.price;
    arr[i++][2] = b.isbn;
    hope that helps!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Reading content of a file into an int array

    How do i use FileReader rd = new FileReader(filename) to read the contents into an int array?

    public static int[] parse_file(String filename)
         Vector vec = new Vector();
         int num[];
         num = new num[vec.size()];
         try {
              BufferedReader br = new BufferedReader(new FileReader(filename));
              String line;
              while((line = br.readLine()) != null) {
              vec.add(line);
              for (int i = 0; i < vec.size(); i++) {
              num[i] = (int) vec.get(i);
              System.out.println("Number=" + num);
         catch(IOException e) {
              System.out.println("error");
         return num;
    Maxsum.java:22: cannot resolve symbol
    symbol : class num
    location: class Maxsum
         num = new num[vec.size()];
    ^
    Maxsum.java:30: inconvertible types
    found : java.lang.Object
    required: int
              num[i] = (int) vec.get(i);
    How do i initialize the array?if i do not initialize it, there'll be array not initialized error also..How do i convert the String to int?

  • Make Array of int's from a String

    Hi,
    Trying to transform a String into an array of ints. The following works, but seems a little longwinded for what must be quite a common function - specifically line 9 in the code (turning a char into an int). Can anyone suggest an easier way?
    public class IntArray {
        public static void main( String[] args ) {
            if ( args.length == 1 ) {
                String s = args[ 0 ] ;
                int sLength = s.length() ;
                int[] argIntArray = new int[ sLength ] ;
                for ( int j = 0 ; j < s.length() ; j++ ) {
                    // This line ...
                    argIntArray [ j ] = Integer.parseInt( s.charAt( j ) + "" );
                for ( int j = 0 ; j < argIntArray.length ; j++ ) {
                    System.out.println( argIntArray[ j ] );
            } else {
                System.out.print( "Please enter an int" );
    }Thanks,
    Rich

    I'm not sure how common it is, but since you are assuming that the input is a String that represents an int...
    public class IntArray {
         public static void main( String[] args ) {
              if ( args.length == 1 ) {
                   String s = args[ 0 ] ;
                   int sLength = s.length() ;
                   int[] argIntArray = new int[ sLength ] ;
                   for ( int j = 0 ; j < s.length() ; j++ ) {
                        // This line ...
                        argIntArray [ j ] = s.charAt( j ) - '0' ;
                        //I suggest checking for <0 or >9
                   for ( int j = 0 ; j < argIntArray.length ; j++ ) {
                        System.out.println( argIntArray[ j ] );
              } else {
                   System.out.print( "Please enter an int" );

  • Returning int array from C to Java as method parameter

    Hello,
    I've been trying to accomplish this
    Java
    int[] myArray = ... (filled with some data);
    int retCode = myNativeCall(myArray);
    if(retCode == 0)
    doSomethingWith(myArray); //myArray gets overwritten with new data (with new size also) during the native call...
    C
    JNIEXPORT jint JNICALL Java_GenChav_rsaEncrypt(JNIEnv *env, jobject obj, jintArray myArray){
    jintArray outbuf;
    int[] new_array = .. // some function will fill this....
    int new_array_length = ...//some function will give me the new size, not big, 512 max...
    jint tmp[new_array_length]; //allocate..need something more ??
    memcpy(tmp, new_array, new_array_lenght);
    outbuf=(*env)->NewIntArray(env, new_array_length);
    (*env)->SetIntArrayRegion(env, outbuf, 0, new_array_length, tmp);
    myArray=outbuf;
    I tought this way I would have a updated myArray ints on the Java program...
    Any thought??

    user5945780 wrote:
    Ok, let's try to be constructive here...
    How I do implement a return type for my method like a int array ?First realized it has nothing to do with JNI. So the same question and answer applies to java only.
    >
    like:
    int[] return = myNativeCall();
    Yes.
    Then I will look for return[0], if it is == to 0, fine, it means a successful calculation by the native code. Not sure what that means. The structure of what you return it up to you.
    It can help when you are unsure of what to do in JNI....write a pseudo representation of what you want to do in a java method. Then translate that java method into JNI, everything in the pseudo method must be in the JNI code.

Maybe you are looking for