Oracle 12c equivalent to C# Encoding.ASCII.GetBytes

Viewed 200

I am trying to mimic the C# function Encoding.ASCII.GetBytes in Oracle 12c. I am nearly there but can't figure why I get the following results:

Oracle

declare
  l_string                varchar2(4000) := 'Test';
begin
  dbms_output.put_line(utl_raw.cast_to_raw(l_string));
end;

The output from this is:

54657374

C#

internal static string ConvertTest()
{
    var inputString = "Test";
    Console.WriteLine(BitConverter.ToString(Encoding.ASCII.GetBytes(inputString)));
}

The output from this is:

54-65-73-74

So I seem to be nearly there but I can't figure out why C# has '-' between each byte and Oracle does not.

Is there an Oracle function to replicate the output from C#?

Thanks.

2 Answers

utl_raw.cast_to_raw does not put '-' in between byte code of each character. I dont think there is a direct way to get series of byte codes in the expected format.

One workaround can be, you loop through each character and append '-' after each iteration.

Sample code -

declare
  l_string                varchar2(4000) := 'Test';
  l_result                varchar2(4000) := '';
begin
  for idx in 1 .. length(l_string)
  loop
     l_result := l_result || utl_raw.cast_to_raw(SUBSTR(l_string,idx,1));
     if idx != length(l_string) then
         l_result := l_result || '-';
     end if;
  end loop;
  dbms_output.put_line(l_result);
end;

I think I was chasing the wrong thing!

utl_raw.cast_to_raw is the equivalent to Encoding.ASCII.GetBytes(). It is BitConverter.ToString that is actually inserting the '-' between each byte.

Thanks for your help!

Related