Jstring, String, char* transform function

Source: Internet
Author: User

  1. #include <malloc.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <vcclr.h>
  5. Using namespace System;
  6. Using namespace System::Text;
  7. Jstring to String
  8. string^ Jstringtostr (jnienv* env, jstring jstr)
  9. {
  10. char* str = jstringtostring (env, JSTR);
  11. string^ value = gcnew String (str);
  12. Free (str);
  13. return value;
  14. }
  15. Jstring to char*
  16. char* jstringtostring (jnienv* env, jstring jstr)
  17. {
  18. char* RTN = NULL;
  19. Jclass clsstring = Env->findclass ("java/lang/string");
  20. Jstring Strencode = Env->newstringutf ("Utf-8");
  21. Jmethodid mid = Env->getmethodid (clsstring, "GetBytes", "(ljava/lang/string;) [B");
  22. Jbytearray Barr = (jbytearray) Env->callobjectmethod (Jstr, Mid, Strencode);
  23. Jsize alen = Env->getarraylength (Barr);
  24. jbyte* ba = env->getbytearrayelements (Barr, Jni_false);
  25. if (Alen > 0)
  26. {
  27. RTN = (char*) malloc (alen + 1);
  28. memcpy (RTN, BA, Alen);
  29. Rtn[alen] = 0;
  30. }
  31. Env->releasebytearrayelements (Barr, BA, 0);
  32. return RTN;
  33. }
  34. String to Jstring
  35. Jstring strtojstring (jnienv* env, string^ RTN)
  36. {
  37. pin_ptr<const wchar_t> wch = PtrToStringChars (RTN);
  38. size_t convertedchars = 0;
  39. size_t sizeinbytes = ((rtn->length + 1) * 2);
  40. char *ch = (char *) malloc (sizeinbytes);
  41. errno_t err = wcstombs_s (&convertedchars,
  42. CH, sizeinbytes,
  43. WCH, sizeInBytes);
  44. Jstring js = stringtojstring (env, CH);
  45. Free (CH);
  46. return JS;
  47. }
  48. char* to Jstring
  49. Jstring stringtojstring (jnienv* env, const char* Pat)
  50. {
  51. Jclass strclass = Env->findclass ("ljava/lang/string;");
  52. Jmethodid Ctorid = Env->getmethodid (strclass, "<init>", "([bljava/lang/string;) V");
  53. Jbytearray bytes = Env->newbytearray (strlen (PAT));
  54. Env->setbytearrayregion (bytes, 0, strlen (PAT), (jbyte*) PAT);
  55. jstring encoding = Env->newstringutf ("Utf-8");
  56. return (jstring) env->newobject (strclass, Ctorid, Bytes, encoding);
  57. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.