十六进制转 64 有符号十进制

发布于 2024-07-23 05:44:17 字数 760 浏览 8 评论 0原文

我有十六进制数据,我必须转换为 64 有符号十进制数据..所以我认为有这样的步骤。 1.十六进制转二进制, 我没有编写自己的代码转换,而是使用此链接中给出的代码 http://necrobious.blogspot.com/2008/03/binary-to-hex-string-back-to-binary-in.html

bin_to_hexstr(Bin) ->
  lists:flatten([io_lib:format("~2.16.0B", [X]) ||
    X <- binary_to_list(Bin)]).

hexstr_to_bin(S) ->
  hexstr_to_bin(S, []).
hexstr_to_bin([], Acc) ->
  list_to_binary(lists:reverse(Acc));
hexstr_to_bin([X,Y|T], Acc) ->
  {ok, [V], []} = io_lib:fread("~16u", [X,Y]),
  hexstr_to_bin(T, [V | Acc]).

2.二进制转十进制, 如何实现这部分。?

或任何其他方式来实现十六进制 -> 的有符号十进制数据

64提前感谢

i have hexdecimal data i have to convert into 64 Signed Decimal data ..so i thought have follwoing step like this.
1.hexadecimal to binary,
instead of writing my own code conversion i m using code given in this link http://necrobious.blogspot.com/2008/03/binary-to-hex-string-back-to-binary-in.html

bin_to_hexstr(Bin) ->
  lists:flatten([io_lib:format("~2.16.0B", [X]) ||
    X <- binary_to_list(Bin)]).

hexstr_to_bin(S) ->
  hexstr_to_bin(S, []).
hexstr_to_bin([], Acc) ->
  list_to_binary(lists:reverse(Acc));
hexstr_to_bin([X,Y|T], Acc) ->
  {ok, [V], []} = io_lib:fread("~16u", [X,Y]),
  hexstr_to_bin(T, [V | Acc]).

2.binary to decimal,
how to achieve this part.?

or any other way to achieve the hexdecimal -> 64 Signed Decimal data

thanx in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜未央樱花落 2024-07-30 05:44:17

要将整数转换为十六进制字符串,只需使用 erlang:integer_to_list(Int, 16)。 要转换回来,请使用 erlang:list_to_integer(List, 16)。 这些函数我相信取 2 到 36 之间的基数。

如果要将二进制文件与十六进制字符串相互转换,您可以使用列表推导式使其更加整洁:

bin_to_hex(Bin) -> [ hd(erlang:integer_to_list(I, 16)) || << I:4 >> <= Bin ].
hex_to_bin(Str) -> << << (erlang:list_to_integer([H], 16)):4 >> || H <- Str >>.

要将整数转换为包含 64 位有符号整数的十六进制字符串,您现在可以执行以下操作:

Int = 1 bsl 48, HexStr = bin_to_hex(<<Int:64/signed-integer>>),
Bin = hex_to_bin(HexStr), <<RoundTrippedInt:64/signed-integer>> = Bin,
Int =:= RoundTrippedInt.

To convert an integer to a hex string, just use erlang:integer_to_list(Int, 16). To convert back, use erlang:list_to_integer(List, 16). These functions take a radix from 2 to 36 I believe.

If you want to convert binaries to and from hex strings you can use list comprehensions to make it tidier:

bin_to_hex(Bin) -> [ hd(erlang:integer_to_list(I, 16)) || << I:4 >> <= Bin ].
hex_to_bin(Str) -> << << (erlang:list_to_integer([H], 16)):4 >> || H <- Str >>.

To convert an integer to a hex string containing a 64 bit signed integer, you can now do:

Int = 1 bsl 48, HexStr = bin_to_hex(<<Int:64/signed-integer>>),
Bin = hex_to_bin(HexStr), <<RoundTrippedInt:64/signed-integer>> = Bin,
Int =:= RoundTrippedInt.
眼眸 2024-07-30 05:44:17

这种方法怎么样?

hex2int(L) ->
   << I:64/signed-integer >> = hex_to_bin(L),
   I.

int2hex(I) -> [ i2h(X) || <<X:4>> <= <<I:64/signed-integer>> ].

hex_to_bin(L) -> << <<(h2i(X)):4>> || X<-L >>.

h2i(X) ->
    case X band 64 of
        64 -> X band 7 + 9;
        _  -> X band 15
    end.

i2h(X) when X > 9 -> $a + X - 10;
i2h(X) -> $0 + X.

What about this approach?

hex2int(L) ->
   << I:64/signed-integer >> = hex_to_bin(L),
   I.

int2hex(I) -> [ i2h(X) || <<X:4>> <= <<I:64/signed-integer>> ].

hex_to_bin(L) -> << <<(h2i(X)):4>> || X<-L >>.

h2i(X) ->
    case X band 64 of
        64 -> X band 7 + 9;
        _  -> X band 15
    end.

i2h(X) when X > 9 -> $a + X - 10;
i2h(X) -> $0 + X.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文