mysql - What to store an IP as in Java -
i connecting java program mysql database, need ip connect it. hard coding ip bad idea made config. problem not sure store ip in variable. looked around lot of places int, don't see how can correct because integers don't have decimals. should do?
edit: answer mattias f more appropriate situation. being said, i'll explain why you've heard ip address can stored in integer.
an ipv4 address consists of 4 octects, or 4 bytes of data. incidentally, that's how information 32-bit integer can hold. here methods go both ways:
public static int ipstringtointeger (final string ip) { int value = 0; final string[] parts = ip.split ("\\."); (final string part : parts) value = (value << 8) + integer.parseint (part); return value; } public static string ipintegertostring (int ip) { final string[] parts2 = new string[4]; (int = 0; < 4; i++) { system.out.println ("value : " + (ip & 0xff)); parts2[3 - i] = integer.tostring (ip & 0xff); ip >>= 8; } return parts2[0] + '.' + parts2[1] + '.' + parts2[2] + '.' + parts2[3]; }
edit: integer.valueof replace integer.parseint, unihedron
Comments
Post a Comment