Convert IPv4 dotted quad into four decimal values - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2) +--- Thread: Convert IPv4 dotted quad into four decimal values (/showthread.php?tid=837) |
Convert IPv4 dotted quad into four decimal values - bert22306 - 09-01-2022 A trivially easy way to enter an IPv4 address into qb64, and extract the four decimals values 0-255 which make up the address, is to use commas as delimiters for each byte of the address. I've done that, and my program (to convert an IP multicast address into its derived MAC group address, per RFC 1112) works fine that way. What would be even better is to be able to enter the IP address in its conventional form, with . instead of , for delimiters. So for example, given the multicast address 239.255.128.1 I'm now entering it as 239,255,128,1 It's okay, super easy to parse, but not really nice. So far, I have this. It inputs the dotted quad as a single string variable, then changes the dots into commas, so that hopefully the four decimal numbers can be easily parsed by qb64. Just trying to figure out an easy way to make the ASCII list of numbers and commas, which you see in the output, into four separate decimal numbers. I'm not proficient in all the niceties of how numbers and characters can be converted in qb64. Any super clever ideas out there? There usually are. Code: (Select All) Dim num(100), char$(100) RE: Convert IPv4 dotted quad into four decimal values - bert22306 - 09-02-2022 Ah, I figured out one way to do this. Break up the string variable into its separate characters, as I have already done, then use digit(j) = Val(char$(i)) to build up the decimal value of each byte of the address. I don't even need to change every dot to a comma. I loop through each address byte until I reach a dot delimiter, and build up the decimal value of that byte. Then move on from there to the next dot delimiter, build up that decimal value. There might be a more clever way, but at least I got this way to work perfectly. Not that much code, once I'm done cleaning it up. Also have to build in checks, to make sure not to exceed valid values. RE: Convert IPv4 dotted quad into four decimal values - SMcNeill - 09-02-2022 If all you need to do is break it up into byte values, then something simple like this: Code: (Select All) _Font _LoadFont("courbd.ttf", 32, "monospace") (And you could simplify that even more, if you just passed it an array to store the values and then ran it in a simple FOR loop.) RE: Convert IPv4 dotted quad into four decimal values - bert22306 - 09-02-2022 Thanks Steve. That works really well too. (Except for the gymongous font you use.) I have to save it for future use. Thanks again. RE: Convert IPv4 dotted quad into four decimal values - SMcNeill - 09-02-2022 Sorry. The version of QB64-PE that I've been testing stuff on doesn't have any DPI Awareness on it, so images don't scale to my 4k screen. An itsy-bitsy 16x8 font is much too small to read anything on, so I've got to scale them up manually like that. The good thing about the above is your error checking -- you only need a single line for IF v < 0 OR v > 255 THEN ERROR 5: EXIT SUB RE: Convert IPv4 dotted quad into four decimal values - SpriggsySpriggs - 09-15-2022 @bert22306 Hi! Here is some Win32 code you can use to convert a string IPv4 address to a 4 byte representation: Code: (Select All) Option Explicit RE: Convert IPv4 dotted quad into four decimal values - bert22306 - 10-31-2022 Thanks, Spriggsy, I did make this work after I posted that, in a somewhat different way. |