how can i convert the ip address into decimal

12 Ansichten (letzte 30 Tage)
mez kari
mez kari am 2 Aug. 2019
Kommentiert: dpb am 5 Aug. 2019
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 Kommentare
Joel Handy
Joel Handy am 2 Aug. 2019
Do you mean how do I parse out the 4 fields into numeric data?
ipFields = str2double(split('192.192.192.2', '.'))
mez kari
mez kari am 2 Aug. 2019
Bearbeitet: dpb am 2 Aug. 2019
i will explaine to you i have sensor connected into my pc via ethernet : i have in my project function wich return for me this number (3233857538) and when i ping this number it return me the ip i write on top so i think it's a number we get when we convert the ip i don't know.
if i put dec2hex(3233857538) it return exactly the same when i convert the ip in hex wich is C0C0C002
see this exemple in c++ :
void SetDeviceInterface(unsigned int nInterface, int nAdditional)
Setting of the number of an interface and transfer of an additional parameter. The additional parameter is used in order to indicate the baud rate of the serial interface. Therefore, the value has to be 115 200. In case of Ethernet interface, nInterface corresponds to the IP address in the so-called host byte order, wherein the 4 bits of the IP address of the sequence are still saved from the highest to the lowest byte. This can be easily achieved by means of a shift operation of the individual bytes of the IP address, as seen in the example for the address
192.168.1.2:
(192<<24) | (168<<16) | (1<<8) | 2 = 3232235778 = 0xC0A80102
but if you know to how to use this in matlab (192<<24......) explain to me or show me an example
thanks for response

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Joel Handy
Joel Handy am 2 Aug. 2019
Bearbeitet: Joel Handy am 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.
  4 Kommentare
mez kari
mez kari am 5 Aug. 2019
What i want to know is how to write (0<<25) in matlab
dpb
dpb am 5 Aug. 2019
"how to write (0<<25) in matlab"
0 is still 0
>> bitshift(0,25)
ans =
0
>>

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu C Shared Library Integration finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by