how can i convert the ip address into decimal
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Joel Handy
el 2 de Ag. de 2019
Do you mean how do I parse out the 4 fields into numeric data?
ipFields = str2double(split('192.192.192.2', '.'))
Respuestas (1)
Joel Handy
el 2 de Ag. de 2019
Editada: Joel Handy
el 2 de Ag. de 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.
4 comentarios
dpb
el 5 de Ag. de 2019
"how to write (0<<25) in matlab"
0 is still 0
>> bitshift(0,25)
ans =
0
>>
Ver también
Categorías
Más información sobre C Shared Library Integration en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!