How to know the size in Kb of a string?

5

I have a string that will be saved in xml file , and this file can not be larger than 500 kb .

  • How can I identify the size of string that I will save in xml file ?
  • When string is saved in the xml file, it can change size with the addition of Windows information? For example: File Creation Date and etc ...
  • asked by anonymous 25.03.2014 / 19:29

    4 answers

    3

    1) If you are using Delphi 7 (according to the tag), just measure using the Length method and it will give you the size in Bytes.

    But this can soon cause you problems if you upgrade your Delphi to a version that uses Unicode. One means that would be proof of updates is to use Length(S) * SizeOf(Char) which will also give you the value in bytes. Code sample below:

    function TamanhoEmBytes(Const strAMedir: String): integer;
    begin
      Result := Length(S) * SizeOf(Char);
    end;
    

    More on the subject:

    link

    link

    2) Information such as access date and file creation date as well as permissions are stored according to the file system of the partition where the file was stored. This will depend on where the file is stored. However, in most cases (all?), This data is stored in a separate file system table, affecting the file size in no way.

    For example: In the NTFS system that is standard from Windows XP to Windows 8, it is stored in the "Master File Table";

        
    26.03.2014 / 16:17
    5

    Another alternative, native to Delphi 2009 or higher, is the ByteLength of unit SysUtils .

    See an example (taken from the documentation):

    var
      LS1: WideString;
    
    begin
      LS1 := 'Hello World!';
      Writeln('UnicodeString: ''', LS1, ''' contains ', Length(LS1), ' characters in ', ByteLength(LS1), ' bytes.');
      Readln;
    end.
    

    The output will be:

    Aswell mentioned by @ EMBarbosa The ByteLength function is not available for versions lower than Delphi 2009, by looking at the source code of the ByteLength function you can see that it is essentially the same code posted in the EMBarbosa response .

    Code:

    function ByteLength(const S: string): Integer;
    begin
    Result := Length(S) * SizeOf(Char);
    end;
    
        
    27.03.2014 / 05:09
    0

    I could not find a simple answer to your first question, however:

    What you can do to get an idea of the size of your XML is to stress your font.

    I did a stress test in 5 minutes. It probably will not suit you, for having parameters other than your xml (which you did not submit when asking the question) , but it is necessary to explain my argument. Following:

    Var
      XML : IXMLDOCUMENT;
      RootNode, CurNode : IXMLNODE;
      i : integer;
    begin
      XML := NewXMLDocument;
      XML.Encoding := 'utf-8';
      XML.Options := [doNodeAutoIndent]; // looks better in Editor ;)
      RootNode := XML.AddChild('XML');
      CurNode := RootNode.AddChild('Teste');
      CurNode := CurNode.AddChild('Test22');
      CurNode.Text := 'Texto Texto Test';
      for I := 0 to 99 do
      begin
        CurNode.Attributes['Novo_Atributo' + IntToStr(i)] := 'Valor';
      end;
    
    
      XMl.SaveToFile('C:\teste.xml');
    

    With this test I noticed something interesting:

    No stretch

      for I := 0 to 99 do
      begin
        CurNode.Attributes['Novo_Atributo' + IntToStr(i)] := 'Valor';
      end;
    

    I have the following size on disk: 4,00 KB (4,096 bytes)

    Changing the value of the loop from 99 to 199

      for I := 0 to 199 do
      begin
        CurNode.Attributes['Novo_Atributo' + IntToStr(i)] := 'Valor';
      end;
    

    I have the following size on disk: 8.00 KB (8,192 bytes)

    From there, not reaching your 500kb becomes a simple 3 rule.

    As for the second, from the moment your file is saved, as a rule nothing changes its size, be carefree.

        
    25.03.2014 / 21:07
    0

    The IXMLDocument interface has a property called XML , which is TStrings . This type has a property called Text which is the string representation of the XML document that will be written. If you do:

    tamanho := Length(MeuXMLDocument.XML.Text) / SizeOf(Char);
    

    will have the size in bytes of the document.

        
    26.03.2014 / 20:49