#include <cassert>
#include <sstream>
#include <iomanip>
#include "FormatTrans.h"


float HEX2DEC(WORD Hex)
{
    BYTE bai = Hex >> 12;
    Hex = Hex - bai * std::pow(16, 3);
    BYTE shi = Hex >> 8;
    Hex = Hex - shi * pow(16, 2);
    BYTE ge = Hex >> 4;
    Hex = Hex - ge * pow(16, 1);
    BYTE xiao = Hex;

    return bai * pow(10, 2) + shi * pow(10, 1) + ge * pow(10, 0) + xiao * pow(10, -1);
}


WORD DEC2HEX(float Dec)
{
    BYTE bai = Dec / pow(10, 2);
    Dec = Dec - bai * pow(10, 2);
    BYTE shi = Dec / pow(10, 1);
    Dec = Dec - shi * pow(10, 1);
    BYTE ge = Dec / pow(10, 0);
    Dec = Dec - ge * pow(10, 0);
    BYTE xiao = Dec / pow(10, -1);

    return bai * pow(16, 3) + shi * pow(16, 2) + ge * pow(16, 1) + xiao * pow(16, 0);
}

/***********************************
function: IsHexChar
desc:   判断字符是否在0-9，a-f之间
input： char-字符
Output：
return:  字符索引，如8-8，a-10，如果
         不是，返回-1
***********************************/
int IsHexChar(char hc)
{
    if('0'<=hc && hc<='9')
        return (int(hc)-int('0'));
    else if('a'<=hc && hc<='f')
        return (int(hc)-int('a')+10);
    else if('A'<=hc && hc<='F')
        return (int(hc)-int('A')+10);
    return -1;
}

/**************************************
function: Hex2Char
desc:     16进制数转化为字符
Input:    hex-16进制，字符串形式，2字符
Output:
return:    ascii字符
**************************************/
unsigned char Hex2Char(const std::string &hex)
{
    assert(hex.length() == 2);
    int high = IsHexChar(hex[0]);
    int low = IsHexChar(hex[1]);
    if(high == -1 || low == -1)
        return '\0';
    int asc = high*16+low;
    //	char b = toascii(asc);
    return asc;
}

/****************************
function: Hex2String
desc:     16进制字符串转化为ascii字符串
Input:    hex-16进制字符串
Output:
return:  ascii字符串
*****************************/
std::string Hex2String(const std::string &hex)
{
    assert(hex.length()%2 == 0);
    std::string hstr;
    for(int i=0; i<hex.length(); i+=2)
    {
        std::string tmp = hex.substr(i,2);
        hstr.append(1,Hex2Char(tmp));
    }
    return hstr;
}
/****************************************
function：PrepareHexString
desc:  去掉字符串里的空格，0x
Input:    str普通字符串
Output:
return:  去掉空格和0x的字符串
****************************************/
std::string PrepareHexString(std::string str)
{
    //先删除空格
    int i=0;
    while(i != str.length())
    {
        i = str.find(' ',i);
        if(i == -1)
            break;
        str = str.erase(i,1);
    }

    //删除0x
    i = 0;
    std::string tmp("0x");
    while(i != str.length())
    {
        i = str.find(tmp,i);
        if(i == -1)
            break;
        str = str.erase(i,2);
    }

    //删除0X
    i = 0;
    tmp = "0X";
    while(i != str.length())
    {
        i = str.find(tmp,i);
        if(i == -1)
            break;
        str = str.erase(i,2);
    }
    if(str.length()%2 != 0)
        str.append(1,'0');

    str = Hex2String(str);
    return str;
}

// 十进制转十六进制
std::string decimalToHexadecimal(int decimal) {
    std::stringstream ss;
    ss << std::hex << decimal;
    return ss.str();
}

// 十六进制转十进制
int hexadecimalToDecimal(const std::string& hexadecimal) {
    return std::stoi(hexadecimal, nullptr, 16);
}

std::string charToHex(char value) {
    int intValue = static_cast<int>(value);
    if (intValue < 0) {
        intValue = 256 + intValue;
    }
    std::stringstream ss;
    ss << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << intValue;
    return ss.str();
}


std::string verify(const std::string& hexStr){
    // 准备字符串，去掉空格和0x
    std::string preparedStr = PrepareHexString(hexStr);

    // 计算校验码
    int sum = 0;
    for (size_t i = 1; i < 6; i += 1) {
        int temp = int(static_cast<unsigned char>(preparedStr[i]));
        sum += temp;
    }

    // 取模并转换为16进制
    int checksum = sum % 0x100;
    std::string hex = decimalToHexadecimal(checksum);
    if (hex.length() == 1){
        hex = "0" + hex;
    }
    return hex;
}