gsoap c与java web之间传输字符串中文乱码问题

来源:互联网 发布:unity3d手机游戏逆向 编辑:程序博客网 时间:2024/06/06 02:59

中文乱码,也就是编码问题不一致,我们也不深究用什么soap_set_mode用SOAP_C_MBSTRING还是SOAP_C_UTFSTRING 只需要我们对字符进行转码成一个统一规范就可以正常显示了,比如c端把字符串转码成十六进制字符

代码如下:

char* asc2hex(char *in){char *ret;int i,len;if(in==NULL)return NULL;len=strlen(in);ret=(char*)malloc(len*2+1);for(i=0;i<len;i++)sprintf(ret+i+i,"%02X",*((unsigned char*)in+i));return ret;}


而java客户端就可以写一个解码函数如:

// 转化十六进制编码为字符串 public static String toStringHex(String hexStr) { byte[] baKeyword = new byte[hexStr.length()/2]; for(int i = 0; i < baKeyword.length; i++) { baKeyword[i] = (byte)(0xff & Integer.parseInt(hrexSt.substring(i*2, i*2+2),16)); } try { String str = new String(baKeyword, "gb2312");//linux操作系统设置字符集 } catch (Exception e1) { e1.printStackTrace(); } return str; }