How to know the Kb size of a string?

Asked

Viewed 1,842 times

5

I have a string which will be saved on file xml, and this file cannot be larger than 500 kb.

  1. How can I identify the size of the string which I will save on file xml?
  2. When the string is saved in the archive xml, it can change size with the addition of Windows information?
    For example: File Creation Date and etc...
  • See if this site can help you.

  • @Samirbraga Help in part. The tip of the informed article explains how to check the size of a file. However, I need to check the size of a particular String which I will still save in xml file.

  • To check if a file larger than 500kb obeys the rule, you first need to create this file...

  • To know what size the file will get, just count the size of the string in bytes, using Length. There is no need to physically create a copy on disk. In fact, if the file is going to be sent as an Nfe, for example, a local copy may well be stored in a database, and the XML string is sent directly to the SEFAZ (creating an intermediate file is totally unnecessary).

  • It is usually not recommended to ask two or more questions together when they do not interfere with each other. It’s hard for you to choose the best answer.

4 answers

5

Another alternative, native from Delphi 2009 or later, is the function 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 exit will be: inserir a descrição da imagem aqui


As well mentioned by @Embarbosa the function ByteLength is not available for lower versions than Delphi 2009 by looking at the function source code ByteLength it is possible to see that it is essentially the same code posted in Embarbosa’s reply.

inserir a descrição da imagem aqui

Code:

function ByteLength(const S: string): Integer;
begin
Result := Length(S) * SizeOf(Char);
end;
  • 1

    This method is not available in Delphi 7. And he tagged the question with the tag Delphi-7.

  • It was beautiful, almost better than mine. P :D

3


1) If you are using Delphi 7 (as tagged), simply measure using the method Length 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 way that would be proof of upgrades is to use Length(S) * SizeOf(Char) which will also give you the value in bytes. Example of code below:

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

More on the subject:

http://edn.embarcadero.com/article/38693

https://stackoverflow.com/questions/16529992/delphi-unicode-string-length-in-bytes

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

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

0

I did not find a simple answer to your first question, but it follows:

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 won’t suit you, because it has parameters other than your xml (that you did not send when asking the question), but it is necessary to explain my argument. Follow:

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 realized something interesting:

In the stretch

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

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

Changing the value of the bond from 99 to 199

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

I have the following disk size: 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, don’t worry.

0

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

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

will have the document’s byte size.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.