作为一个匿名程序:(注意:这样可以正常工作)
DECLARE
strBinary varchar2(32);
intAnswer number := 0;
intExponent number := 0;
strReverse varchar2(32);
strLength number := 0;
intBinChar number := 0;
BinChar char;
BEGIN
dbms_output.enable;
-- Reverse the Binary String first using similar function to Reversed_Name
strBinary := ('11111111');
strLength := Length(StrBinary) + 1;
FOR i IN 1..strLength LOOP
BinChar := SUBSTR(strBinary,strLength-i,1);
strReverse := strReverse || BinChar;
END LOOP;
--Extract the binary character from the reversed string and do the math
FOR i IN REVERSE 1..LENGTH(strReverse)-1 LOOP
BinChar := SUBSTR(strReverse, i, 1);
intExponent := i - 1;
intBinChar := TO_NUMBER(BinChar);
intAnswer := intAnswer + (intBinChar * (2 ** intExponent));
END LOOP;
-- RETURN intAnswer;
dbms_output.put_line(intAnswer);
END;--RKC_BinToInt;
这里的功能是无效的:
CREATE OR REPLACE FUNCTION RKC_BinToInt(strBinary IN varchar2(32) RETURN number IS
intExponent number := 0;
strReverse varchar2(32);
strLength number := 0;
intBinChar number := 0;
BinChar char;
BEGIN
-- Initialize Reverse the Binary String first using similar function to Reversed_Name
intAnswer := 0;
strLength := Length(StrBinary) + 1;
FOR i IN 1..strLength LOOP
BinChar := SUBSTR(strBinary,strLength-i,1);
strReverse := strReverse || BinChar;
END LOOP;
--Extract the binary character from the reversed string and do the math
FOR i IN REVERSE 1..LENGTH(strReverse)-1 LOOP
BinChar := SUBSTR(strReverse, i, 1);
intExponent := i - 1;
intBinChar := TO_NUMBER(BinChar);
intAnswer := intAnswer + (intBinChar * (2 ** intExponent));
END LOOP;
RETURN intAnswer;
END RKC_BinToInt;
/
答案 0 :(得分:0)
你的功能应该是这样的
CREATE OR REPLACE FUNCTION RKC_BinToInt(strBinary IN varchar2) RETURN number IS
intExponent number := 0;
strReverse varchar2(32);
strLength number := 0;
intBinChar number := 0;
BinChar char;
intAnswer NUMBER;
BEGIN
-- Initialize Reverse the Binary String first using similar function to Reversed_Name
intAnswer := 0;
strLength := Length(StrBinary) + 1;
FOR i IN 1..strLength LOOP
BinChar := SUBSTR(strBinary,strLength-i,1);
strReverse := strReverse || BinChar;
END LOOP;
--Extract the binary character from the reversed string and do the math
FOR i IN REVERSE 1..LENGTH(strReverse)-1 LOOP
BinChar := SUBSTR(strReverse, i, 1);
intExponent := i - 1;
intBinChar := TO_NUMBER(BinChar);
intAnswer := intAnswer + (intBinChar * (2 ** intExponent));
END LOOP;
RETURN intAnswer;
END RKC_BinToInt;
/
答案 1 :(得分:0)
我发现了答案:
问题不在于我为了进行基本的Bin到Dec转换而创建的非常复杂的代码。
问题是RETURN变量intAnswer必须在Header(Create或Replace)语句中声明。即使我在代码的BEGIN部分初始化了变量,编译器也没有它的句柄。这只是一些不好的建议的结果,因为我问过一个非常有经验的用户是否必须首先声明该变量。注意:RETURN不会声明在程序体内使用的变量 - 它在Anonymous程序中工作,因为它当然是声明的。
感谢您抽出宝贵时间帮助我们所有新手。对于本网站上的每一张海报,您都应该得到的不仅仅是对您的不懈努力的简单谢谢。
再次感谢 罗伯坎贝尔