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 781659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:15:59+00:00 2026-05-14T20:15:59+00:00

I need to convert a binary file (a zip file) into hexadecimal representation, to

  • 0

I need to convert a binary file (a zip file) into hexadecimal representation, to then send it to sql-server as a varbinary(max) function parameter.

A full example (using a very small file!) is:

1) my file contains the following bits 0000111100001111

2) I need a procedure to QUICKLY convert it to 0F0F

3) I will call a sql server function passing 0x0F0F as parameter

The problem is that I have large files (up to 100MB, even if average file size is 100KB files are possible), so I need the fastest way to do this.

Otherwise stated: I need to create the string

'0x'+BinaryDataInHexadecimalRepresentation

in the most efficient way. (Note: may be there is a way to immediately open a file and obtain an hexadecimal string, so in this case all I need is to use “this way”, if it is there).

Related question: passing hexadecimal data to sql server

UPDATE: after reading the comments I think it is needed to add more information here. The reason why I try to use a T-SQL text command to send the binary data to the stored procedure is that in this way I remove some overhead to the server: the stored prcoedure recieves the binary data and writes it to a file (this is my final goal). If I use a DAC component I will be able to easily send the biray data to the server, but in that case I need to use a temp table to store the data, and then sending this data to the storedprocedure that writes the file.

So the idea is:

1) using T-SQL “long” command: more overhead on client because I need to read the file and convert it to hexadecimal to preparing the long command; less server overhead since sql server just recieves the binary data and processes it in the stored function

2) using DAC: I need to pass through a temp table in sql server, therefore having more overhead on the server

Since I am using the server as web document server (it’s a trick), I want to try to reduce the overhead on the server. Anyway may be I am wrong and (2) anyway is a better technique than (1)

  • 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-14T20:16:00+00:00Added an answer on May 14, 2026 at 8:16 pm

    Well here’s a option that would do the conversion as fast as I can think of.

    Features of the code:

    • Only one allocation for the string (so no realloc and no move or copy)
    • Fast read from the file.

    Since we know that one byte translates to exactly two hexadecimal chars, we know our result string needs to be exactly twice the size of the file. We allocate an string of the required size and then we read from the file in large-enough blocks so the OS can optimize it for us (reading byte-by-byte is evil). We use an actual string but we write into the string using an pointer:

    function TEditorDeschidereDeCredite.FileToHex(FileName: string): AnsiString;
    var FS:TFileStream;
        PS:PAnsiChar;
        Block:array[0..1023] of byte; // 1Kb
        len,i,pos:Integer;
        B:Byte;
    const Nibbs: array[0..15] of AnsiChar = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
    begin
      FS := TFileStream.Create(FileName, fmOpenRead);
      try
        Result := '';
        SetLength(Result, FS.Size * 2);
        PS := PAnsiChar(Result);
        pos := 0; // position into the result string
        len := FS.Read(Block, SizeOf(Block));
        while len <> 0 do
        begin
          for i:=0 to len-1 do
          begin
            B := Block[i];
            PS[pos] := Nibbs[B div $F];
            Inc(pos);
            PS[pos] := Nibbs[B mod $F];
            Inc(pos);
          end;
          len := FS.Read(Block, SizeOf(Block));
        end;
      finally FS.Free;
      end;
    end;
    

    P.S: I’m using AnsiString, and PAnsiChar so the code works also works with Unicode Delphi. If you happen to be on Delphi 2010 find a way to use this in it’s current form (AnsiString) so you can skip the conversions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 378k
  • Answers 378k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Have a look at Fast CGI for Ror. You can… May 14, 2026 at 9:23 pm
  • Editorial Team
    Editorial Team added an answer Found a way to do a setBounds here: http://www.mymapofjapan.com/blog/setting-size-and-zoom-level-of-maps/ var… May 14, 2026 at 9:23 pm
  • Editorial Team
    Editorial Team added an answer I wrote a small lib called ip2c to do just… May 14, 2026 at 9:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.