protobuf转换的数组传进scoket中类型转换错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了protobuf转换的数组传进scoket中类型转换错误相关的知识,希望对你有一定的参考价值。
只有转换才能转成 byte[]数组类型的[8, 100, 16, -17, -65, -67, 3, 26, 5, 55, 56, 57, 55, 57]为什么我转换的话就变成了这样的数组[91, 56, 44, 32, 49, 48, 48, 44, 32, 49, 54, 44, 32, 45, 49, 55, 44, 32, 45, 54, 53, 44, 32, 45, 54, 55, 44, 32, 51, 44, 32, 50, 54, 44, 32, 53, 44, 32, 53, 53, 44, 32, 53, 54, 44, 32, 53, 55, 44, 32, 53, 53, 44, 32, 53, 55, 93],怎么转换成byte[]数组类型的[8, 100, 16, -17, -65, -67, 3, 26, 5, 55, 56, 57, 55, 57]
参考技术A 我需要把protobuf发送出去,需要打包到一个ByteArrayVarint里,数据头是数据长度local__buf=ByteArrayVarint.new()对于PB的Base128Varints编码__buf:writeVInt(request:ByteSize());应该是这样吧?那数据呢?我有一串字符串要放进__buf里面,是用writeByte()?还是writeStringUShort()??我在你的代码里面没发现有用到写字符的方法..追问我想知道的是怎么如图转成[8, 100, 16, -17, -65, -67, 3, 26, 5, 55, 56, 57, 55, 57]这个@OnMessage
某些数组和字符串类型转换(转)
jbytearray转c++byte数组
1 jbyte * arrayBody = env->GetByteArrayElements(data,0); 2 jsize theArrayLengthJ = env->GetArrayLength(data); 3 BYTE * starter = (BYTE *)arrayBody;
jbyteArray 转 c++中的BYTE[]
1 //jbytearray strIn 2 jbyte * olddata = (jbyte*)env->GetByteArrayElements(strIn, 0); 3 jsize oldsize = env->GetArrayLength(strIn); 4 BYTE* bytearr = (BYTE*)olddata; 5 int len = (int)oldsize;
C++中的BYTE[]转jbyteArray
1 //nOutSize是BYTE数组的长度 BYTE pData[] 2 jbyte *by = (jbyte*)pData; 3 jbyteArray jarray = env->NewByteArray(nOutSize); 4 env->SetByteArrayRegin(jarray, 0, nOutSize, by);
jbyteArray 转 char *
char* data = (char*)env->GetByteArrayElements(strIn, 0);
char* 转jstring
-
1 jstring WindowsTojstring(JNIEnv* env, char* str_tmp) 2 { 3 jstring rtn=0; 4 int slen = (int)strlen(str_tmp); 5 unsigned short* buffer=0; 6 if(slen == 0) 7 { 8 rtn = env->NewStringUTF(str_tmp); 9 } 10 else 11 { 12 int length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str_tmp, slen, NULL, 0); 13 buffer = (unsigned short*)malloc(length*2+1); 14 if(MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str_tmp, slen, (LPWSTR)buffer, length) > 0) 15 { 16 rtn = env->NewString((jchar*)buffer, length); 17 } 18 } 19 if(buffer) 20 { 21 free(buffer); 22 } 23 return rtn; 24 }
下面这个没有用过,刚看到,也写进来,以后如果遇到可以验证下看。
jstring 转 char* 或者 const char*
1 // jstring str 2 const char *key = env->GetStringUTFChars(str, 0); 3 //jboolean isOffer 4 jsClient->modify(key, isOffer); 5 env->ReleaseStringUTFChars(str, key);
JNI 返回 jbyteArray
1 JNIEXPORT jbyteArray JNICALL Java_Test_getByteArray(JNIEnv *env, jobject obj) 2 { 3 jbyteArray firstMacArray = env->NewByteArray( 6 ); 4 ...... 5 jbyte *bytes = env->GetByteArrayElements( firstMacArray, 0); 6 for ( int i = 0; i < sizeof( pAdapterInfo->Address ); i++ ) 7 { 8 bytes[ i ] = pAdapterInfo->Address[ i ]; 9 } 10 11 env->SetByteArrayRegion(firstMacArray, 0, 6, bytes ); 12 return firstMacArray; 13 }
//jstring to char*
1 char* jstringTostring(JNIEnv* env, jstring jstr) 2 { 3 char* rtn = NULL; 4 jclass clsstring = env->FindClass("java/lang/String"); 5 jstring strencode = env->NewStringUTF("utf-8"); 6 jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B"); 7 jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode); 8 jsize alen = env->GetArrayLength(barr); 9 jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE); 10 if (alen > 0) 11 { 12 rtn = (char*)malloc(alen + 1); 13 memcpy(rtn, ba, alen); 14 rtn[alen] = 0; 15 } 16 env->ReleaseByteArrayElements(barr, ba, 0); 17 return rtn; 18 }
//char* to jstring
1 jstring stoJstring(JNIEnv* env, const char* pat) 2 { 3 jclass strClass = env->FindClass("Ljava/lang/String;"); 4 jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V"); 5 jbyteArray bytes = env->NewByteArray(strlen(pat)); 6 env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat); 7 jstring encoding = env->NewStringUTF("utf-8"); 8 return (jstring)env->NewObject(strClass, ctorID, bytes, encoding); 9 }
//将jstring类型转换成windows类型
1 char* jstringToWindows( JNIEnv *env, jstring jstr ) 2 { 3 int length = (env)->GetStringLength(jstr ); 4 const jchar* jcstr = (env)->GetStringChars(jstr, 0 ); 5 char* rtn = (char*)malloc( length*2+1 ); 6 int size = 0; 7 size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL ); 8 if( size <= 0 ) 9 return NULL; 10 (env)->ReleaseStringChars(jstr, jcstr ); 11 rtn[size] = 0; 12 return rtn; 13 }
//将windows类型转换成jstring类型
1 jstring WindowsTojstring( JNIEnv* env, char* str ) 2 { 3 jstring rtn = 0; 4 int slen = strlen(str); 5 unsigned short * buffer = 0; 6 if( slen == 0 ) 7 rtn = (env)->NewStringUTF(str ); 8 else 9 { 10 int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 ); 11 buffer = (unsigned short *)malloc( length*2 + 1 ); 12 if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length ) >0 ) 13 rtn = (env)->NewString( (jchar*)buffer, length ); 14 } 15 if( buffer ) 16 free( buffer ); 17 return rtn; 18 } 19 /*JNIEXPORT jstring JNICALL Java_test_cs_web_SWIFTAlianceCASmfTest_strcal 20 (JNIEnv *env, jclass obj, jstring jstr1, jstring jstr2) 21 { 22 jbyteArray bytes = 0; 23 jthrowable exc; 24 char *pszResult = NULL; 25 char *pszSTR1 = NULL; 26 char *pszSTR2 = NULL; 27 28 pszSTR1 = jstringTostring(env, jstr1); 29 pszSTR2 = jstringTostring(env, jstr2); 30 31 32 int nlen = sizeof(char)*(strlen(pszSTR1)+strlen(pszSTR2)); 33 pszResult = (char*)malloc(nlen); 34 strcpy(pszResult, pszSTR1); 35 strcat(pszResult, pszSTR2); 36 37 jstring jstrRe = stoJstring(env, pszResult); 38 free(pszSTR1); 39 free(pszSTR2); 40 free(pszResult); 41 return(jstrRe); 42 } 43 */
jni object的使用
每一个jni格式的dll中的object对应该java里面的一个类。
如下例有一个 ObjData类,类中有成员bData ,Len
public class ObjData {
public byte[] bData;
public int Len;
}
//------------------------jni获得传过来的Object类型的变量objDataIn--------
jclass clazz =(env)->FindClass("ObjData");
//从传进来的对象中取出byte[]
1 jfieldID byteData = (env)->GetFieldID(clazz,"bData","[B"); 2 jbyteArray pDataIn = (jbyteArray) (env)->GetObjectField(objDataIn, byteData); 3 jsize theArrayLeng = env->GetArrayLength(pDataIn);
//byte[]转为BYTE[]
1 jbyte * arrayBody = env->GetByteArrayElements(pDataIn,0); 2 BYTE * jDataIn = (BYTE *)arrayBody;
//将BYTE数组转为jarray
1 jbyte* byte = (jbyte*)jDataOut; 2 jbyteArray jarray = env->NewByteArray(theArrayLeng); 3 env->SetByteArrayRegion(jarray, 0, theArrayLeng, byte);
//给每一个实例的变量付值
1 (env)->SetObjectField(objDataIn,byteData,jarray); 2 (env)->SetIntField(objDataIn,pDataInLen,jDataInLen); 3 (env)->ReleaseByteArrayElements(pDataIn, arrayBody, 0);
其他参考:
Java 通过JNI调用C或者CPP代码
http://blog.csdn.net/kenera/archive/2009/02/16/3895343.aspx
http://apps.hi.baidu.com/share/detail/15732549
http://dniit.blog.163.com/blog/static/28012894200842810332491/
http://hi.baidu.com/liangwind/blog/item/7dcce2c9729d1d1e7f3e6f49.html
以上是关于protobuf转换的数组传进scoket中类型转换错误的主要内容,如果未能解决你的问题,请参考以下文章