zoukankan      html  css  js  c++  java
  • byte array convert to its corresponding numeric value

    nanomsg-1.1.5/src/utils/wire.c

    /*
        Copyright (c) 2012-2013 Martin Sustrik  All rights reserved.
    
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"),
        to deal in the Software without restriction, including without limitation
        the rights to use, copy, modify, merge, publish, distribute, sublicense,
        and/or sell copies of the Software, and to permit persons to whom
        the Software is furnished to do so, subject to the following conditions:
    
        The above copyright notice and this permission notice shall be included
        in all copies or substantial portions of the Software.
    
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
        THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
        FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
        IN THE SOFTWARE.
    */
    
    #include "wire.h"
    
    #if defined NN_HAVE_WINDOWS
    #include "win.h"
    #else
    #include <arpa/inet.h>
    #endif
    
    uint16_t nn_gets (const uint8_t *buf)
    {
        return (((uint16_t) buf [0]) << 8) |
               ((uint16_t) buf [1]);
    }
    
    void nn_puts (uint8_t *buf, uint16_t val)
    {
        buf [0] = (uint8_t) (((val) >> 8) & 0xff);
        buf [1] = (uint8_t) (val & 0xff);
    }
    
    uint32_t nn_getl (const uint8_t *buf)
    {
        return (((uint32_t) buf [0]) << 24) |
               (((uint32_t) buf [1]) << 16) |
               (((uint32_t) buf [2]) << 8) |
               ((uint32_t) buf [3]);
    }
    
    void nn_putl (uint8_t *buf, uint32_t val)
    {
        buf [0] = (uint8_t) (((val) >> 24) & 0xff);
        buf [1] = (uint8_t) (((val) >> 16) & 0xff);
        buf [2] = (uint8_t) (((val) >> 8) & 0xff);
        buf [3] = (uint8_t) (val & 0xff);
    }
    
    uint64_t nn_getll (const uint8_t *buf)
    {
        return (((uint64_t) buf [0]) << 56) |
               (((uint64_t) buf [1]) << 48) |
               (((uint64_t) buf [2]) << 40) |
               (((uint64_t) buf [3]) << 32) |
               (((uint64_t) buf [4]) << 24) |
               (((uint64_t) buf [5]) << 16) |
               (((uint64_t) buf [6]) << 8) |
               (((uint64_t) buf [7] << 0));
    }
    
    void nn_putll (uint8_t *buf, uint64_t val)
    {
        buf [0] = (uint8_t) ((val >> 56) & 0xff);
        buf [1] = (uint8_t) ((val >> 48) & 0xff);
        buf [2] = (uint8_t) ((val >> 40) & 0xff);
        buf [3] = (uint8_t) ((val >> 32) & 0xff);
        buf [4] = (uint8_t) ((val >> 24) & 0xff);
        buf [5] = (uint8_t) ((val >> 16) & 0xff);
        buf [6] = (uint8_t) ((val >> 8) & 0xff);
        buf [7] = (uint8_t) (val & 0xff);
    }

    nanomsg-1.1.5/src/utils/wire.h

    /*
        Copyright (c) 2012-2013 Martin Sustrik  All rights reserved.
    
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"),
        to deal in the Software without restriction, including without limitation
        the rights to use, copy, modify, merge, publish, distribute, sublicense,
        and/or sell copies of the Software, and to permit persons to whom
        the Software is furnished to do so, subject to the following conditions:
    
        The above copyright notice and this permission notice shall be included
        in all copies or substantial portions of the Software.
    
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
        THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
        FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
        IN THE SOFTWARE.
    */
    
    #ifndef NN_WIRE_INCLUDED
    #define NN_WIRE_INCLUDED
    
    #include <stdint.h>
    
    uint16_t nn_gets (const uint8_t *buf);
    void nn_puts (uint8_t *buf, uint16_t val);
    uint32_t nn_getl (const uint8_t *buf);
    void nn_putl (uint8_t *buf, uint32_t val);
    uint64_t nn_getll (const uint8_t *buf);
    void nn_putll (uint8_t *buf, uint64_t val);
    
    #endif
  • 相关阅读:
    【ES6】数组的扩展——扩展运算符
    【ES6】函数的扩展
    菜鸡程序员是如何写代码的?
    我是技术总监,我出来求职,竟然找不到工作!
    为什么互联网公司天天都在招人?
    这个立冬,我线下面基了一位TMD高级专家,太牛逼了!
    太可怕了!有些码农为啥写代码,写到监狱里去了?
    56岁潘石屹生日当天宣布要学编程语言Python,网友:地产商来抢码农饭碗了!
    如何写出让同事无法维护的代码?
    hdu 1037 Keep on Truckin'
  • 原文地址:https://www.cnblogs.com/dong1/p/14040135.html
Copyright © 2011-2022 走看看