• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] how to get the last digit of a player's IP
#2
Easiest (and, afaik the best) way would be with sscanf:
Code:
new ip[] = "192.168.2.10"; ip_part1, ip_part2, ip_part3, ip_part4 ; sscanf(ip, "p<.>dddd", ip_part1, ip_part2, ip_part3, ip_part4); //OR, if you (as the title suggests) really just want the last part: sscanf(ip, "p<.>{ddd}d", ip_part4); //ip_part1 = 192 //ip_part2 = 168 //ip_part3 = 2 //ip_part4 = 10

I read your post after placing this.. (I did quickly go over it but didn't really read it, oops) how does strfind() not work for you? It either returns -1 (string you're looking for not found), or it returns the first character of the found string (or according to the wiki: "The number of characters before the sub string (the sub string's start position) ", so basically the same thing)
Code:
new ip[] = "192.168.1.200"; if (strfind(ip, "192.168.1") == 0) //0, because "192.168.1" should be found in "192.168.1.200". //That found character 'has no characters before the sub string' (so, the found string started at index 0) { //OK, Accepted LAN IP } else { //NOK, VPN? }

To come back to the sscanf example, this would be an alternative way:
Code:
new ip[] = "192.168.1.200"; ip_part1, ip_part2, ip_part3, ip_part4 ; sscanf(ip, "p<.>dddd", ip_part1, ip_part2, ip_part3, ip_part4); if (ip_part1 == 192 && ip_part2 == 168 && ip_part3 == 1 && (1 <= ip_part4 <= 255)) { //OK, Accepted LAN IP } else { //NOK, VPN? }
.
  Reply


Messages In This Thread
RE: how to get the last digit of a player's IP. - by Kwarde - 2021-07-15, 11:09 PM