Network byte sequence Conversion Function (64-bit)

Source: Internet
Author: User

In network programming, the network byte sequence is big-Endian, while most PC systems are x86 processor series, x86 uses little-Endian, so you need

Network data streams must be converted to local data streams in byte order.

 

The standard library provides two functions, hlton () and nthl (), to support conversion.

Hston (unsigned short) and hlton (unsigned long) convert local bytes to network bytes

Ntohl (unsigned long) and ntohs (unsigned short) convert the network byte order to the local byte order

 

However, for 64-bit integers, the standard library does not provide the corresponding conversion function. This article will provide an original 64-bit bytecode conversion function.

 

# Ifndef ulong64

# Define unsigned long ulong64

# Endif

 

// Host long 64 to Network

 

Ulong64 hl64ton (ulong64 host)

{

Ulong64 ret = 0;

Ulong high, low;

 

Low = Host & 0 xffffffff;

High = (host> 32) & 0 xffffffff;

Low = htonl (low );

High = htonl (high );

Ret = low;

RET <= 32;

RET | = high;

Return ret;

}

 

 

 

// Network to host long 64

 

Ulong64 ntohl64 (ulong64 host)

{

Ulong64 ret = 0;

 

Ulong high, low;

 

Low = Host & 0 xffffffff;

High = (host> 32) & 0 xffffffff;

Low = ntohl (low );

High = ntohl (high );

 

 

Ret = low;

RET <= 32;

RET | = high;

Return ret;

}

 

 

 

For more information about little endian and big endian, I will not go into detail in this article.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.