浮點數網路傳輸,浮點數傳輸

來源:互聯網
上載者:User

浮點數網路傳輸,浮點數傳輸

作業系統 : CentOS7.3.1611_x64

gcc版本 :4.8.5

Python 版本 : 2.7.5

思路如下 :

1、將浮點數a通過記憶體拷貝,賦值給相同位元組的整型資料b;

2、將b轉換為網路位元組序變數c並發送到服務端;

3、服務端接收c並將c轉換為主機位元組序變數d;

4、將整型資料d通過記憶體拷貝,賦值給相同位元組的浮點數據e;

至此,浮點數網路傳輸完成。

C範例程式碼:

#define htonl64 htobe64#define ntohl64 be64tohuint64_t htonf64(double dvalue){    uint64_t ulltmp = 0;    memcpy(&ulltmp,&dvalue,8);    ulltmp = htonl64(ulltmp);    return ulltmp;}double ntohf64(uint64_t ulvalue){    uint64_t ulltmp = 0;    double ret = 0.0;    ulltmp = ntohl64(ulvalue);    memcpy(&ret,&ulltmp,8);    return ret;}

完整範例程式碼如下:

https://github.com/mike-zhang/cppExamples/blob/master/socketOpt/byteOrder/double_test1.c

python範例程式碼 :

def htonfl(f):    s = struct.pack('d',f)    return struct.unpack('!Q',s)[0]def fltonl(v):    s = struct.pack('!Q',v)    return struct.unpack('d',s)[0]

完整範例程式碼如下:

https://github.com/mike-zhang/pyExamples/blob/master/socketRelate/byteOrder_double1.py

好,就這些了,希望對你有協助。

本文github地址:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2018/20180320_浮點數網路傳輸.rst

歡迎補充

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.