package com.safeluck.aykj.decoder; import com.safeluck.aykj.utils.BytesUtils; import java.io.UnsupportedEncodingException; /** * Created by zhouwei on 2016/12/3. */ public class GBKStringDecoder extends BaseDecoder { public String stripEnd(String str, String stripChars) { int end; if(str != null && (end = str.length()) != 0) { if(stripChars == null) { while(end != 0 && Character.isWhitespace(str.charAt(end - 1))) { --end; } } else { if(stripChars.length() == 0) { return str; } while(end != 0 && stripChars.indexOf(str.charAt(end - 1)) != -1) { --end; } } return str.substring(0, end); } else { return str; } } @Override public String decode(String str) { if(str==null) return null; byte[] bytes = BytesUtils.hexStringToBytes(str); try { String ret = new String(bytes,"gbk").trim(); return ret;// this.stripEnd(ret,"\0"); } catch (UnsupportedEncodingException e) { return null; } } @Override public String encode(String str) { try { if(str==null) return null; return BytesUtils.bytesToHexString(str.getBytes("gbk")); } catch (UnsupportedEncodingException e) { return null; } } }