3

I'm using Delphi XE2. Pretty self explanatory question. I have a variant in which I'm storing a single character (eg. '?') and I want to cast it as an AnsiChar. If I can't cast it, then how do I convert it. I've tried to cast it the usual way casting is done, but it didn't work. I've tried searching on Google and found no straight forward answers. I'm not interested in the theory of unicode conversion. I'm looking for the reference information in one or two lines of delphi code.

5
  • How is it stored in the Variant. It should be stored as an integral type. Oct 24, 2013 at 7:34
  • That's just wishful thinking. In fact you do not have a character stored in this variant. You have stored a string in the variant. Which is of course why your attempts to cast fail. If you want to store a single character, do so rather than using a string. Oct 24, 2013 at 9:59
  • I presume that's sarcasm, but I'm not sure what point you are making Oct 24, 2013 at 12:38
  • @Sam - if you really want an AnsiChar specifically, then your thinking is already beyond 'intuitive', 'abstracted' etc. and concerned with implementation details instead. What's the context out of interest, i.e. why do you need an AnsiChar specifically? Oct 24, 2013 at 13:35
  • You also have the possibility to declare your own variant types, should the need arise, TCustomVariantType.
    – LU RD
    Oct 24, 2013 at 21:42

2 Answers 2

7

This is one way to do it:

v : Variant;
a : AnsiChar;
...
v := AnsiChar('a'); // Will be stored as an AnsiString
// Check variant
if VarIsStr(v) and (Length(AnsiString(v)) = 1) then
  a := AnsiString(v)[1];  // Cast variant holding an AnsiString to an AnsiChar
//Another way
if (VarType(v) = varString) and (Length(AnsiString(v)) = 1) then
  a := AnsiString(v)[1];  // Cast variant holding an AnsiString to an AnsiChar

The variant can hold different string types (AnsiString=varString,UnicodeString=varUString and WideString=varOleStr).

If you are storing your AnsiChar as a byte in your variant:

v := Byte(AnsiChar('A')); // Will be stored as a byte
if (VarType(v) = varByte) then
  a := AnsiChar(Byte(v));

Note: Storing an AnsiChar as a byte vs. an AnsiString is to prefer, since you avoid heap allocations.


Noted in comments that you assign your variants as:

v := 'c';

This means that your variant string will be in a varUString format.

if (Vartype(v) = varUString) and (Length(String(v)) = 1) then 
  a := AnsiChar(String(v)[1]); 

Should you want to cover all bases and have a one-for-all variant conversion routine:

function GiveMeThatAnsiCharFromVariant( const v: Variant; var a: AnsiChar): Boolean;
// Accepts UnicodeString,AnsiString,WideString,byte for conversion to AnsiChar;
begin
  Result := False;
  if VarIsStr(v) then
  begin
    case VarType(v) of
    varString :
      if (Length(AnsiString(v)) = 1) then
      begin
        a := AnsiChar(AnsiString(v)[1]);
        Result := True;
      end;
    varUString :
      if (Length(String(v)) = 1) then
      begin
        a := AnsiChar(String(v)[1]);
        Result := True;
      end;
    varOleStr :
      if (Length(Widestring(v)) = 1) then
      begin
        a := AnsiChar(Widestring(v)[1]);
        Result := True;
      end;
    end;
  end
  else
  begin
    if (VarType(v) = varByte) then
    begin
      a := AnsiChar(Byte(v));
      Result := True;
    end;
  end;
end;
1
  • 1
    +1 for identifying the correct way to store a character in a variant Oct 24, 2013 at 8:01
5

If you variant is holding a AnsiChar (Which is stored as an AnsiString) you can access the VString field of the TVarData and then cast the value to AnsiString. like so

var
 v : Variant;
 a : AnsiChar;
begin
 v:=AnsiString('a'); //this is a variant holding a single AnsiChar 
 if (TVarData(v).VType=varString) and (TVarData(v).VString<>nil) then
   a:=AnsiString(TVarData(v).VString)[1];// or a:= AnsiString(v)[1];

If you variant is holding a Char (Which is stored as an String) you can access the VUString field of the TVarData and then cast the value to AnsiString. like so

 v:=String('a');
 if (TVarData(v).VType=varUString) and (TVarData(v).VUString<>nil) then
   a:= AnsiString(VarToStr(v))[1];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.