c#dll语言帮忙转换为c dll -凯发网址

gzcjx555
qq  296686949
级别: 网络英雄
精华主题: 0
发帖数量: 562 个
工控威望: 5154 点
下载积分: 84 分
在线时间: 442(小时)
注册时间: 2014-02-15
最后登录: 2025-10-02
查看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);
        }
附件: (1471 k) 下载次数:15
附件: (2 k) 下载次数:14
联系电话15071699246,qq296686949
zlcp123
级别: 工控侠客
精华主题: 0
发帖数量: 128 个
工控威望: 4321 点
下载积分: 13692 分
在线时间: 560(小时)
注册时间: 2018-09-11
最后登录: 2025-09-28
/
1楼  发表于: 2024-03-06 09:22
|
看上去就是十六进制的字符串与单双精浮点数的互相转换,既然你会c 应该可以按功能自己写一个

“42f6e979”<=>1.23456e 2

“405edd2f1a9fbe77”<=>1.23456e 2

就是这么个东西
[ 此帖被zlcp123在2024-03-06 09:28重新编辑 ]
gzcjx555
qq  296686949
级别: 网络英雄
精华主题: 0
发帖数量: 562 个
工控威望: 5154 点
下载积分: 84 分
在线时间: 442(小时)
注册时间: 2014-02-15
最后登录: 2025-10-02
查看gzcjx555的 主题 / 回贴
2楼  发表于: 2024-03-06 16:45
|
引用
引用第1楼zlcp123于2024-03-06 09:22发表的  :
看上去就是十六进制的字符串与单双精浮点数的互相转换,既然你会c 应该可以按功能自己写一个

“42f6e979”<=>1.23456e 2

“405edd2f1a9fbe77”<=>1.23456e 2
.......

我会c#,不会c ,这个好像有点区别
联系电话15071699246,qq296686949
已翻身的咸鱼
级别: 家园常客
精华主题: 0
发帖数量: 323 个
工控威望: 527 点
下载积分: 884 分
在线时间: 559(小时)
注册时间: 2021-03-04
最后登录: 2025-09-30
/
3楼  发表于: 2024-03-11 08:52
|
#include
#include

std::string float_to_string(float fl)
{
    std::vector a(sizeof(float));
    memcpy(&a[0], &fl, sizeof(float));

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

    return result;
}

std::string float_to_string(double dl)
{
    std::vector a(sizeof(double));
    memcpy(&a[0], &dl, sizeof(double));

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

    return result;
}

float string_to_float(std::string f1)
{
    std::vector 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 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)
  • gzcjx555
    qq  296686949
    级别: 网络英雄
    精华主题: 0
    发帖数量: 562 个
    工控威望: 5154 点
    下载积分: 84 分
    在线时间: 442(小时)
    注册时间: 2014-02-15
    最后登录: 2025-10-02
    查看gzcjx555的 主题 / 回贴
    4楼  发表于: 2024-03-11 09:46
    |
    引用
    引用第3楼已翻身的咸鱼于2024-03-11 08:52发表的  :
    #include
    #include

    std::string float_to_string(float fl)
    {
    .......

    感谢大神,我只会c#的语言,c 不会,epson手臂可以支持c 的dll文件,可以做复杂的逻辑,这个是比其他手臂要强大很多了
    联系电话15071699246,qq296686949

      网站地图