小编Vla*_*lam的帖子

如何从二进制文件中读取单精度浮点数并转换为Erlang浮点数?

我使用Erlang在二进制文件中读取了4个字节(小端).

在尝试将二进制转换为浮点数时,我遇到了以下错误:

** exception error: bad argument
     in function  list_to_integer/1
        called as list_to_integer([188,159,21,66])
Run Code Online (Sandbox Code Playgroud)

它似乎不匹配浮动模式,最终调用list_to_integer.如何将我的单精度浮点数转换为原生的Erlang浮点数?

我的Erlang功能如下:

readfloat(S, StartPos) ->
    io:format("Pos: ~w~n", [StartPos - 1]),
    case file:pread(S, StartPos - 1, 4) of
    eof ->
        ok;
    {ok, Data} ->
        % io:format("Wut: ~w~n", Data),
        N = binary_to_list(Data),
        case string:to_float(N) of
            {error,no_float} ->
                list_to_integer(N);
            {F,_Rest} ->
                F
        end

        % have tried this section as well, error too
        % N = binary_to_list(Data),
        % try list_to_float(N)
        % catch
            % error:badarg ->
                % list_to_integer(N)
        % end
    end.
Run Code Online (Sandbox Code Playgroud)

floating-point erlang

2
推荐指数
2
解决办法
653
查看次数

D与C库集成会导致输出乱码

我有一个C库,如下所示:

Abc.h文件

typedef struct
{
    uint8_t dbtype;
    uint32_t dbcount;
} Abc;

void test_string(Abc* abcobj, char* strA, char* strB);
Abc* create_abc(void);
Run Code Online (Sandbox Code Playgroud)

Abc.c

void test_string(Abc* abcobj, char* strA, char* strB)
{
    printf("Str A is %s and str B is %s\n", strA, strB);
    return;
}

Abc* create_abc()
{
    Abc* abcobj;
    abcobj = (Abc*) calloc(1, sizeof(Abc));
    return abcobj;
}
Run Code Online (Sandbox Code Playgroud)

现在,我试图在我的D代码中调用这些函数.

testD.d

module testD;

import std.stdio;
import core.stdc.string;
import core.sys.posix.dlfcn;

extern (C)
{
    struct Abc {
        ubyte dbtype;
        int dbcount;
    }
}

int main() { …
Run Code Online (Sandbox Code Playgroud)

c d

1
推荐指数
1
解决办法
96
查看次数

该表达式的类型为字符串 t,但表达式应为字符串类型

我是 OCaml 新手,所以这对你们中的一些人来说可能是显而易见的,但希望你们能对我有耐心。

在我的代码顶部,我有这些:

open Lwt
open Cohttp
open Cohttp_lwt_unix
Run Code Online (Sandbox Code Playgroud)

然后我有以下代码:

let call_api config ip add_on lang =
  let protocol = if config.use_ssl then "https" else "http" in
  let uri = Uri.of_string (protocol ^ "://api.example.com/v2/?key=" ^ config.api_key ^ "&ip=" ^ ip ^ "&package=" ^ config.api_package ^ "&addon=" ^ add_on ^ "&lang=" ^ lang) in
  
  Lwt_main.run begin
    Client.get uri >>= fun (resp, body) ->
    let code = resp |> Response.status |> Code.code_of_status in
    let json = body |> Cohttp_lwt.Body.to_string in
    printf …
Run Code Online (Sandbox Code Playgroud)

ocaml

1
推荐指数
1
解决办法
274
查看次数

标签 统计

c ×1

d ×1

erlang ×1

floating-point ×1

ocaml ×1