🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.pascal.borland
Article #746

Re: binary to hexa

#746
From: Mike Copeland
Date: Wed, 07 Jul 1999 00:00
80 lines
3116 bytes
> how can you convert binary number to hexadecimal using pascal 7

   Here's something posted by another contributor here (Frank Peelo) 
which should help:

Subject: Re: Integer to Hex notation.
From: "Frank Peelo" <fpeelo@indigo.ie>
Newsgroups: comp.lang.pascal.borland

>How does one convert intergers to hex notation.

First a small function that does hex to any desired length, assuming
the hex will fit in that length, padding with leading zeros. Specify 0
as length if you want no padding.

-------------------------- Cut here --------------------------
Function Hex(i:LongInt; Lgt:Byte):String;
  { Return i in hex with at least Lgt chars.
    Lgt is byte so I don't have to check for values > 255. If another
    type was used, and a value > 255 was passed in, the routine would
    loop indefinitely.
  }
Const
  HexDigs:Array[0..15] of Char = '0123456789ABCDEF';
Var
  s : String;
Begin
  s := '';
  Repeat
    s := HexDigs[i and $F] + s;
    i := i shr 4;
  until (i = 0) and (length(s) >= Lgt);
  Hex := s;
End;
-------------------------- Cut here --------------------------

Here's a generalised version of the above, that will handle any base
from 2 to 36.

-------------------------- Cut here --------------------------
Function Base(b:integer; i:LongInt; Lgt:byte):String;
  { Version of Hex generalised for other bases.
    b should be in [2..36]. Unless the base is to be entered by the
    user or read from a file, a bad value should never be passed in
    so I have no error handling for bad values of b.
  }
Const
  CsOffset = ord('A')-ord('9')-1;
    { '0'..'9' must be consecutive, as must 'A'..'Z' but there is
      usually an offset from '9' to 'A'
    }
Var   s : String;
  digit : Integer;
Begin
  { If you have an Assert system
    Assert((b>=2) and (b<=36));
  }
  s := '';
  Repeat
    digit := i mod b;
    If digit>9 then
      inc(digit, CsOffset);
    s := Char(digit+ord('0')) + s;
    i := i div b;
  until (i = 0) and (length(s) >= Lgt);
  Base := s;
End;
-------------------------- Cut here --------------------------

Examples:
  Hex(123,0) returns '7B' as does Base(16,123,0)
  Hex(123,4) returns '007B' as does Base(16,123,4)
  Base(2,123,0) returns '1111011' but Base(2,123,8) returns '01111011'
which is probably more readable.
  Base(25,123,0) returns '4N' - for bases above 10, the letters
'A'..'Z' are used.
  Base(10,123,0) returns '123' but sure you'd nearly guess that
anyway.

FP
Technical Details
Message-ID: <MPG.11ed8dfa63187dfe989c49@news.primenet.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <7lvm8r$u27$1@tempo.news.iphil.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10a726,3ac8802a9bf50bcc
X-Google-Attributes: gid10a726,public
X-Google-Thread: 111659,3ac8802a9bf50bcc
X-Google-Attributes: gid111659,public
From: mrcope@primenet.com (Mike Copeland)
Subject: Re: binary to hexa
Date: 1999/07/07
Message-ID: <MPG.11ed8dfa63187dfe989c49@news.primenet.com>
X-Deja-AN: 498339107
References: <7lvm8r$u27$1@tempo.news.iphil.net>
X-Complaints-To: abuse@globalcenter.net
Organization: Copeland Computer Services
X-Posted-By: @207.218.5.168 (mrcope)
Newsgroups: comp.lang.pascal.borland,fr.comp.lang.pascal