# Board index » delphi » Re: Binary Type

## Re: Binary Type

2005-06-02 02:51:56 PM
delphi32
##### Quote
Hex type is something like \$FF, what about Binary type?
You can not type binary in Delphi programs, but luckily binary is
converted to hexadecimal very easily; you can directly translate
sequences of 4 bits:
0000 =>0
0001 =>1
0010 =>2
0011 =>3
...
1110 =>E
1111 =>F
If you have a number with only one bit set you can also write it like
"1 shl n", where n is the index of the bit, counting from the right and
starting at 0.

## Re: Binary Type

Hi,
Hex type is something like \$FF, what about Binary type?
Thanks
Steve

## Re: Binary Type

Hi,
Hex type is something like \$FF, what about Binary type?
Thanks
Steve

## Re: Binary Type

Steve writes:
##### Quote
Hex type is something like \$FF, what about Binary type?
Is that a graphics issue ?
No types. You have to see it in this way:
If the value of a variable is 42 then it will be digitalized for the
computer in bits.
Decimal representation: 42
Binary representation : 00101010
Hex representation : 2A
Hans.

## Re: Binary Type

Yeah, I know that, but I thought there might be a way to represent binary
data
"Hans Galema" <XXXX@XXXXX.COM>writes
##### Quote
Steve writes:

>Hex type is something like \$FF, what about Binary type?

Is that a graphics issue ?

No types. You have to see it in this way:
If the value of a variable is 42 then it will be digitalized for the
computer in bits.

Decimal representation: 42
Binary representation : 00101010
Hex representation : 2A

Hans.

## Re: Binary Type

Yeah, I know that, but I thought there might be a way to represent binary
data directly.
message news:429eac8c\$XXXX@XXXXX.COM...
##### Quote
>Hex type is something like \$FF, what about Binary type?

You can not type binary in Delphi programs, but luckily binary is
converted to hexadecimal very easily; you can directly translate
sequences of 4 bits:

0000 =>0
0001 =>1
0010 =>2
0011 =>3
...
1110 =>E
1111 =>F

If you have a number with only one bit set you can also write it like
"1 shl n", where n is the index of the bit, counting from the right and
starting at 0.

## Re: Binary Type

"Steve" <XXXX@XXXXX.COM>schrieb im Newsbeitrag
##### Quote
Hi,

Hex type is something like \$FF, what about Binary type?

Thanks
Steve
I'm afraid you need to code your binary constant as string
and wherever you want to use it, you need to convert the string
In this manner I created a strtol() function similar to the C library.
program Bases;
{\$ifdef Win32}
{\$AppType Console}
const MaxLongestInt = \$7FFFFFFFFFFFFFFF;
type LongestInt = int64;
{\$else}
uses WinCRT;
const MaxLongestInt = \$7FFFFFFF;
type LongestInt = LongInt;
{\$endif}
var Number : LongestInt;
ErrorPos,
Base : Integer;
s : String;
function strtol(s : String; var ErrorPos : Integer; Base : Integer) :
LongestInt;
var l : LongestInt;
c : char;
begin
l := 0;
ErrorPos := 0;
c := #0;
while s>'' do
begin
inc(ErrorPos);
c := upcase(s);
s := Copy(s, 2, length(s));
if ('0' <= c) and (c <= '9') and (c < chr(Base + ord('0'))) then
begin
l := l * Base + ord(c) - ord('0');
c := #0;
end
else
if ('A' <= c) and (c < chr(Base - 10 + ord('A'))) then
begin
l := l * Base + ord(c) - ord('A') + 10;
c := #0;
end
else
s := '';
end;
if c = #0 then
begin
strtol := l;
ErrorPos := 0;
end
else
strtol := 0;
end;
function ltoa(Number : LongestInt; Base : Integer) : String;
var s : String;
Digit : Integer;
begin
s := '';
if Number < 0 then
begin
{
positive Number = \$100... + Number;
\$100... = \$80... + \$40... + \$40...;
Number and \$7F... = Number + \$80... avoiding overflow;
\$40... = MaxLongestInt shr 1 + 1;
}
Number := Number and MaxLongestInt;
Digit := Number mod Base;
Number := Number div Base;
Number := Number + (MaxLongestInt shr 1 + 1 + Digit) div Base;
Digit := (MaxLongestInt shr 1 + 1 + Digit) mod Base;
Number := Number + (MaxLongestInt shr 1 + 1 + Digit) div Base;
Digit := (MaxLongestInt shr 1 + 1 + Digit) mod Base;
if Digit < 10 then
s := chr(Digit + ord('0')) + s
else
s := chr(Digit - 10 + ord('a')) + s;
end;
while Number <>0 do
begin
Digit := Number mod Base;
Number := Number div Base;
if Digit < 10 then
s := chr(Digit + ord('0')) + s
else
s := chr(Digit - 10 + ord('a')) + s;
end;
ltoa := s;
end;
begin
repeat
write('Number? ');
write('Base? ');
if Base>= 2 then
begin
writeln(s, ' Base ', Base, ' is ', strtol(s, ErrorPos, Base), ' Base
10');
writeln('ErrorPos is ', ErrorPos);
writeln;
end;
until Base < 2;
writeln;
repeat
write('Number Base 10? ');
write('Base? ');
if Base>= 2 then
begin
writeln(Number, ' Base 10 is ', ltoa(Number, Base), ' Base ', Base);
writeln;
end;
until Base < 2;
end.

## Re: Binary Type

Steve writes:
##### Quote
Yeah, I know that, but I thought there might be a way to represent binary
data
Actually all data is stored binary (bits). So then the choice
is how to represent it. That can be binary (with 0 and 1) or
decimal or octal or hexadecimal or what you want.
It is unclear to me what you are looking for. Please give
an ezample.
Hans.

## Re: Binary Type

"Hans Galema" <XXXX@XXXXX.COM>writes
##### Quote
Steve writes:
>Yeah, I know that, but I thought there might be a way to represent
binary
>data

Actually all data is stored binary (bits). So then the choice
is how to represent it. That can be binary (with 0 and 1) or
decimal or octal or hexadecimal or what you want.

It is unclear to me what you are looking for. Please give
an ezample.

Hans.
question is how do you type 101 and have it interpreted as 5 and not one
hundred and one.
Simon.

## Re: Binary Type

Simon D writes:
##### Quote
To indicate that a string like "01234" has to be interpreted
##### Quote
question is how do you type 101 and have it interpreted as 5 and not one
hundred and one.
Where do you type it ?
There is as far as I know no prefix which tells that a string
like "010101" would contain binary digits only. You have to
explicitly tell that the converting function.
See for example strtol() and strtoul().
Hans.

## Re: Binary Type

"Hans Galema" <XXXX@XXXXX.COM>writes
##### Quote
Simon D writes:

To indicate that a string like "01234" has to be interpreted

>question is how do you type 101 and have it interpreted as 5 and not one
>hundred and one.

Where do you type it ?
In your code I am assuming.
##### Quote
There is as far as I know no prefix which tells that a string
like "010101" would contain binary digits only.
Nor me.
##### Quote
You have to
explicitly tell that the converting function.

See for example strtol() and strtoul().
However that would be a run time conversion rather than a compile time
representation.
Simon.

## Re: Binary Type

Simon D writes:
##### Quote
In your code I am assuming.
However that would be a run time conversion rather than a compile time
representation.
You could have said in your first post that it was for the compiler.
Hans.

## Re: Binary Type

"Hans Galema" <XXXX@XXXXX.COM>writes
##### Quote
Simon D writes:

>In your code I am assuming.

>However that would be a run time conversion rather than a compile time
>representation.

You could have said in your first post that it was for the compiler.

Hans.
Sorry, I thought that much was clear from the original question. (That's
assuming I am correct of course).
Simon.