how can i convert the ip address into decimal
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
Joel Handy
2019-8-2
Do you mean how do I parse out the 4 fields into numeric data?
ipFields = str2double(split('192.192.192.2', '.'))
回答(1 个)
Joel Handy
2019-8-2
编辑:Joel Handy
2019-8-2
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 C Shared Library Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!