Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7218691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:31:00+00:00 2026-05-28T21:31:00+00:00

To read a index file in a specific format, I cooked the following piece

  • 0

To read a index file in a specific format, I cooked the following piece of code without considering byte ordering:

unit uCBI;

interface

uses
  SysUtils,
  Classes,
  Generics.Collections;

type
  TIndexList = class
  private
    FIndexList:TList<Cardinal>;

    FOwnedStream:Boolean;
    FMemoryStream: TMemoryStream;
    function GetCount: Integer;

  protected

  public
    constructor Create(AStream:TMemoryStream; OwnedStream:Boolean=True);
    destructor Destroy; override;

    function Add(const Value: Cardinal): Integer;
    procedure Clear;

    procedure SaveToFile(AFileName:TFileName);
    procedure LoadFromFile(AFileName:TFileName);

    property Count: Integer read GetCount;
  end;

implementation

{ TIndexList }

function TIndexList.Add(const Value: Cardinal): Integer;
begin
  Result := FIndexList.Add(Value)
end;

procedure TIndexList.Clear;
begin
  FIndexList.Clear;
end;

constructor TIndexList.Create(AStream: TMemoryStream; OwnedStream: Boolean);
begin
  FMemoryStream := AStream;
  FOwnedStream := OwnedStream;
  FIndexList := TList<Cardinal>.Create;
end;

destructor TIndexList.Destroy;
begin
  if (FOwnedStream and Assigned(FMemoryStream)) then
    FMemoryStream.Free;

  FIndexList.Free;
  //
  inherited;
end;

function TIndexList.GetCount: Integer;
begin
  Result := FIndexList.Count;
end;

procedure TIndexList.LoadFromFile(AFileName: TFileName);
var
  lMemoryStream:TMemoryStream;
  lCount:Cardinal;
begin
  lMemoryStream := TMemoryStream.Create;

  try
    lMemoryStream.LoadFromFile(AFileName);
    lMemoryStream.ReadBuffer(lCount,SizeOf(Cardinal));

    if (lCount = Cardinal((lMemoryStream.Size-1) div SizeOf(Cardinal))) then
    begin
      FMemoryStream.Clear;
      lMemoryStream.Position :=0;
      FMemoryStream.CopyFrom(lMemoryStream,lMemoryStream.Size)
    end else
      raise Exception.CreateFmt('Corrupted CBI file: %s',[ExtractFileName(AFileName)]);
  finally
    lMemoryStream.Free;
  end;
end;

procedure TIndexList.SaveToFile(AFileName: TFileName);
var
  lCount:Cardinal;
  lItem:Cardinal;
begin
  FMemoryStream.Clear;

  lCount := FIndexList.Count;
  FMemoryStream.WriteBuffer(lCount,SizeOf(Cardinal));

  for lItem in FIndexList do
  begin
    FMemoryStream.WriteBuffer(lItem,SizeOf(Cardinal));
  end;
  //
  FMemoryStream.SaveToFile(AFileName);
end;

end.

It tested it and seems to work well as needed. Great was my suprise when I pursue extensive tests with real sample file. In fact the legacy format was devised with Amiga computer with a different byte ordering.

My Question:

How can I fix it ?

I want to keep the code unchanged and wonder wether a decorated TMemorySream will do so that I can transparently switch between big endian and little endian.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:31:01+00:00Added an answer on May 28, 2026 at 9:31 pm

    To change ‘endianness’ of Cardinals you can use the following:

    function EndianChange(Value: Cardinal): Cardinal;
    var
      A1: array [0..3] of Byte absolute Value;
      A2: array [0..3] of Byte absolute Result;
      I: Integer;
    
    begin
      for I:= 0 to 3 do begin
        A2[I]:= A1[3 - I];
      end;
    end;
    

    If you want to keep your code unchanged, you can write your own TMemoryStream descendant and override its Read and Write methods using the above function, like that:

    function TMyMemoryStream.Read(var Buffer; Count: Integer): Longint;
    var
      P: PCardinal;
      I, N: Integer;
    
    begin
      inherited;
      P:= @Buffer;
      Assert(Count and 3 = 0);
      N:= Count shr 2;
      while N > 0 do begin
        P^:= EndianChange(P^);
        Inc(P);
        Dec(N);
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a binary file with following format : [N bytes identifier & record
I try to read Claroline source code to learn from their coding. In index.php
... after having just read http://www.cocoadev.com/index.pl?CocoaInsecurity ... I am curious to know about your
How to get checked RadioButton Index using jQuery? I need to read it and
I wrote a bunch of code in Haskell to create an index of a
I'm using a FileReader wrapped in a LineNumberReader to index a large text file
I've read in and stored a data file that I am processing into an
I have a text file I use to hold an index of files and
Is there any documentation about internal structure of Git index file? From various book
How to include .css and .js files in a php file (index.html.php for example)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.