Convert StringBuffer to char*

Hi,
I try to convert a StringBuffer field to a char* in C++.
But it doesn't work at all.
I try to get the String of the StringBuffer and then convert this String with UTF.
I post my code. If anyone can help, please ....
     jfieldID fidLoN = env->GetFieldID(cls, "pszLogicalName", "Ljava/lang/StringBuffer;");
     jobject strBuff = (jobject) env->GetObjectField(pModelInfo, fidLoN);          
     jclass jclsStrBuf = env->FindClass("Ljava/lang/StringBuffer;");
     jmethodID jmethToStr = env->GetMethodID(jclsStrBuf, "toString", "()Ljava/lang/String;");
     jstring str1 = NULL;
     env->CallObjectMethod(str, jmethToStr, str1);
     const char *buf1 = env->GetStringUTFChars(str1,0);
     strcpy(BUF1, buf1);
// C++ structure
     modInf.pszLogicalName = (char*) BUF1;
Thanks a lot
Zauz

void testmethod(char *s) {
  if(s != NULL) {
    printf("Received: %s\n", s);
    sprintf(s, "hello world");
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java_exampleJNI_testmethod(JNIEnv *jenv, jclass jcls, jobject jarg1) {
    char *arg1 ;
    (void)jenv;
    (void)jcls;
        arg1 = NULL;
        if(jarg1 != NULL) {
            /* Get the String from the StringBuffer */
            jmethodID setLengthID;
            jclass sbufClass = (*jenv)->GetObjectClass(jenv, jarg1);
            jmethodID toStringID = (*jenv)->GetMethodID(jenv, sbufClass, "toString", "()Ljava/lang/String;");
            jstring js = (jstring) (*jenv)->CallObjectMethod(jenv, jarg1, toStringID);
            /* Convert the String to a C string */
            const char *pCharStr = (*jenv)->GetStringUTFChars(jenv, js, 0);
            /* Take a copy of the C string as the typemap is for a non const C string */
            jmethodID capacityID = (*jenv)->GetMethodID(jenv, sbufClass, "capacity", "()I");
            jint capacity = (*jenv)->CallIntMethod(jenv, jarg1, capacityID);
            arg1 = (char *) malloc(capacity+1);
            strcpy(arg1, pCharStr);
            /* Release the UTF string we obtained with GetStringUTFChars */
            (*jenv)->ReleaseStringUTFChars(jenv,  js, pCharStr);
            /* Zero the original StringBuffer, so we can replace it with the result */
            setLengthID = (*jenv)->GetMethodID(jenv, sbufClass, "setLength", "(I)V");
            (*jenv)->CallVoidMethod(jenv, jarg1, setLengthID, (jint) 0);
    testmethod(arg1);
        if(arg1 != NULL) {
            /* Append the result to the empty StringBuffer */
            jstring newString = (*jenv)->NewStringUTF(jenv, arg1);
            jclass sbufClass = (*jenv)->GetObjectClass(jenv, jarg1);
            jmethodID appendStringID = (*jenv)->GetMethodID(jenv, sbufClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
            (*jenv)->CallObjectMethod(jenv, jarg1, appendStringID, newString);
            /* Clean up the string object, no longer needed */
            free(arg1);
            arg1 = NULL;
#ifdef __cplusplus
#endifJava code:
class exampleJNI {
  public final static native void testmethod(StringBuffer jarg1);
public class example {
  public static void testmethod(StringBuffer SBUF) {
    exampleJNI.testmethod(SBUF);
public class main {
  public static void main(String argv[]) {
    System.loadLibrary("example");
    StringBuffer sb = new StringBuffer(20);
    sb.append( "Java message" );
    example.testmethod(sb);
    System.out.println("returned: " + sb);
}Run main and you should get:
Received: Java message
returned: hello world
Code adapted from one of the SWIG examples.

Similar Messages

  • Converting a 6 char string to date

    Please help..
    How can I convert a 6 char date (ddmmyy) to date format
    that can be passed as input to function module?

    Hi,
    Try FM,
    CONVERT_DATE_TO_INTERNAL
    Run this sample code,
    DATA:
      my_date(6) TYPE c VALUE '300708',
      w_date     TYPE sy-datum.
    CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
      EXPORTING
        date_external                  = my_date
    *   ACCEPT_INITIAL_DATE            = ACCEPT_INITIAL_DATE
    IMPORTING
       date_internal                  = w_date
    EXCEPTIONS
       date_external_is_invalid       = 1.
    WRITE: w_date.
    Regards
    Adil

  • Converting int to char

    Hi!..
    I have a problem!...
    I need to convert integers to chars, but i`m using char(int)....
    So, only accept until ascii code 128..
    Please, What can I do?
    This is my code:
    Thanks!
    public Convert() {
    public static void main(String[] args) {
    Convert convert1 = new Convert();
    System.out.print(f_Transforma("ABCD"));
    public static String f_Transforma(String Password)
    int IntStrLong=Password.length();
    String StrResultado=new String();
    while (IntStrLong >=1)
    int tmpchar=(int)Password.charAt(IntStrLong-1); //valor ascii
    tmpchar=256-tmpchar+IntStrLong;
    String aChar = new Character((char)tmpchar).toString(); //ASCII code to String
    StrResultado=StrResultado+aChar;
    IntStrLong-=1;
    return (StrResultado);
    }

    I did what you told me and nothing happened...
    int tmpchar=(int)Password.charAt(IntStrLong-1); //valor ascii
    tmpchar=256-tmpchar+IntStrLong;
    tmpchar=tmpchar>>8;
    String aChar = new Character((char)tmpchar).toString(); //ASCII code to String

  • 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 to convert ASCII to CHAR?

    Hi, experts.
    Now I want to convert ASCII to CHAR, can you give me some methods? And I also want to know what's my ABAP Version.
    Thanks in advance!

    *After look so bad code that no work i did my own code, a gift for all you:*
    FORM CHARACTER_ASCII   using    p_letra
                                                 changing p_nro   type i.
    field-symbols: <n> type x.
    assign p_letra to <n> casting.
    move <n> to p_nro.
    ENDFORM.
    FORM ASCII_CHARACTER using    p_nro type i
                                               changing p_letra.
    DATA: c    TYPE c,
          x(4) TYPE x.
    FIELD-SYMBOLS: <fc>  TYPE c.
    x = p_nro.
    ASSIGN x to <fc> CASTING TYPE c.
    MOVE <fc>+1(1) to p_letra.
    ENDFORM.

  • Converting raw to char

    Hi SDN,
    I have to convert raw to char.
    data : lv_raw(16) type RAW.
    data : lv_char(16) type c.
    lv_raw = some value.
    i want to convert this value in lv_raw to lv_char in character format.
    please let me know.
    i tried the FM BANK_API_PP_UT_CHAR16_TO_RAW16 but no use.
    Regards,
    Rahul Wagh

    HI Vikranth,
    i tried that FM also but it is not giving me correct value.
    my hex value = 16D7A4FCA7442DDA3AD93C9A726597E4
    the character value for this should come as = test1234
    but FM given by u gives me junk values.
    is there any other way for this
    Regards,
    Rahul

  • Convert number to char

    Hi,
    I need to format numbers when converting it to char.
    I need to show the number with a precision of 3, and when between -1 and 1 to show the 0.
    ex:
    0.234
    -0.234
    1.220
    not .234
    thanks in advance

    I need to show the number with a precision of 3, and when between -1 and 1 to show the 0.This ?
    SQL> with t as (
    select 0.234 n from dual union all
    select -0.234 from dual union all
    select  13.22 from dual union all
    select 1.220 from dual
    select n, to_char(n, '990.000')  n2 from t
             N N2     
          ,234    0.234
        -0,234   -0.234
         13,22   13.220
          1,22    1.220
    4 rows selected.

  • Converting quantity to char

    How to convert quantity to char without using FM's like C14W_CHAR_NUMBER_CONVERSION,
    C14W_NUMBER_CHAR_CONVERSION,
    CHAR_INT_CONVERSION as these FM's do not exist in my server?

    Hi...
    this is my program...try this code....
    Parameters:
      p_Number(9) type c.                  " Input string.
    data:
      w_number(9) type c,                  " Input number
      w_strlen(9) type c,                  " Strlen
      w_numc(9)   type n,                  " Digit string
      w_2bit(2)   type c.                  " first 2 positions
      w_number = p_number.
      w_numc = p_number.
      write: 'The given number in words:  '.
      do 5 times.
        case sy-index.
          when 1.
            w_2bit = w_numc+0(2).
          when 2.
            w_2bit = w_numc+2(2).
          when 3.
            w_2bit = w_numc+4(2).
          when 4.
            w_2bit+0(1) = 0.
            w_2bit1(1) = w_numc6(1).
          when 5.
            w_2bit = w_numc+7(2).
        endcase.                           " CASE SY-INDEX.
       if w_2bit+0(2) ne 0.
         if w_2bit le 19.
           case w_2bit.
            when '01'.
              write 'ONE'.
            when '02'.
               write 'TWO'.
            when '03'.
               write 'THREE'.
            when '04'.
               write 'FOUR'.
            when '05'.
               write 'FIVE'.
            when '06'.
               write 'SIX'.
            when '07'.
               write 'SEVEN'.
            when '08'.
               write 'EIGHT'.
            when '09'.
               write 'NINE'.
            when '10'.
               write 'TEN'.
            when '11'.
               write 'LEVEN'.
            when '12'.
               write 'TWELVE'.
            when '13'.
               write 'THIRTEEN'.
            when '14'.
               write 'FOURTEEN'.
            when '15'.
               write 'FIFTEEN'.
            when '16'.
               write 'SIXTEEN'.
            when '17'.
               write 'SEVENTEEN'.
            when '18'.
               write 'EIGHTEEN'.
            when '19'.
               write 'NINETEEN'.
           endcase.                        " CASE 2BITS POSITIONS 1,2.
        else.
          case w_2bit+0(1).
            when '2'.
               write 'TWENTY'.
            when '3'.
               write 'THIRTY'.
            when '4'.
               write 'FOURTY'.
            when '5'.
               write 'FIFTY'.
            when '6'.
               write 'SIXTY'.
            when '7'.
               write 'SEVENTY'.
            when '8'.
                write 'EIGHTY'.
            when '9'.
               write 'NINETY'.
          endcase.                         " CASE W_2BIT+0(1)
        if w_2bit+1(1) ne 0.
           case w_2bit+1(1).
             when '1'.
                write 'ONE'.
             when '2'.
                write 'TWO'.
             when '3'.
                write 'THREE'.
             when '4'.
                write 'FOUR'.
             when '5'.
                write 'FIVE'.
             when '6'.
                write 'SIX'.
             when '7'.
                write 'SEVEN'.
             when '8'.
                write 'EIGHT'.
             when '9'.
                write 'NINE'.
             when others.
                write ' '.
            endcase.                       " CASE W_2BIT+1(1)
         endif.                            " IF W_2BIT+1(1)
      endif.                               " IF 2BIT LE 19
      case sy-index.
          when 1.
            write 'CRORES'.
          when 2.
            write 'LAKHS'.
          when 3.
            write 'THOUSANDS'.
          when 4.
            write 'HUNDREDS'.
       endcase.                            " CASE SY-INDEX
    endif.                                " IF 2BIT NE 0

  • Convert 14000 wide char table to 255 wide char table

    How to convert 14000 wide char table to 255 wide char table.
    I have used QCE1_CONVERT which is throwing exception - convert not possible.
    Please suggest.
    Thanks a lot.

    You haven't provided that much info but I guess you are looking for something like FM RSDG_WORD_WRAP does, correct?

  • URG: Converting encoded greek char from HTML page to greek char

    String geekvalues = "&# 966;&# 948;&# 945;&# 963;&# 966;&# 945;&# 963;&# 948;&# 966;";
    ( i have inserted space b/w &# ddd; because while framing the query they get converted to
    greek char)
    I parse the following string and insert the integer values in the following
    o/p stream with UTF8 ecoding.
    I can see proper greek char in the file, but when i read the contents of the file
    and try inserting these values to the DB, they appear as ���������(not what i see on my m/c) some junk char.
    bufferWriter = new BufferedWriter
    (new OutputStreamWriter(new FileOutputStream("d:\\tempAA.txt",false),"UTF8"));
    bufferWriter.write(x);
    bufferReader = new BufferedReader
    (new InputStreamReader(new FileInputStream("d:\\tempAA.txt"),"UTF8"));
    retGreekValue =bufferReader.readLine();
    String sqlStr = "INSERT INTO test_table VALUES('"+retGreekValue+"')";
    Could anybody suggest how do i insert proper greek characters in Database from the encoded string from browser.
    Thanks in advance,

    You could try the Internationalization forum. Although the question "I need to get data in script X from my database through the Internet to a browser and back, and it doesn't work" is asked frequently there, and never seems to get an answer.

  • Help! java novice, i need to convert boolean to char

    I have multiple checkboxes. Depending on what checkbox is selected, I need to convert that true boolean data to char data like 'L' or 'B'.
    If anyone can, please help. I would greatly appreciate it. Thanks.

    I'm not quite sure what you mean - boolean has only 2 states, true and false. A char can be any character. How about:
    if (someBooleanVar == true){
    someCharVar = "L";
    }else{
    someCharVar = "B";
    Again, I don't understand what you're doing, but it seems that you need to test the values of the checkboxes, and based on the values, you need to assign values to some char variables. Don't waste you're time trying to converts booleans to chars - it ain't gonna happen.

  • HOW can I convert int value char TO String

    I am trying to study Java by books, but sometimes it is quite difficult to find answers even to simple questions...
    I am writing a program that analyzes a Chinese text in Unicode. What I get from the text is CHAR int value (in decimals), and now I need to find them in another text file (with RegEx). But I can't find a way how to convert an INT value of a char into a String.
    Could you help me?
    Thank you in advance.

    You are confusing matters a bit. A char is a char is a char, no matter
    how you represent it. Have a look at this:char a= 'A';
    char b= 65;Both a and b have the same value. The representation of that value
    can be 'A' or 65 or even (char)('B'-1), because the decimal representation
    of 'B' is 66. Note the funny construct (char)(...). This construct casts
    the value ... back to the char type. Java performs all non-fraction
    arithmetic using four byte integers. The cast transforms it back to
    a two byte char type.
    Strings are just concatenations of zero or more chars. the charAt(i)
    method returns you the i-th char in a string. Have a look:String s= "AB";
    char a= 'A';
    char b= (char)(a+1);
    if (a == s.charAt(0)) System.out.println("yep");
    if (b == s.charAt(1)) System.out.println("yep");This piece of code prints "yep" two times. If you want to compare two
    Strings instead, you have to do this:String s= "AB";
    char a= 'A';
    char b= 'B';
    String t= ""+a+b;
    if (s.equals(t)) System.out.println("yep");Note the 'equals' method instead of the '==' operator. Also note that
    string t was constructed using those two chars. The above was a
    shorthand of something like this:StringBuffer sb= new StringBuffer("");
    sb.append(a);
    sb.append(b);
    String t= sb.toString();This is all handled by the compiler, for now, you can simply use the '+'
    operator if you want to append a char to a string. Note that it is not
    very efficient, but it'll do for now.
    Does this get your started?
    kind regards,
    Jos

  • Convert Float to Char

    Hi,
    I'm having problem when converting a float variable to char - it seems like the float value is automatically rounded into the char variable, but I don't want that.
    Does anybody know a wafunction or a way of avoiding this convertion round?
    Thanks,
    Nuno Afonso

    With the input parameters you gave me...
    Error in ASSIGN: Memory protection error
    The current program "SAPLSCFL " uses the ASSIGN statement. 
    However, the offset of the segment (536) plus the length (1)
    is greater than the length of the segment (536).           
    This is not allowed.                                       

  • How does LabVIEW convert string to char* when passing them to a Call Library Node?

    I have a program which calls C++-based libraries which themself use external libraries (Qt). I pass strings to my library through the Call Library Node, and was wondering how the formatting goes? I have to interpret the char* according to Latin1, UTF-8, UTF-16, ... to convert them to a string which the Qt libraries understand. I need to use char* instead of LV strings, because the library is indepent of LabVIEW.
    It seems that interpreting the char* as Latin1 (default) does not work on Korean systems (for one of our customers), which is understandable when you know that the Latin character set does not know Korean signs. Anyone knows how the char* should be interpreted then? In other words, for non-Latin languages, what exactly is passed to the DLL?

    I don't think that we reinterpret your string in anyway, just reformat the data so that it can be passed to your dll. 
    So assuming you are getting text from, say, keyboard editing, the text should be in the ANSI codepage that the system is running under.  That is, if you are running Windows using an English locale, it will be codepage 1252 (Windows Western, Latin 1), if you are running Windows with a Korean codepage iirc it will be codepage 949, Unified Hangul code.
    If you are filling the string with data that you get from an instrument or some other fashion, it could really be anything.
    Here is a list of the codepages that Windows knows about
    http://msdn2.microsoft.com/en-us/library/ms776446(VS.85).aspx
    I do have some experience with Qt as well, and I think that the function you are looking for to create, say, a QString from this is:
    QString::fromLocal8Bit
    but I am not 100% certain about this as I am not a Qt expert.
    Jeff Peters
    LabVIEW R & D
    Message Edited by jpeters on 04-02-2008 12:38 PM

  • Converting Date to Char

    Using MaxDB Version Version: 7.7.04.28
    I am seeing difficulties with the CHAR(a,t) function. I have an SQL statement where I would like to convert the current date to a string and pick only the year information.
    If I understand the documentation correctly, this should work: CHAR(NOW(),'YYYY')
    However, I find that the second parameter is not a year but a locale.
    This works: CHAR(NOW(),USA) and gives 2009-10-09
    So to get the year information I am doing this: SUBSTR(CHAR(NOW(), USA),1,4)
    But this looks like  a hack. The clean way would be to use CHAR(NOW(),'YYYY'). How come this does not work?
    Many thanks for any help.

    > I am seeing difficulties with the CHAR(a,t) function. I have an SQL statement where I would like to convert the current date to a string and pick only the year information.
    >
    > If I understand the documentation correctly, this should work: CHAR(NOW(),'YYYY')
    You don't understand this command correctly, sorry.
    > However, I find that the second parameter is not a year but a locale.
    > This works: CHAR(NOW(),USA) and gives 2009-10-09
    Yes, sure it works.
    The documentation says that exactly this would happen when you use the command as you did.
    Problem in sight?
    > So to get the year information I am doing this: SUBSTR(CHAR(NOW(), USA),1,4)
    > But this looks like  a hack. The clean way would be to use CHAR(NOW(),'YYYY'). How come this does not work?
    What you do there is a hack, but it's not about the data extraction model.
    There is a specific function for what you want to to: YEAR().
    Have a look at
    [Extraction (extraction_function)|http://maxdb.sap.com/doc/7_7/45/583ea6484365d1e10000000a1553f6/content.htm]
    and the bunch of SQL Tutorials on this topic you also find in the documentation.
    Just as a general hint: don't try to do Oracle with any database that is not Oracle.
    Examples:
    sqlcli db77=> select year(now()) from dual
    | EXPRESSION1        |
    | ------------------ |
    |               2009 |
    1 row selected (168.846 msec)
    sqlcli db77=> select month(now()) from dual
    | EXPRESSION1        |
    | ------------------ |
    |                 10 |
    1 row selected (1406 usec)
    sqlcli db77=> select day(now()) from dual
    | EXPRESSION1        |
    | ------------------ |
    |                 10 |
    1 row selected (1234 usec)
    sqlcli db77=> select dayofweek(now()) from dual
    | EXPRESSION1        |
    | ------------------ |
    |                  6 |
    1 row selected (1671 usec)
    sqlcli db77=> select weekofyear(now()) from dual
    | EXPRESSION1        |
    | ------------------ |
    |                 41 |
    1 row selected (885 usec)
    sqlcli db77=> select now() from dual
    | EXPRESSION1                |
    | -------------------------- |
    | 2009-10-10 08:39:17.890000 |
    1 row selected (28.149 msec)
    regards,
    Lars
    Edited by: Lars Breddemann on Oct 10, 2009 8:40 AM

Maybe you are looking for

  • How can I update software on iPhone 3g from ios 3.1.3 to ios 4?

    Recently been given an iPhone 3g. I had to restore the phone to sync to my iTunes. Now a lot of apps will not download as I need at least iOS 4 but the phone is iOS 3. iTunes tells me that this is the latest software for this model when I check for u

  • Popup Applet: Can we used popup applet to list records from other BC

    I have created a popup applet based on contact BC and called it from one control(ex. Add button) in my newly added applet.I am not able to see any records there in popup applet. Can you please suggest what is the issue or can we use popup applet to s

  • JMS CCDT Channel Error

    Hello All, I am trying working JMS CCDT Scenario in SAP PI 7.31. I have done all configuration as per this blog High Availability in JMS Adapter But in Receiver channel I got following error. I have created queues and client channels as per my knowle

  • How can I save an attachment from PDF portfolio from a datagrid display?

    The reason I need this is a client is limited to a certain version of reader where this functionality is missing from the file menu. (10.0.1). I was thinking either a right click on the item (custom context menu), or a save button elsewhere on the fo

  • Ssid access control with WPA Ent and RADIUS author

    Hi, I'd like to control the ssid requested in WPA Enterprise with RADIUS authorization: how to ? Is there an attribute in RADIUS IOS or Cisco Aironet ? thanks