This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NTON function not available in SDK17

Hi,

I am trying to convert a number from little endian to big endian.  There is a built in function "NTON" that I can use instead of writing my own code, but I can not find it in nrf52840 SDK17.

I am using Keil as IDE.   The example code that I am using is from STM32F4 board.  Could anybody show me how to use NTON or NTONL function in Nordic SDK17.

Thanks

Ray

Parents
  • Have a look at endian.h; to convert from little-endian (silly order) to big endian (sensible order) that header provides the conversion functions:

    #ifndef __machine_host_to_from_network_defined
    #if _BYTE_ORDER == _LITTLE_ENDIAN
    #define	__htonl(_x)	__bswap32(_x)
    #define	__htons(_x)	__bswap16(_x)
    #define	__ntohl(_x)	__bswap32(_x)
    #define	__ntohs(_x)	__bswap16(_x)
    #else
    #define	__htonl(_x)	((__uint32_t)(_x))
    #define	__htons(_x)	((__uint16_t)(_x))
    #define	__ntohl(_x)	((__uint32_t)(_x))
    #define	__ntohs(_x)	((__uint16_t)(_x))
    #endif
    #endif /* __machine_host_to_from_network_defined */

    endian.h is here (example, depends on compiler used in SES or Keil):

    C:\program files (x86)\gnu tools arm embedded\9 2019-q4-major\arm-none-eabi\include\machine\endian.h

    static __inline __uint16_t __bswap16(__uint16_t _x)
    {
    	return ((__uint16_t)((_x >> 8) | ((_x << 8) & 0xff00)));
    }
    
    static __inline __uint32_t __bswap32(__uint32_t _x)
    {
    	return ((__uint32_t)((_x >> 24) | ((_x >> 8) & 0xff00) |
    	    ((_x << 8) & 0xff0000) | ((_x << 24) & 0xff000000)));
    }
    
    static __inline __uint64_t __bswap64(__uint64_t _x)
    {
    	return ((__uint64_t)((_x >> 56) | ((_x >> 40) & 0xff00) |
    	    ((_x >> 24) & 0xff0000) | ((_x >> 8) & 0xff000000) |
    	    ((_x << 8) & ((__uint64_t)0xff << 32)) |
    	    ((_x << 24) & ((__uint64_t)0xff << 40)) |
    	    ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56))));
    }

Reply
  • Have a look at endian.h; to convert from little-endian (silly order) to big endian (sensible order) that header provides the conversion functions:

    #ifndef __machine_host_to_from_network_defined
    #if _BYTE_ORDER == _LITTLE_ENDIAN
    #define	__htonl(_x)	__bswap32(_x)
    #define	__htons(_x)	__bswap16(_x)
    #define	__ntohl(_x)	__bswap32(_x)
    #define	__ntohs(_x)	__bswap16(_x)
    #else
    #define	__htonl(_x)	((__uint32_t)(_x))
    #define	__htons(_x)	((__uint16_t)(_x))
    #define	__ntohl(_x)	((__uint32_t)(_x))
    #define	__ntohs(_x)	((__uint16_t)(_x))
    #endif
    #endif /* __machine_host_to_from_network_defined */

    endian.h is here (example, depends on compiler used in SES or Keil):

    C:\program files (x86)\gnu tools arm embedded\9 2019-q4-major\arm-none-eabi\include\machine\endian.h

    static __inline __uint16_t __bswap16(__uint16_t _x)
    {
    	return ((__uint16_t)((_x >> 8) | ((_x << 8) & 0xff00)));
    }
    
    static __inline __uint32_t __bswap32(__uint32_t _x)
    {
    	return ((__uint32_t)((_x >> 24) | ((_x >> 8) & 0xff00) |
    	    ((_x << 8) & 0xff0000) | ((_x << 24) & 0xff000000)));
    }
    
    static __inline __uint64_t __bswap64(__uint64_t _x)
    {
    	return ((__uint64_t)((_x >> 56) | ((_x >> 40) & 0xff00) |
    	    ((_x >> 24) & 0xff0000) | ((_x >> 8) & 0xff000000) |
    	    ((_x << 8) & ((__uint64_t)0xff << 32)) |
    	    ((_x << 24) & ((__uint64_t)0xff << 40)) |
    	    ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56))));
    }

Children
No Data
Related