C # C + + Java interface type conversions

Source: Internet
Author: User

Recent days to do a part-time, mainly the C language and C # language encapsulated DLL, after C + + to the Java language Support, the main problem is the type conversion, which lists the common types of conversion process.

1#include"stdafx.h"2#include <cstdlib>3#include <cstdio>4#include <iostream>5#include"Jstring_cstring_char_ptr.h"6#include <malloc.h>7#include <Windows.h>8 9 using namespacestd;Ten using namespacesystem::runtime::interopservices; One using namespaceSystem; A  - //char*->c# -system::string^ charptrtocstring (Const Char*str) the { -system::string^ str1 = Marshal::P trtostringansi ((INTPTR) (Char*) str); -     returnstr1; - } +  -  + //c#->char* A Char* CSTRINGTOCHARPTR (system::string^s) at { -     Char* Buffer = (Char*)(void*) Marshal::stringtohglobalansi (s); -     returnbuffer; - } -  -  in //jstring->char* - Char* JSTRINGTOCHARPTR (JNIENV *env, jstring jstr) to{//utf8/16 conversion into gb2312 +     intLength = (env)getstringlength (JSTR); -     Constjchar* jcstr = (env)->getstringchars (JSTR,0 ); the     Char* Rtn = (Char*)malloc(length*2+1 ); *     intSize =0; $Size = WideCharToMultiByte (CP_ACP,0, (LPCWSTR) jcstr, Length, RTN, (length*2+1), NULL, NULL);Panax Notoginseng     if(Size <=0 ) -         returnNULL; the(ENV)Releasestringchars (Jstr, jcstr); +Rtn[size] =0; A     returnRtn; the } +  - //char*->jstring $Jstring charptrtojstring (jnienv* env,Const Char*str) ${//gb2312 conversion into UTF8/16 -Jstring RTN =0; -     intSlen =strlen (str); theUnsigned Short* Buffer =0; -     if(Slen = =0 )WuyiRTN = (env)Newstringutf (str); the     Else -     { Wu         intLength = MultiByteToWideChar (CP_ACP,0, (LPCSTR) str, slen, NULL,0 ); -Buffer = (unsigned Short*)malloc(length*2+1 ); About         if(MultiByteToWideChar (CP_ACP,0, (LPCSTR) str, Slen, (LPWSTR) buffer, length) >0 ) $RTN = (env)->newstring (jchar*) buffer, length); -     } -     if(buffer) -          Free(buffer); A     returnRtn; + } the  - //Jchar->char $ CharJchartochar (Jchar c) the { the     CharCH = (Char) (C &0x00ff); the     returnch; the } -  in //char*->jchar theJchar Chartojchar (Charc) the { AboutJchar ch = (Jchar) (c |0xff00); the     returnch; the } the  + //Char->c# -System::char Chartochar (Charc) the {BayiSystem::char C = (System::char) ((int) (c)); the     returnC; the } -  - //C#->char the CharChartochar (System::char c) the { the     CharC = (Char)((int) (c)); the     returnC; -}

C # C + + Java interface type conversions

Related Article

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.