<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Found a better one-liner for encode. Also fixed that N >= 0!<div>(Still would like to replace the math:pow with something better)<br><div><div><br></div><div><div>encode(N, S) when N >= 0, S > 0 -></div><div> << <<(X-$0):4>> || X <- tl(integer_to_list(trunc(math:pow(10,S*2)) + (N rem trunc(math:pow(10,S*2))))) >>.</div></div><div><br></div><div>/Tony</div><div><br><div><div>On 4 feb 2012, at 16:21, Tony Rogvall wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Here is a version not using loops, 'case', 'if', io_lib nor lists (just for fun)<div>I put macros here to make the code a bit more readable :-)</div><div><br></div><div><div>-define(SIGN(X), ((abs((X)+1) - abs((X)-1)) div 2)).</div><div>-define(POSITIVEP(X), ((?SIGN((X)) + 1) div 2)).</div><div>-define(TRUNCD(N,S), ((N) rem trunc(math:pow(10,(S))))).</div><div><br></div><div>encode(N, Size) when N > 0, Size > 0 -></div><div> Bits = << <<(X-$0):4>> || X <- integer_to_list(?TRUNCD(N,Size*2))>>,</div><div> K = Size*8 - bit_size(Bits),</div><div> <<0:(K*?POSITIVEP(K)),Bits/bits>>.</div><div><br></div><div><br></div><div>decode(Bits) -></div><div> list_to_integer([X+$0 || <<X:4>> <= Bits]).</div></div><div><br></div><div>/Tony</div><div><br><div><div>On 4 feb 2012, at 13:29, Steve Davis wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>encode(N, Size) -><br><span class="Apple-tab-span" style="white-space:pre"> </span>encode0(N, Size * 2, <<>>).<br><br>encode0(N, Size, Acc) when Size > 0 -><br><span class="Apple-tab-span" style="white-space:pre"> </span>encode0(N div 10, Size - 1, <<(N rem 10):4, Acc/bits>>);<br>encode0(_, _, Acc) -><br><span class="Apple-tab-span" style="white-space:pre"> </span>Acc.<br><br>decode(N, Size) -><br><span class="Apple-tab-span" style="white-space:pre"> </span>case byte_size(N) of<br><span class="Apple-tab-span" style="white-space:pre"> </span>Size -><br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>decode0(N, 0);<br><span class="Apple-tab-span" style="white-space:pre"> </span>_ -><br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>error<br><span class="Apple-tab-span" style="white-space:pre"> </span>end.<br><br>decode0(<<X:4, Bin/bits>>, Acc) -><br><span class="Apple-tab-span" style="white-space:pre"> </span>decode0(Bin, Acc * 10 + X);<br>decode0(<<>>, Acc) -><br><span class="Apple-tab-span" style="white-space:pre"> </span>Acc.<br><br>..assuming the "size" argument in decode is a constraint. Not sure you<br>need it.<br><br>regs,<br>/s<br><br>On Feb 3, 11:01 pm, Avinash Dhumane <avin...@balinlabs.com> wrote:<br><blockquote type="cite">Can the following encode and decode functions be written without any library calls from modules 'lists' and 'io_lib'? Please show how. The test cases are included at the end. Thanks<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">$ cat bcd.erl<br></blockquote><blockquote type="cite">-module(bcd).<br></blockquote><blockquote type="cite">-compile(export_all).<br></blockquote><blockquote type="cite">% pack the digits of an integer as BCD in a given size of binary<br></blockquote><blockquote type="cite">% pad with leading zeros<br></blockquote><blockquote type="cite">encode(N, Size) -><br></blockquote><blockquote type="cite"> << <<X:4>> || X <- lists:flatten(io_lib:fwrite("~*..0B", [Size*2, N])) >>.<br></blockquote><blockquote type="cite">% unpack the given size of BCD binary into an integer<br></blockquote><blockquote type="cite">% strip leading zeros<br></blockquote><blockquote type="cite">decode(N, Size) -><br></blockquote><blockquote type="cite"> io_lib:fread("~d", [ X+$0 || <<X:4>> <= <<N:(Size*8)/bits>> ]).<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">$ erl<br></blockquote><blockquote type="cite">Eshell V5.8.3 (abort with ^G)<br></blockquote><blockquote type="cite">1> c(bcd).<br></blockquote><blockquote type="cite">{ok,bcd}<br></blockquote><blockquote type="cite">2> bcd:encode(1, 5).<br></blockquote><blockquote type="cite"><<0,0,0,0,1>><br></blockquote><blockquote type="cite">3> bcd:decode(v(2), 5).<br></blockquote><blockquote type="cite">{ok,[1],[]}<br></blockquote><blockquote type="cite">4> bcd:encode(12345, 5).<br></blockquote><blockquote type="cite"><<0,0,1,35,69>><br></blockquote><blockquote type="cite">5> bcd:decode(v(4), 5).<br></blockquote><blockquote type="cite">{ok,[12345],[]}<br></blockquote><blockquote type="cite">6> bcd:encode(1234567890, 5).<br></blockquote><blockquote type="cite"><<18,52,86,120,144>><br></blockquote><blockquote type="cite">7> bcd:decode(v(6), 5).<br></blockquote><blockquote type="cite">{ok,[1234567890],[]}<br></blockquote><blockquote type="cite">8><br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">_______________________________________________<br></blockquote><blockquote type="cite">erlang-questions mailing list<br></blockquote><blockquote type="cite">erlang-questi...@<a href="erlang.orghttp://erlang.org/mailman/listinfo/erlang-questions">erlang.orghttp://erlang.org/mailman/listinfo/erlang-questions</a><br></blockquote>_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br><a href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a><br></div></blockquote></div><br><div apple-content-edited="true">
<div><span class="Apple-style-span" style="font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; ">"Installing applications can lead to corruption over time. </span><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; ">Applications gradually write over each other's libraries, partial upgrades occur, user and system errors happen, and minute changes may be unnoticeable and difficult to fix"</span></div><div><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; "><br></span></div><br class="Apple-interchange-newline">
</div>
<br></div></div>_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>http://erlang.org/mailman/listinfo/erlang-questions<br></blockquote></div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; ">"Installing applications can lead to corruption over time. </span><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; ">Applications gradually write over each other's libraries, partial upgrades occur, user and system errors happen, and minute changes may be unnoticeable and difficult to fix"</span></div><div><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; "><br></span></div></span><br class="Apple-interchange-newline">
</div>
<br></div></div></div></body></html>