将 0 和 1 的数组转换为 base36 字符串

发布于 2024-09-01 10:42:46 字数 177 浏览 2 评论 0原文

事情是这样的。我有一个 0 和 1 的数组。它应该是一个二进制字符串。我需要的是将其格式化为包含该二进制文件的 base36 转换的字符串。 换句话说,我需要这样做: 1 和 0 的数组 ->某种二进制数 ->转换为base36数字->将其放入字符串中。 怎么做呢?

数千石油,300钢铁,回答者+1运气。

Here is the thing. I have an array of 0 and 1. It is supposed to be a binary string. What i need is to format it to string that contains base36 conversion from that binary.
In other words, i need to do this: array of 1 and 0 -> some kind of binary number -> convert to base36 number -> put it into string.
How to do that?

Thousands of oil, 300 steel and +1 luck to answerers.

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

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

发布评论

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

评论(1

屋檐 2024-09-08 10:42:46

没有内置的 Oracle 来执行此类转换。在下面的示例中,我使用了 不可估量的 Mr Kyte 编写的两个函数to_dec() 将其他基数转换为小数,to_base() 将小数转换为其他基数。

该过程采用一个由 1 和 0 组成的数组,并返回一个以 36 为基数的字符串。

create or replace type binary_nt as table of number(1,0);
/

create or replace function base2_to_base36 
    (p_onesnzeroes in binary_nt)
    return varchar2
is
    s_b2 varchar2(38);
    n_b10 pls_integer;
    s_b36 varchar2(38);
begin
    for i in 1..p_onesnzeroes.count()
    loop
        s_b2 := s_b2||trim(to_char(p_onesnzeroes(i)));
    end loop;

    n_b10 := to_dec(s_b2, 2);
    s_b36 := to_base(n_b10, 36);

    return s_b36;
end;
/

布丁的证明以及所有这些...

SQL> set serveroutput on size unlimited
SQL>     declare
  2          bins binary_nt := binary_nt(1,0,0,0,1,1,1,0);
  3          s varchar2(128);
  4      begin
  5          --  10001110 => 142  => 3Y
  6          s :=  base2_to_base36(bins);
  7          dbms_output.put_line(s);
  8      end;
  9      /
3Y

PL/SQL procedure successfully completed.

SQL>

编辑

当我组装此示例时,您发布了您的零数组是~450 个条目长。这个例程不会处理这样的事情。在您命中该大小的数字之前,它会抛出ORA-01426: numeric Overflow

编辑2

如果您愿意冒险一点不精确,您可以将 NUMBER 变量替换为 BINARY_DOUBLE 变量(在我的示例和 Tom 的函数中)。该数据类型可以处理更大的数字。我将其提高到 array_count=470,在基数 36 中可能如下所示:

EKQA1EJ6QB4SC8WOOWKWGGOS4KWWWWCS4WCW4SCWCOSOOS888K4CSC8SWO8OCKC8SSCWCOGK844CKG00SOW8KGS0CC4

There is no Oracle built-in to do this sort of conversion. In the following example I use two functions witten by the inestimable Mr Kyte. to_dec() turns other bases to decimals, and to_base() turns decimals into other bases.

The procedure takes an array of ones and zeroes and returns a string of base 36.

create or replace type binary_nt as table of number(1,0);
/

create or replace function base2_to_base36 
    (p_onesnzeroes in binary_nt)
    return varchar2
is
    s_b2 varchar2(38);
    n_b10 pls_integer;
    s_b36 varchar2(38);
begin
    for i in 1..p_onesnzeroes.count()
    loop
        s_b2 := s_b2||trim(to_char(p_onesnzeroes(i)));
    end loop;

    n_b10 := to_dec(s_b2, 2);
    s_b36 := to_base(n_b10, 36);

    return s_b36;
end;
/

The proof of the pudding and all that ...

SQL> set serveroutput on size unlimited
SQL>     declare
  2          bins binary_nt := binary_nt(1,0,0,0,1,1,1,0);
  3          s varchar2(128);
  4      begin
  5          --  10001110 => 142  => 3Y
  6          s :=  base2_to_base36(bins);
  7          dbms_output.put_line(s);
  8      end;
  9      /
3Y

PL/SQL procedure successfully completed.

SQL>

edit

While I was assembling this sample you posted that your array of zeroes was ~450 entries long. This routine won't handle anything like that. It will hurl ORA-01426: numeric overflow way before you hit a number of that size.

edit 2

If you are happy to gamble with a little imprecision, you could replace the NUMBER variables with BINARY_DOUBLE variables (in both my sample and Tom's functions). That datatype can handle much bigger numbers. I cranked it up to array_count=470, which might look like this in base 36 :

EKQA1EJ6QB4SC8WOOWKWGGOS4KWWWWCS4WCW4SCWCOSOOS888K4CSC8SWO8OCKC8SSCWCOGK844CKG00SOW8KGS0CC4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文