Java tutorial
/* * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * http://www.apache.org/licenses/LICENSE-2.0 */ package com.dc.gameserver.extComponents.Kit.ip; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.UnsupportedEncodingException; import java.util.StringTokenizer; /** * ?? */ public class Util { private static Log log = LogFactory.getLog(Util.class); //WorldLogger /** * ip?? * * @param ip * ?ip * @return ?ip */ public static byte[] getIpByteArrayFromString(String ip) { byte[] ret = new byte[4]; StringTokenizer st = new StringTokenizer(ip, "."); try { ret[0] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF); ret[1] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF); ret[2] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF); ret[3] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF); } catch (Exception e) { log.info("ip??"); } return ret; } /** * @param ip * ip? * @return ?ip */ public static String getIpStringFromBytes(byte[] ip) { try { StringBuffer sb = new StringBuffer(); sb.delete(0, sb.length()); sb.append(ip[0] & 0xFF); sb.append('.'); sb.append(ip[1] & 0xFF); sb.append('.'); sb.append(ip[2] & 0xFF); sb.append('.'); sb.append(ip[3] & 0xFF); return sb.toString(); } catch (Exception e) { return ""; } } /** * ??????? * * @param b * * @param offset * ??? * @param len * ?? * @param encoding * ?? * @return encoding???? */ public static String getString(byte[] b, int offset, int len, String encoding) { try { return new String(b, offset, len, encoding); } catch (UnsupportedEncodingException e) { return new String(b, offset, len); } } }