1, hexadecimal hex Convert to integer

thinking : This is relatively simple , Just shift it , Integers are binary in nature

//int is 4 char,need the input is array with four hex; 
int char4_int(unsigned char* pData)
{
    int t = 0;
    for (int i = 0; i < 4; i++){
        t = t << 8 | pData[i];
    }
    return t;
}

//long int is 8 char,need the input is array with eight hex; 
long int char8_int(unsigned char* pData)
{
    long int t = 0;
    for (int i = 0; i < 8; i++){
        t = t << 8 | pData[i];
    }
    return t;
}

 

2,8 individual hex Convert to a double

thinking : Convert to a 64 Length array , Then the conversion is carried out according to the floating-point conversion algorithm . Calculate the starting number of the index first , And then add up . Or will it hex Array to a 64 Integer of bits , Then the floating-point algorithm is used to calculate the floating-point value !

double char8_double(unsigned char* pData)
{
    char bin[64];
    for(int i = 0; i < 8; i++){
        unsigned char GetBit = 0x80;
        for (int j = 0; j < 8; j++){
            if (GetBit & pData[i]){
                bin[i * 8 + j] = 1;
            }
            else
                bin[i * 8 + j] = 0;
            GetBit = GetBit >> 1;
        }
    }
    return BinaryToDouble2(bin);
}
double BinaryToDouble2(char *bina){
    int i, j, expo = 0;
    double sum = 0, sum2 = 0;
    for (i = 11; i > 0; i--){
        expo += (bina[i] * power(2, 11 - i));
    }
    expo -= 1023;// index = Order code subtraction 1023!

    bina[11] = 1;
    j = expo + 1;//
    for (i = 11; i < 64; i++){
        sum2 += (bina[i] * power(2, --j));
    }
    /*
    for (i = 11 + expo; i < 64; i++){
        sum += bina[i] * (1.0 / power(2, i - 10 - expo));
    }*/

    if (bina[0] == 1){
        return (-(sum2 + sum + 0.0000001));
    }
    else{
        return (sum2 + sum + 0.0000001);
    }
}

//function: base^index
double power(int base, int index){
    double value = 1.0;
    int count;
    if (index == 0){
        value = 1; 
    }
    else if (index<0){
        index = -index;
        for (count = 1; count <= index; count++){
            value *= base;
        }
        value = 1.0 / value;
    }
    else if (index>0){
        for (count = 1; count <= index; count++){
            value *= base;
        }
    }
    return value;
}

Technology
©2019-2020 Toolsou All rights reserved,
Python Garbage collection and memory leak hive Summary of processing methods for a large number of small files The difference between memory overflow and memory leak , Causes and Solutions Create data mysql Library process You don't know ——HarmonyOS stay Vue Use in Web WorkerSparkSQL Achieve partition overlay write msf Generate Trojan horse attack android mobile phone Linux Page replacement algorithm C Language implementation Django Personal blog building tutorial --- Time classified archiving