🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: binary to hexa

#747
From: Clif Penn
Date: Thu, 08 Jul 1999 00:00
38 lines
2327 bytes
querier wrote in message <7lvm8r$u27$1@tempo.news.iphil.net>...
>hello all
>
>how can you convert binary number to hexadecimal using pascal 7
>
>
>rose


Depending on your needs, here is an unusual approach to converting char
string representation of binary numbers directly to char string
representation of hexadecimal numbers. If the hexstring is <= 7FFFFFFF it
can be evaluated as the largest base 10 integer. However the hex conversion
is correct for binary strings longer than this. Good homework problem.

Every hex digit is 4 bits (a nibble). Patch the input binary string to be
a multiple of 4 in length.
      While Length(bin) MOD 4 <> 0 do bin := '0' + bin;

Evaluate bin 4 bits at a time and use that 0 to 15 decimal value to extract
the correct hex char. The nibbles can be converted forward or backward as
long as the hex concatenation is correct.
HexChar := '0123456789ABCDEF';
nibVal := 11;
hex = hex + HexChar[nibVal + 1];  {assuming left to right conversion}
HexChar[12] = 'B' because of the leading '0'.

Assume the hex string = 7FFFFFFF  {31 '1's in a binary string}
Concatenate '$' in front
hex = '$' + hex;  giving $7FFFFFFF

VAL(hex, decVal, errCode) will evaluate this to the largest positive long
integer. Anything greater than this up to $80000000 would evaluate negative
so you screen out values < 0. If the binary string is too long for VAL,
the errCode <> 0 BUT the hex number representation will still be correct.

<clifpenn@aimail.net>  7/8/99
Technical Details
Message-ID: <A37A9EB945EF6B57.694312022C85BACB.73FF796A901ED2B9@lp.airnews.net>
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: 111659,3ac8802a9bf50bcc
X-Google-Attributes: gid111659,public
X-Google-Thread: 10a726,3ac8802a9bf50bcc
X-Google-Attributes: gid10a726,public
From: "Clif Penn" <clifpenn@airmail.net>
Subject: Re: binary to hexa
Date: 1999/07/08
Message-ID: <A37A9EB945EF6B57.694312022C85BACB.73FF796A901ED2B9@lp.airnews.net>
X-Deja-AN: 498839590
References: <7lvm8r$u27$1@tempo.news.iphil.net>
X-Orig-Message-ID: <7m355k$b7h@library1.airnews.net>
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
NNTP-Posting-Time: Thu Jul  8 16:28:20 1999
Organization: Airnews.net! at Internet America
NNTP-Proxy-Relay: library1.airnews.net
Abuse-Reports-To: abuse at airmail.net to report improper postings
Newsgroups: comp.lang.pascal.borland,fr.comp.lang.pascal