How Do I convert bytes to ASCII chars, using Oracle PL/SQL - sql

I have a problem with conversion this bytes to ASCII chars, need to do similar conversion using Oracle PL/SQL, the original one is in MS SQLserver:
/// </summary>
/// <param name="blob"></param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static String VarbinaryToVarchar(SqlBytes blob)
{
if (blob.IsNull) return null;
byte[] buffer = blob.Buffer;
char[] res = new char[buffer.Length * 2];
for (int i = 0; i < buffer.Length; i++)
{
res[i * 2] = (char)((buffer[i] & 0xF) + 65);
res[i * 2 + 1] = (char)(((buffer[i] >> 4) & 0xF) + 65);
}
return new String(res);
}
Thanks in advance guys!

You could explore the use of Oracle built-in UTL_RAW coupled with HEXTORAW, then convert your hex. value to integer and add "65". After that, CHR will give you the ASCII you expect:
declare
p_s_in varchar2(1000):= '0000EB862CB8024892CD419A26BB3AE';
l_s_cmp varchar2(1000):= 'AAAALOGIMCILCAIEJNBEKJGCLIDLOK';
l_s_out varchar2(1000) :='';
p_pad binary_integer:=1;
l_cur1 char;
l_cur2 char;
l_cur_bi1 binary_integer;
l_cur_bi2 binary_integer;
begin
l_cur1 := substr(p_s_in, 1,1);
l_cur2 := substr(p_s_in, 2, 1);
while (length(l_cur1)=1)
loop
l_cur_bi1 := UTL_RAW.cast_to_binary_integer(HEXTORAW(l_cur1))+65;
l_cur_bi2 := UTL_RAW.cast_to_binary_integer(HEXTORAW(l_cur2))+65;
dbms_output.put_line(l_cur2||'->'||chr(l_cur_bi2)||'?>'||substr(l_s_cmp, 1,1));
l_s_cmp := substr(l_s_cmp, 2);
dbms_output.put_line(l_cur1||'->'||chr(l_cur_bi1)||'?>'||substr(l_s_cmp, 1,1));
l_s_out:=l_s_out||chr(l_cur_bi2)||chr(l_cur_bi1);
-- end loop
p_s_in := substr(p_s_in, 3);
l_s_cmp := substr(l_s_cmp, 2);
l_cur1 := substr(p_s_in, 1, 1);
l_cur2 := substr(p_s_in, 2, 1);
end loop;
dbms_output.put_line(l_s_out);
end;
/
I let debugging output, because I'm not completely sure this is what you're looking for. You gave us an output that slightly differs from what I get with your code:
... you or I might be missing something.

Related

How to sum 1+2^2+3^3+...n^n in pascal?

I want to sum that thing but only use the 'for loop' (no power functions). I've already created a loop that generates powers:
Program powers;
Var
i, n, result : integer;
writeln('enter N'), read(n);
BEGIN
Result := 1;
for i := 1 to n do
begin
Result := Result * n;
end;
writeln('result=',result);
END.
But I neither have any idea on how to make that code generate multiple powers ( this code only generates n^n) nor how to make a loop that sums them together.
It's best to break problems down into smaller problems. In this case, you may wish to have a pow function to handle the exponentiation for you.
Hopefully the pow function is acceptable if it's not a library function.
function pow(n, exp : integer) : integer;
var
i, result : integer;
begin
result := n;
for i := 2 to exp do
result := result * n;
pow := result;
end;
Then the main portion of your program is simpler.
program powers;
var
n, i, sum : integer;
function pow(n, exp : integer) : integer;
var
i, result : integer;
begin
result := n;
for i := 2 to exp do
result := result * n;
pow := result;
end;
begin
sum := 0;
write('enter N:');
readln(n);
for i := 0 to n do
sum := sum + pow(i, i);
writeln('result=', sum);
end.
Now that I finally understand what you're asking, please try this:
Program powers;
Var
i, j, n, p, result : integer;
BEGIN
write('enter N:');
readln(n);
Result := 0;
for i := 1 to n do
begin
p := 1;
for j := 1 to i do
p := p * i;
Result := Result + p;
end;
writeln('result=', result);
END.

Why won't this PL/SQL function compile?

I'm learning about Oracle Apex, Oracle SQL and PL/SQL and I'm currently learning about functions. I have written a function in which a table is created, data is stored and the table is returned to the calling query. The problem is the code won't compile. I can't see where the problem may be and I've compared the code to various online resources including this site. Everything looks fine to me, so the answer is not obvious to me.
This is the code I wrote:
CREATE OR REPLACE TYPE t_table AS OBJECT
(
futureValues NUMBER
);
CREATE OR REPLACE TYPE t_futureValues AS TABLE OF t_table;
/
CREATE OR REPLACE FUNCTION "CALCULATE_VALUE"( lastRowMinus0 IN NUMBER DEFAULT 1,
lastRowMinus1 IN NUMBER DEFAULT 2,
lastRowMinus2 IN NUMBER DEFAULT 3,
lastRowMinus3 IN NUMBER DEFAULT 4,
lastRowMinus4 IN NUMBER DEFAULT 5,
lastRowMinus5 IN NUMBER DEFAULT 6,
lastRowMinus6 IN NUMBER DEFAULT 7 )
RETURN t_futureValues AS
tableObject t_futureValues;
predictedValue NUMBER := 0;
lastRowMinus0Value NUMBER := 0;
lastRowMinus1Value NUMBER := 0;
lastRowMinus2Value NUMBER := 0;
lastRowMinus3Value NUMBER := 0;
lastRowMinus4Value NUMBER := 0;
lastRowMinus5Value NUMBER := 0;
lastRowMinus6Value NUMBER := 0;
avgDiff NUMBER := 0;
BEGIN
tableObject := t_futureValues();
lastRowMinus0Value := 3;
lastRowMinus1Value := 6;
lastRowMinus2Value := 9;
lastRowMinus3Value := 12;
lastRowMinus4Value := 14;
lastRowMinus5Value := 20;
lastRowMinus6Value := 60;
avgDiff := (lastRowMinus5Value - lastRowMinus6Value) + avgDiff;
avgDiff := (lastRowMinus4Value - lastRowMinus5Value) + avgDiff;
avgDiff := (lastRowMinus3Value - lastRowMinus4Value) + avgDiff;
avgDiff := (lastRowMinus2Value - lastRowMinus3Value) + avgDiff;
avgDiff := (lastRowMinus1Value - lastRowMinus2Value) + avgDiff;
avgDiff := (lastRowMinus0Value - lastRowMinus1Value) + avgDiff;
avgDiff := avgDiff / 6;
predictedValue := avgDiff + lastRowMinus0Value;
begin
for i in 2..13 loop
predictedValue := predictedValue + avgDiff;
IF predictedValue < 0 THEN
predictedValue := 0;
END IF;
insert into tableObject(futureValues)
values(predictedValue);
end loop;
end;
RETURN (tableObject);
END;
The error message I get is:
ORA-06545: PL/SQL: compilation error - compilation aborted ORA-06550: line 6, column 1: PLS-00103: Encountered the symbol "CREATE" ORA-06550: line 0, column 0: PLS-00565: T_TABLE must be completed as a potential REF target (object type)
I have no doubt that the problem is something simple, so if anyone knows, then I'd be grateful.
I don't work with types very often, but your insert is trying to insert into an array. A table must be the target of an insert.
Use the following code:
CREATE OR REPLACE TYPE brianl.t_table AS OBJECT
(
futureValues NUMBER
);
CREATE OR REPLACE TYPE brianl.t_futureValues AS TABLE OF t_table;
/
CREATE OR REPLACE FUNCTION brianl."CALCULATE_VALUE"( lastRowMinus0 IN NUMBER DEFAULT 1,
lastRowMinus1 IN NUMBER DEFAULT 2,
lastRowMinus2 IN NUMBER DEFAULT 3,
lastRowMinus3 IN NUMBER DEFAULT 4,
lastRowMinus4 IN NUMBER DEFAULT 5,
lastRowMinus5 IN NUMBER DEFAULT 6,
lastRowMinus6 IN NUMBER DEFAULT 7 )
RETURN t_futureValues AS
tableObject t_futureValues;
predictedValue NUMBER := 0;
lastRowMinus0Value NUMBER := 0;
lastRowMinus1Value NUMBER := 0;
lastRowMinus2Value NUMBER := 0;
lastRowMinus3Value NUMBER := 0;
lastRowMinus4Value NUMBER := 0;
lastRowMinus5Value NUMBER := 0;
lastRowMinus6Value NUMBER := 0;
avgDiff NUMBER := 0;
BEGIN
tableObject := t_futureValues();
lastRowMinus0Value := 3;
lastRowMinus1Value := 6;
lastRowMinus2Value := 9;
lastRowMinus3Value := 12;
lastRowMinus4Value := 14;
lastRowMinus5Value := 20;
lastRowMinus6Value := 60;
avgDiff := (lastRowMinus5Value - lastRowMinus6Value) + avgDiff;
avgDiff := (lastRowMinus4Value - lastRowMinus5Value) + avgDiff;
avgDiff := (lastRowMinus3Value - lastRowMinus4Value) + avgDiff;
avgDiff := (lastRowMinus2Value - lastRowMinus3Value) + avgDiff;
avgDiff := (lastRowMinus1Value - lastRowMinus2Value) + avgDiff;
avgDiff := (lastRowMinus0Value - lastRowMinus1Value) + avgDiff;
avgDiff := avgDiff / 6;
predictedValue := avgDiff + lastRowMinus0Value;
begin
for i in 2..13 loop
predictedValue := predictedValue + avgDiff;
IF predictedValue < 0 THEN
predictedValue := 0;
END IF;
tableobject.extend();
tableobject(tableobject.count).futureValues := predictedValue;
-- insert into tableObject(futureValues)
-- values(predictedValue);
end loop;
end;
RETURN (tableObject);
END;

Does HelpNDoc Pascal Script support structures?

I am trying to create a structure:
MyTopic
TopicID : String;
HelpID : Integer;
I wanted to create an array of these structures so I could sort them.
I have tried using this type / record syntax but it is failing.
Update
I defined this type and procedure:
type
TMyTopicRecord = record
idTopic : String;
idContextHelp : integer;
End;
procedure GetSortedTopicIDs(aTopics : array of String; size : Integer);
var
aMyTopicRecords : array of TMyTopicRecord;
temp : TMyTopicRecord;
iTopic, i, j : Integer;
begin
// Init the array
SetLength(aMyTopicRecords, size);
// Fill the array with the existing topid ids.
// Get the context ids at the same time.
for iTopic := 0 to size - 1 do
aMyTopicRecords[iTopic].idTopic := aTopics[iTopic];
aMyTopicRecords[iTopic].idContextHelp := HndTopics.GetTopicHelpContext(aTopics[iTopic]);
// Sort the array on context id
for i := size-1 DownTo 1 do
for j := 2 to i do
if (aMyTopicRecords[j-1].idContextHelp > aMyTopicRecords[j].idContextHelp) Then
begin
temp := aMyTopicRecords[j-1];
aMyTopicRecords[j-1] := aMyTopicRecords[j];
aMyTopicRecords[j] := temp;
end;
// Rebuild the original array of topic ids
for iTopic := 0 to size - 1 do
aTopics[iTopic] := aMyTopicRecords[iTopic].idTopic;
end;
The procedure gets called in a loop of the parent function (code snipped):
function GetKeywordsAsHtml(): string;
var
aKeywordList: THndKeywordsInfoArray;
aAssociatedTopics: array of string;
nBlocLevel, nDif, nClose, nCurKeywordLevel, nCurKeywordChildrenCnt: Integer;
nCurKeyword, nCurKeywordTopic: Integer;
nCountAssociatedTopics: Integer;
sCurrentKeyword, sKeywordLink, sKeywordRelated: string;
sKeywordJsCaption: string;
begin
Result := '<ul>';
nBlocLevel := 0;
try
aKeywordList := HndKeywords.GetKeywordList(False);
for nCurKeyword := 0 to length(aKeywordList) - 1 do
begin
sCurrentKeyword := aKeywordList[nCurKeyword].id;
nCurKeywordLevel := HndKeywords.GetKeywordLevel(sCurrentKeyword);
nCurKeywordChildrenCnt := HndKeywords.GetKeywordDirectChildrenCount(sCurrentKeyword);
sKeywordLink := '#';
sKeywordRelated := '[]';
aAssociatedTopics := HndTopicsKeywords.GetTopicsAssociatedWithKeyword(sCurrentKeyword);
nCountAssociatedTopics := Length(aAssociatedTopics);
if nCountAssociatedTopics > 0 then
begin
GetSortedTopicIDs(aAssociatedTopics, nCountAssociatedTopics);
// Code snipped
end;
end;
finally
Result := Result + '</ul>';
end;
end;
The script compiled in the HelpNDoc internal editor with no issues. But when I go to actually build my HTML documentation I encounter a problem:
The HelpNDoc API is explained here.
Is there something wrong with my code?
I decided to go about it a different way and used a simpler technique:
procedure GetSortedTopicIDs(var aTopics : array of String; iNumTopics : Integer);
var
iTopic : Integer;
// List of output
aList: TStringList;
begin
// Init list
aList := TStringList.Create;
// Build a new array of "nnn x"
// - nnn is the help context id
// - x is the topid id
// Note: I know that the context ID values are within the range 0 - 200
for iTopic := 0 to iNumTopics - 1 do
// We pad the context id with 0. We could increase the padding width to
// make the script mre useful
aList.Add(Format('%0.3d %s', [
HndTopics.GetTopicHelpContext(aTopics[iTopic]),
aTopics[iTopic]
]));
// Now we sort the new array (which basically sorts it by context id)
aList.Sort;
// Update original array
for iTopic := 0 to iNumTopics - 1 do
// We ignore the "nnn " part of the string to get just the topic id
aTopics[iTopic] := copy(aList[iTopic],5, length(aList[iTopic])-4);
// Tidy up
aList.Free;
end;
This compiles and I get the sorted array of topic IDs at the end of it. So the pop-up help is now listed as I want.

how to compare two linear lists in pascal

The task is to create new list which contains 2 linear lists, and if elements of first list equal to elements of second list then we must delete the same element from new list
procedure CreateList(var t1,L1,t2,L2:plist);
var
tmp1, tmp2:plist;
begin
t1 := L1;
t2 := L2;
while t1 <> nil do
begin
write(t1^.Data, ' ');
t1 := t1^.Next;
while t2 <> nil do
begin
write(t2^.data, ' ');
tmp2:=t2;
t2 := t2^.Next;
if(t2 = tmp1^.next) then // here is the problem how to compare 2 elements
begin
tmp1 := t1;
t1 := t1^.Next;
Dispose(tmp1);
end;
end;
end;
Writeln;
readkey;
end;
I advise you to change type of you list for something like:
type
TData = integer;
PList = ^TList;
TList = record
data : TData;
next : PList;
copy : boolean;
end;
After you will use value .copy for checking copys. Here is simple code you need.
I tried to make all code as simple as possible.
procedure deleteCopys(var list:PList);
// make all TList.copy := false (after we will use it)
var
curList : PList;
begin
curList := list;
while (curList<>nil) do
begin
curList^.copy := false;
curList := curList^.next;
End;
End;
procedure createList(var list1, list2, list3:PList);
// we belive that: (list3 is empty) and (there is no copys in list1)
// and (there is no copys in list2)
var
curList1, curList2 : PList;
begin
deleteCopys(list1);
deleteCopys(list2);
deleteCopys(list3);
// make TList.copy := true for equal elements
// from curList1 and curList2
curList1 := list1;
while (curList1 <> nil) do
begin
curList2 := list2;
while (curList2 <> nil) do
begin
if (curList1^.data = curList2^.data) then
begin
curList1^.copy := true;
curList2^.copy := true;
break;
End;
curList2 := curList2^.next;
End;
curList1 := curList1^.next;
End;
// now we can put all elements from list1 and list2
// with state TLilt.copy=false
// and each of them will be different
// (Let's do it :)
curList1 := list1;
while (curList1 <> nil) do
begin
if (curList1^.copy = false) then
begin
//some procedure for new element
putNewElement(list3, curList1^.data);
End;
End;
curList2 := list2;
while (curList2 <> nil) do
begin
if (curList2^.copy = false) then
begin
//some procedure for new element
putNewElement(list3, curList2^.data);
End;
End;
end;
I hope it will help you!

How do I iterate over a MULTI_SZ string in Inno Setup?

How do I iterate over a MULTI_SZ string in Inno Setup Pascal scripting?
e.g. over values returned by RegQueryMultiStringValue
Here's the code to do it:
var
I,StartPos:Integer;
MultiSzStr,Element:String;
...
StartPos := 1;
for I:= 1 to Length(MultiSzStr) do
begin
if MultiSzStr[I] = #0 then
begin
Element := Copy(MultiSzStr, StartPos, I - StartPos);
{ do something with the element }
StartPos := I + 1;
end;
end;