C/C++中double與byte數(shù)組互轉(zhuǎn)的方法

這篇文章主要介紹“C/C++中double與byte數(shù)組互轉(zhuǎn)的方法”,在日常操作中,相信很多人在C/C++中double與byte數(shù)組互轉(zhuǎn)的方法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”C/C++中double與byte數(shù)組互轉(zhuǎn)的方法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

創(chuàng)新互聯(lián)公司專(zhuān)注于額濟(jì)納企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開(kāi)發(fā),商城網(wǎng)站定制開(kāi)發(fā)。額濟(jì)納網(wǎng)站建設(shè)公司,為額濟(jì)納等地區(qū)提供建站服務(wù)。全流程定制設(shè)計(jì),專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)

本文包含了C/C++中byte(unsigned char)類(lèi)型與double數(shù)組互轉(zhuǎn)的方法,參見(jiàn)本文的最后兩個(gè)方法,親測(cè)可行。

#include <vector>
#include <iostream>
#include <string.h>
typedef unsigned char byte;

typedef struct {
    int precession;
    double value;
}ConstantValue;

byte int2Byte(int intVal);
void double2bytes(double data, byte* bytes);
double bytes2double(byte bytes[]);

std::vector<byte> convert2Byte(ConstantValue cv);
using namespace std;

/**
double convert to bytes
refers to struct body attribute
*/

int main(){

    ConstantValue cv1, cv2;
    cv1.precession = 10;
    cv1.value = 36.32598;

    cv2.precession = 15;
    cv2.value = 271.15801;

    vector<byte> cv1Byte = convert2Byte(cv1);
    vector<byte> cv2Byte = convert2Byte(cv2); 

    for(int i = 0; i < cv1Byte.size(); i++){

        cout<<"------"<<(int)cv1Byte[i]<<endl;
    }
//    byte dByte[8] =  {58,16,64,0,0,0,0,0};
//    cout<<"----double--"<<bytes2Double(dByte)<<endl;
    byte b2[8] = {0x6E, 0x86, 0x1B, 0xF0, 0xF9, 0x21, 0x09, 0x40};
    double fValue = 100.111;
    byte bytes[8]; 

   double2bytes(fValue, bytes);
/*    
    for(int i = 0; i < 8; i++){

        cout<<"------"<<(int)bytes[i]<<endl;
    }
      byte b3[8] = {201, 118, 190, 159, 26, 7, 89, 64};
*/    
    byte arr[8];
    
    copy(cv1Byte.begin()+3, cv1Byte.end(), arr);
    cout<<"-----qqqqq-"<<bytes2double(arr)<<endl;    

    return 0;
}


    vector<byte> convert2Byte(ConstantValue cv){

    vector<byte> vecData;

    vecData.push_back('I');//data type    

    vecData.push_back('N');//data type

    vecData.push_back(int2Byte(cv.precession));//data type

//    byte *valByte = double2bytes(cv.value);

    byte valByte[8];
    double2bytes(cv.value, valByte);    

    for(int i = 0; i < 8; i++){        
        vecData.push_back(valByte[i]);//data type
    }

    return vecData;

}

/**
 *  c++ double type length is 8 byte 
 */
void double2bytes(double data, byte bytes[]){
    int i;
    char* p = (char*)&data; 
    for(i=0; i<8; i++)
    {
        bytes[i] = *p++;  
    }
}

/**
 * convert byte array to double type
 */
double bytes2double(byte bytes[]){
    double data = *((double *)bytes);  
    return data;
}

到此,關(guān)于“C/C++中double與byte數(shù)組互轉(zhuǎn)的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

網(wǎng)站標(biāo)題:C/C++中double與byte數(shù)組互轉(zhuǎn)的方法
標(biāo)題URL:http://muchs.cn/article0/iehiio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、定制網(wǎng)站、App開(kāi)發(fā)虛擬主機(jī)、外貿(mào)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司