The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,262 other subscribers

Archive for June 17th, 2020

Chr equivalent for Unicode in Delphi 7 – Stack Overflow

Posted by jpluimers on 2020/06/17

From a long time ago: [WayBackchr equivalent for Unicode in Delphi 7 – Stack Overflow answered by David Heffernan:

Q

I need to initialize a Widestring in Delphi 7 but I can’t use chrfunction which is ANSI

var
  ws : Widestring;
begin

 ws := chr($FFFF) + chr($FFFF) + chr($FFFF);

end;

What can I use, then ?

A

I’m not sure there’s a simply way to do what you wish. You can convert a Word into a WideChar with a simple cast:

WideChar($FFFF)

but you cannot concatenate WideChar. So this is a compiler error:

WideChar($FFFF) + WideChar($FFFF)

You could use a helper function to get the job done:

function InitialiseWideString(const chars: array of Word): WideString;
var
  i: Integer;
begin
  SetLength(Result, Length(chars));
  for i := 0 to high(chars) do
    Result[i+1] := WideChar(chars[i]);
end;

Then you can call it like this:

ws := InitialiseWideString([$0054, $0069, $006D, $0065, $0020, $0074, $006F, 
  $0020, $0075, $0070, $0067, $0072, $0061, $0064, $0065]);

–jeroen

Posted in Delphi, Delphi 7, Development, History, Software Development | 3 Comments »

Dynamically instantiating instances in Delphi without knowing their full type is hard

Posted by jpluimers on 2020/06/17

For my reading list: [WayBack] So am working on a particular .NET project and came across NewtonSoft JSON.NET Serializer, it allows you serialize Collections such as a generic list an… – Ugochukwu Mmaduekwe – Google+:

Stefan Glienke:
Serializing and mostly deserializing generics in Delphi is a major PITA because we don’t have such capabilities to dynamically work with them like in C# where you can easily create new generic lists at runtime where you don’t know the precise type T at compile time.

–jeroen

 

Posted in .NET, C#, Delphi, Development, Software Development | Leave a Comment »

How to return a string value from a Bash function – Stack Overflow

Posted by jpluimers on 2020/06/17

Cool: you can return strings both as a function result, and by reference: they are explained in the question, second and fourth answer of [WayBack] How to return a string value from a Bash function – Stack Overflow.

Returning them by reference has two important benefits:

  1. it is much faster (especially useful in tight loop)
  2. you can use echo (normally used to return a result) for debugging purposes

I also needed a bit of switch magic which I found at [WayBack] bash – Switch case with fallthrough? – Stack Overflow and array magic (from [WayBack] Array variables) as arrays are far more readable than indirection (on the why not, see [WayBack] BashFAQ/006 – Greg’s Wiki: How can I use variable variables (indirect variables, pointers, references) or associative arrays?).

So here is a function that returns a specific IPv4 octet.

function getIpv4Octet() {
  IPv4=$1
  octetIndex=$2
  outputVariable=$3

  slice="${IPv4}"
  count=1
  while [ "${count}" -le 4 ]
  do
    octet[${count}]="${slice%%.*}"
    slice="${slice#*.}"
    count=$((count+1))
  done
   
  case "${octetIndex}" in
    "1" | "2" | "3" | "4")
      ;;
    *)
      octetIndex="4"
      ;;
  esac
  eval $outputVariable="${octet[$octetIndex]}"
}

You call it like this:

$ getIpv4Octet "192.168.178.32" 3 result && echo ${result}
178

–jeroen

Posted in *nix, *nix-tools, bash, bash, Development, Power User, Scripting, Software Development | Leave a Comment »