erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123 行
5.0 KiB

  1. -module(utHex).
  2. -compile([export_all, nowarn_export_all]).
  3. -define(Hex(X), (getHex(X)):16).
  4. -spec binaryToHex(Bin :: binary()) -> string().
  5. binaryToHex(Bin) ->
  6. HexBin = <<<<?Hex(X)>> || <<X:8>> <= Bin>>,
  7. erlang:binary_to_list(HexBin).
  8. -spec binaryToHexBin(Bin :: binary()) -> binary().
  9. binaryToHexBin(Bin) ->
  10. <<<<?Hex(X)>> || <<X:8>> <= Bin>>.
  11. -spec hexToString(Hex :: string()) -> string().
  12. hexToString(Hex) ->
  13. {String, _} =
  14. lists:foldr(
  15. fun(E, {Acc, nolow}) ->
  16. {Acc, deHex(E)};
  17. (E, {Acc, LO}) ->
  18. {[deHex(E) * 16 + LO | Acc], nolow}
  19. end,
  20. {[], nolow}, Hex),
  21. String.
  22. -spec hexToBinary(Hex :: string()) -> string().
  23. hexToBinary(Hex) ->
  24. HexBin = list_to_binary(Hex),
  25. <<<<(deHex(X1) * 16 + deHex(X2))>> || <<X1:8, X2:8>> <= HexBin>>.
  26. -spec hexBinToString(HexBin :: binary()) -> string().
  27. hexBinToString(HexBin) ->
  28. [(deHex(X1) * 16 + deHex(X2)) || <<X1:8, X2:8>> <= HexBin].
  29. -spec hexBinToBinary(HexBin :: binary()) -> string().
  30. hexBinToBinary(HexBin) ->
  31. <<<<(deHex(X1) * 16 + deHex(X2))>> || <<X1:8, X2:8>> <= HexBin>>.
  32. -spec integerToHex(I :: integer()) -> string().
  33. integerToHex(I) ->
  34. erlang:integer_to_list(I, 16).
  35. -spec hexToInteger(Hex :: string()) -> integer().
  36. hexToInteger(Hex) ->
  37. lists:foldl(fun(E, Acc) -> Acc * 16 + deHex(E) end, 0, Hex).
  38. -spec rawBinaryToInteger(Bin :: binary()) -> integer().
  39. rawBinaryToInteger(Bin) ->
  40. rawBinaryToInteger(Bin, 0).
  41. -spec rawBinaryToInteger(Bin :: binary(), Acc :: integer()) -> integer().
  42. rawBinaryToInteger(<<>>, Acc) ->
  43. Acc;
  44. rawBinaryToInteger(<<X:8, Rest/binary>>, Acc) ->
  45. rawBinaryToInteger(Rest, Acc * 256 + X).
  46. -spec integerToRawBinary(I :: integer()) -> binary().
  47. integerToRawBinary(I) ->
  48. integerToRawBinary(I, 16, []).
  49. -spec integerToRawBinary(I :: integer(), Len :: pos_integer()) -> binary().
  50. integerToRawBinary(I, Len) ->
  51. integerToRawBinary(I, Len, []).
  52. integerToRawBinary(I, Len, Acc) when I < 256 ->
  53. NewAcc = [I | Acc],
  54. ZeroPadding = lists:duplicate(Len - length(NewAcc), 0),
  55. NewAcc2 = [ZeroPadding | NewAcc],
  56. list_to_binary(NewAcc2);
  57. integerToRawBinary(I, Len, Acc) ->
  58. Div = I div 256,
  59. Rem = I rem 256,
  60. integerToRawBinary(Div, Len, [Rem | Acc]).
  61. deHex(H) when H >= $a, H =< $f -> H - $a + 10;
  62. deHex(H) when H >= $A, H =< $F -> H - $A + 10;
  63. deHex(H) when H >= $0, H =< $9 -> H - $0.
  64. -compile({inline, [getHex/1]}).
  65. -spec getHex(X :: non_neg_integer()) -> integer().
  66. getHex(X) ->
  67. element(X + 1,
  68. {
  69. 16#3030, 16#3031, 16#3032, 16#3033, 16#3034, 16#3035, 16#3036,
  70. 16#3037, 16#3038, 16#3039, 16#3061, 16#3062, 16#3063, 16#3064,
  71. 16#3065, 16#3066, 16#3130, 16#3131, 16#3132, 16#3133, 16#3134,
  72. 16#3135, 16#3136, 16#3137, 16#3138, 16#3139, 16#3161, 16#3162,
  73. 16#3163, 16#3164, 16#3165, 16#3166, 16#3230, 16#3231, 16#3232,
  74. 16#3233, 16#3234, 16#3235, 16#3236, 16#3237, 16#3238, 16#3239,
  75. 16#3261, 16#3262, 16#3263, 16#3264, 16#3265, 16#3266, 16#3330,
  76. 16#3331, 16#3332, 16#3333, 16#3334, 16#3335, 16#3336, 16#3337,
  77. 16#3338, 16#3339, 16#3361, 16#3362, 16#3363, 16#3364, 16#3365,
  78. 16#3366, 16#3430, 16#3431, 16#3432, 16#3433, 16#3434, 16#3435,
  79. 16#3436, 16#3437, 16#3438, 16#3439, 16#3461, 16#3462, 16#3463,
  80. 16#3464, 16#3465, 16#3466, 16#3530, 16#3531, 16#3532, 16#3533,
  81. 16#3534, 16#3535, 16#3536, 16#3537, 16#3538, 16#3539, 16#3561,
  82. 16#3562, 16#3563, 16#3564, 16#3565, 16#3566, 16#3630, 16#3631,
  83. 16#3632, 16#3633, 16#3634, 16#3635, 16#3636, 16#3637, 16#3638,
  84. 16#3639, 16#3661, 16#3662, 16#3663, 16#3664, 16#3665, 16#3666,
  85. 16#3730, 16#3731, 16#3732, 16#3733, 16#3734, 16#3735, 16#3736,
  86. 16#3737, 16#3738, 16#3739, 16#3761, 16#3762, 16#3763, 16#3764,
  87. 16#3765, 16#3766, 16#3830, 16#3831, 16#3832, 16#3833, 16#3834,
  88. 16#3835, 16#3836, 16#3837, 16#3838, 16#3839, 16#3861, 16#3862,
  89. 16#3863, 16#3864, 16#3865, 16#3866, 16#3930, 16#3931, 16#3932,
  90. 16#3933, 16#3934, 16#3935, 16#3936, 16#3937, 16#3938, 16#3939,
  91. 16#3961, 16#3962, 16#3963, 16#3964, 16#3965, 16#3966, 16#6130,
  92. 16#6131, 16#6132, 16#6133, 16#6134, 16#6135, 16#6136, 16#6137,
  93. 16#6138, 16#6139, 16#6161, 16#6162, 16#6163, 16#6164, 16#6165,
  94. 16#6166, 16#6230, 16#6231, 16#6232, 16#6233, 16#6234, 16#6235,
  95. 16#6236, 16#6237, 16#6238, 16#6239, 16#6261, 16#6262, 16#6263,
  96. 16#6264, 16#6265, 16#6266, 16#6330, 16#6331, 16#6332, 16#6333,
  97. 16#6334, 16#6335, 16#6336, 16#6337, 16#6338, 16#6339, 16#6361,
  98. 16#6362, 16#6363, 16#6364, 16#6365, 16#6366, 16#6430, 16#6431,
  99. 16#6432, 16#6433, 16#6434, 16#6435, 16#6436, 16#6437, 16#6438,
  100. 16#6439, 16#6461, 16#6462, 16#6463, 16#6464, 16#6465, 16#6466,
  101. 16#6530, 16#6531, 16#6532, 16#6533, 16#6534, 16#6535, 16#6536,
  102. 16#6537, 16#6538, 16#6539, 16#6561, 16#6562, 16#6563, 16#6564,
  103. 16#6565, 16#6566, 16#6630, 16#6631, 16#6632, 16#6633, 16#6634,
  104. 16#6635, 16#6636, 16#6637, 16#6638, 16#6639, 16#6661, 16#6662,
  105. 16#6663, 16#6664, 16#6665, 16#6666
  106. }).