java string gbk_java String gbk编码和c# String gbk 编码难道不一样?

展开全部

您好,提问者:

确实是不一样的,这个时候给你一种也可用于e68a8462616964757a686964616f31333335313231.net对接的加密:/**

* 加密方法

* @param str

* @return

*/

public static String encode(String str){

//根据默认编码获取字节数组

byte[] bytes=str.getBytes();

StringBuilder sb=new StringBuilder(bytes.length*2);

//将字节数组中每个字节拆解成2位16进制整数

for(int i=0;i

sb.append(hexString.charAt((bytes[i]&0xf0)>>4));

sb.append(hexString.charAt((bytes[i]&0x0f)>>0));

}

return sb.toString();

}

/**

* 得到16进制的GBK编码,转换为汉字

* @param hexStr

* @return

*/

public static String decode(String hexStr){

if( null==hexStr || "".equals(hexStr) || (hexStr.length())%2 != 0 ) {

return null;

}

int byteLength = hexStr.length() / 2;

byte[] bytes=new byte[ byteLength ];

int temp=0;

for(int i=0;i

temp = hex2Dec(hexStr.charAt(2*i))*16+hex2Dec(hexStr.charAt(2*i+1));

bytes[i]=(byte)( temp<128 ? temp : temp-256 ) ;

}

return new String(bytes);

}

private static String hexString="0123456789ABCDEF";

private static int hex2Dec(char ch){

if(ch == '0')

return 0;

if(ch == '1')

return 1;

if(ch == '2')

return 2;

if(ch == '3')

return 3;

if(ch == '4')

return 4;

if(ch == '5')

return 5;

if(ch == '6')

return 6;

if(ch == '7')

return 7;

if(ch == '8')

return 8;

if(ch == '9')

return 9;

if(ch == 'a')

return 10;

if(ch == 'A')

return 10;

if(ch == 'B')

return 11;

if(ch == 'b')

return 11;

if(ch == 'C')

return 12;

if(ch == 'c')

return 12;

if(ch == 'D')

return 13;

if(ch == 'd')

return 13;

if(ch == 'E')

return 14;

if(ch == 'e')

return 14;

if(ch == 'F')

return 15;

if(ch == 'f')

return 15;

else return -1;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值