我使用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) 我有一个C库,如下所示:
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)
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代码中调用这些函数.
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) 我是 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)