gzcjx555
QQ  296686949
级别: 工控侠客
精华主题: 0
发帖数量: 477 个
工控威望: 4163 点
下载积分: 212 分
在线时间: 332(小时)
注册时间: 2014-02-15
最后登录: 2024-11-17
查看gzcjx555的 主题 / 回贴
楼主  发表于: 2024-03-05 16:35
请广大网友帮忙把编程语言给转换一下
        public string float_TO_string(float fl)
        {
            byte[] a = BitConverter.GetBytes(fl);
            return a[0].ToString("X2") + a[1].ToString("X2") + a[2].ToString("X2") + a[3].ToString("X2");
        }
        public string float_TO_string(double dl)
        {
            byte[] a = BitConverter.GetBytes(dl);
            return a[0].ToString("X2") + a[1].ToString("X2") + a[2].ToString("X2") + a[3].ToString("X2")+ a[4].ToString("X2") + a[5].ToString("X2") + a[6].ToString("X2") + a[7].ToString("X2");
        }


        public float string_TO_float(string f1)
        {
            byte[] a = new byte[4];
            a[0] = Convert.ToByte(f1.Substring(0, 2));
            a[1] = Convert.ToByte(f1.Substring(2, 2));
            a[2] = Convert.ToByte(f1.Substring(4, 2));
            a[3] = Convert.ToByte(f1.Substring(6, 2));
            return BitConverter.ToSingle(a, 0);
        }
        public double string_TO_double(string d1)
        {
            byte[] a = new byte[8];
            a[0] = Convert.ToByte(d1.Substring(0, 2));
            a[1] = Convert.ToByte(d1.Substring(2, 2));
            a[2] = Convert.ToByte(d1.Substring(4, 2));
            a[3] = Convert.ToByte(d1.Substring(6, 2));
            a[4] = Convert.ToByte(d1.Substring(8, 2));
            a[5] = Convert.ToByte(d1.Substring(10, 2));
            a[6] = Convert.ToByte(d1.Substring(12, 2));
            a[7] = Convert.ToByte(d1.Substring(14, 2));
            return BitConverter.ToDouble(a, 0);
        }
附件: epson_dll.pdf (1471 K) 下载次数:14
网站提示: 请不要用迅雷下载附件,容易出错
附件: EPSON float与string互转.txt (2 K) 下载次数:13
网站提示: 请不要用迅雷下载附件,容易出错
联系电话15071699246,QQ296686949
已翻身的咸鱼
级别: 略有小成
精华主题: 0
发帖数量: 258 个
工控威望: 443 点
下载积分: 707 分
在线时间: 410(小时)
注册时间: 2021-03-04
最后登录: 2024-11-16
查看已翻身的咸鱼的 主题 / 回贴
1楼  发表于: 2024-03-11 08:52
#include <iostream>
#include <vector>

std::string float_TO_string(float fl)
{
    std::vector<unsigned char> a(sizeof(float));
    memcpy(&a[0], &fl, sizeof(float));

    std::string result;
    for (unsigned char byte : a) {
        char hex[3];
        snprintf(hex, sizeof(hex), "%02X", byte);
        result += hex;
    }

    return result;
}

std::string float_TO_string(double dl)
{
    std::vector<unsigned char> a(sizeof(double));
    memcpy(&a[0], &dl, sizeof(double));

    std::string result;
    for (unsigned char byte : a) {
        char hex[3];
        snprintf(hex, sizeof(hex), "%02X", byte);
        result += hex;
    }

    return result;
}

float string_TO_float(std::string f1)
{
    std::vector<unsigned char> a(4);
    for (int i = 0; i < 4; ++i) {
        a = std::stoul(f1.substr(i * 2, 2), nullptr, 16);
    }

    float fl;
    memcpy(&fl, &a[0], sizeof(float));

    return fl;
}

double string_TO_double(std::string d1)
{
    std::vector<unsigned char> a(8);
    for (int i = 0; i < 8; ++i) {
        a = std::stoul(d1.substr(i * 2, 2), nullptr, 16);
    }

    double dl;
    memcpy(&dl, &a[0], sizeof(double));

    return dl;
}

int main()
{
    // 测试转换函数
    float f = 3.14f;
    double d = 6.28;

    std::string floatHex = float_TO_string(f);
    std::string doubleHex = float_TO_string(d);

    std::cout << "Float to Hex String: " << floatHex << std::endl;
    std::cout << "Double to Hex String: " << doubleHex << std::endl;

    float convertedFloat = string_TO_float(floatHex);
    double convertedDouble = string_TO_double(doubleHex);

    std::cout << "Hex String to Float: " << convertedFloat << std::endl;
    std::cout << "Hex String to Double: " << convertedDouble << std::endl;

    return 0;
}
本帖最近评分记录:
  • 下载积分:+5(gzcjx555)