Page 1 of 1

Question about %HI format specifier in printf, etc.

Posted: Sat Jan 09, 2021 9:52 pm
by jaypdx
I see both %I and %HI format specifiers in the Netburner sample code (i.e. examples\Ethernet\ManualConfig\src\main.cpp). When I use %HI I get a warning "use of 'H' length modifier with 'I' type character has either no effect or undefined behavior". But if I just use %I it says that's for IPADDR6 (I'm printing an IPADDR4). This is on NNDK 3.3.0. Are these specifiers documented someplace? Should I just ignore the warning?

Re: Question about %HI format specifier in printf, etc.

Posted: Sun Jan 10, 2021 11:29 am
by pbreed
%hI for an IPADDR4 STORED in an IPADDR4.
%I for an IPADDR4 stored in an IPADDR (IPADDR6)

So what is the actual TYPE of the variable you are using in the printf?

The code ties to make the distinction between IPV6 and IPV4 disappear...
IE You should not care....

You should use IPADDR and even if you are only ever putting IPV4 addresses in it...
You should only ever use an IPADD4 if you have a legacy structure or flash storage that saves it as a 32bit DWORD.


IPADDR ia=AsciiToIp("10.1.2.3");

ia is an IPADDR (aka IPADDR6) it is holding an IPV4 address...

If you use it in a network call udp, tcp connect etc....
the packets that are generated on the wire will be IPv4 packets.

if you want to printf it use %I as its an IPADDR, its type is not an IPADD4 type , even though its current contents hold an IPV4 address.
When it prints out using %I it will print out in IPv4 format because it is an IPV4 address...

Re: Question about %HI format specifier in printf, etc.

Posted: Sun Jan 10, 2021 8:28 pm
by jaypdx
My code is:

IPADDR4 EthernetIP;
EthernetIP = InterfaceIP(GetFirstInterface());
iprintf("IP address: %HI\n", EthernetIP);

InterfaceIP(), as defined in netinterface.h, returns an IPADDR4. If I define EthernetIP as an IPADDR I get an error. All I'm trying to here is get the IP address of my ethernet interface and print it to the console or to a string (via sprintf). Should I be doing this another way?

I should ignore the warning, right?:

../src/main.cpp: In function 'void UserMain(void*)':
../src/main.cpp:165:13: warning: use of 'H' length modifier with 'I' type character has either no effect or undefined behavior [-Wformat=]
iprintf("IP address: %HI\n", EthernetIP);
^~~~~~~~~~~~~~~~~~~

Re: Question about %HI format specifier in printf, etc.

Posted: Mon Jan 11, 2021 8:51 am
by TomNB
The show interface example goes through all this in detail: \nburn\examples\ShowInterfaces\. It also has utility files you can copy to your design if you wish.