how can i convert the ip address into decimal
13 views (last 30 days)
Show older comments
Hello,
for exemple i have this address and i want to convert it in decimal how can I do this with matlab
192.192.192.2
3 Comments
Joel Handy
on 2 Aug 2019
Do you mean how do I parse out the 4 fields into numeric data?
ipFields = str2double(split('192.192.192.2', '.'))
Answers (1)
Joel Handy
on 2 Aug 2019
Edited: Joel Handy
on 2 Aug 2019
ipFields = uint8(str2double(split('192.192.192.2', '.')))';
typecast(fliplr(ipFields), 'uint32')
% Explanation
dec2hex(192)
dec2hex(2)
Each field of an Ip address is an 8 bit (1 byte) integer, which can be represented by 2 digits in hexadecimal. If you squish the four 8 bit fields together, you get a single 32 bit integer which can be represented by 8 hexadecimal digits.
192.192.192.2 => [192 192 192 2] => [0C 0C 0C 02] => 0C0C0C02 => 3233857538
The typecast is where you "squish" the individual bytes together.
The field reordering is necessary due to the order of bits and bytes in memory. Different systems store bits in different orders. Some systems store data most significant bit, MSB, first while other store data least significant bit first, LSB. Some systems put the most significant byte before the least significant byte, which is called big endian, while others do the revers which is called little endian. You need to be aware of how your system stores data in order to correctly order the bytes before you squish them together.
See Also
Categories
Find more on C Shared Library Integration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!