Author Topic: The best name for procedure that convert binary array to hex string  (Read 438 times)

LemonParty

  • Hero Member
  • *****
  • Posts: 654
I have a procedure that converts byte by byte binary array to hex string. For example:
Code: Pascal  [Select][+][-]
  1. const
  2.   Arr: array [0..3] of Byte = (0, 1, 14, 15);
  3. var
  4.   S: String;
  5. begin
  6.   SetLength(S, SizeOf(Arr) * 2);
  7.   BinToHex(Arr[0], 4, PAnsiChar(@S[1])^);
  8.   {S now contain 00010E0F}
  9. end;
Header of my procedure look like this:
Code: Pascal  [Select][+][-]
  1. procedure BinToHex(constref Binary: Byte; Count: SizeUInt; var Hex: AnsiChar);
The question is: What name fit the best to this procedure?

Two variants are:
1. BinToHex;
2. HexStr; (dublicate the name of already existed in System functions)
Maybe you have you own variant of the name.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thausand

  • Hero Member
  • *****
  • Posts: 613
Re: The best name for procedure that convert binary array to hex string
« Reply #1 on: August 11, 2026, 06:47:28 pm »
1. BinToHex;
Bin is make imply binary. binary is zero and one. May be you is mean BytesToHex ?

Quote
2. HexStr; (dublicate the name of already existed in System functions)
Can make overload and make use HexStr (may be is more better HexStrBytes or HexStrArr)

Quote
Maybe you have you own variant of the name.
I name HexStr

Code: Pascal  [Select][+][-]
  1. function HexStr(Bytes:TByteDynArray):string;overload;
  2. var
  3.   b:byte;
  4. begin
  5.   result:='';
  6.   for b in bytes
  7.     do result+={system.}HexStr(b,2);
  8. end;
  9.  
  10. var
  11.   Arr:array [0..5] of Byte=(0,1,14,15,4,3);
  12. begin
  13.   writeln('$',HexStr(Arr));
  14. end.
  15.  

because is same system HexStr that is return string.
A docile goblin always follow HERMES.md

J-G

  • Hero Member
  • *****
  • Posts: 1287
Re: The best name for procedure that convert binary array to hex string
« Reply #2 on: August 11, 2026, 07:08:37 pm »
This may well be 'Left Field'  but I use 'SetHEX'.

I'm currently using such to convert Integer to Hex.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Thausand

  • Hero Member
  • *****
  • Posts: 613
Re: The best name for procedure that convert binary array to hex string
« Reply #3 on: August 11, 2026, 07:15:12 pm »
@J-G,
I not know this you want for learn how to but there is exist helper that can do convert for you: https://www.freepascal.org/docs-html/rtl/sysutils/tintegerhelper.tohexstring.html
A docile goblin always follow HERMES.md

J-G

  • Hero Member
  • *****
  • Posts: 1287
Re: The best name for procedure that convert binary array to hex string
« Reply #4 on: August 11, 2026, 07:49:19 pm »
@J-G,
I not know this you want for learn how to but there is exist helper that can do convert for you: https://www.freepascal.org/docs-html/rtl/sysutils/tintegerhelper.tohexstring.html
Like most of my projects, this is a re-work of an old one that was (is?) an excercise to improve my knowledge of how 'numbers' work;  so using a 'black-box' that I feed a number to doesn't help me understand the internal working.

It's not just Int to Hex, it takes Integer, Hex or Binary input and returns INT, HEX, BIN & OCT -  and now UNICODE character in any Font & size available.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

ASerge

  • Hero Member
  • *****
  • Posts: 2518
Re: The best name for procedure that convert binary array to hex string
« Reply #5 on: August 12, 2026, 03:56:03 pm »
Two variants are:
1. BinToHex;
2. HexStr; (dublicate the name of already existed in System functions)
Maybe you have you own variant of the name.
I use the Lazarus IDE, so there are no issues with entering long names (auto‑completion). Suggest BinaryArrayToHexString.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 769
Re: The best name for procedure that convert binary array to hex string
« Reply #6 on: August 12, 2026, 05:13:51 pm »
Naming functions and procedures is an art form.  Has anyone tried asking something like ChatGPT to suggests a name in cases like this?

LemonParty

  • Hero Member
  • *****
  • Posts: 654
Re: The best name for procedure that convert binary array to hex string
« Reply #7 on: August 12, 2026, 07:02:53 pm »
Curt Carpenter, I building library for humans not AI. So I interesting in their opinion.
Thank you for answers.
I will left HexStr for now, but maybe change to ToHex later.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018