String to BCD (embarcadero delphi) - delphi

Edit:
I have (test file in ascii) the following record in ascii: "000000000.00"
I need to output it ISO upon parsing it's counter part in BCD (the other test file in bcd/ebcdic). I believe it takes 6 char in BCD and 11 in ascii.
So my need was something that could convert it back and forth.
First I thought of taking each chars, feed it to a convert function and convert it back hence my messed up question.
I hope i'm more clear.
Yain

Dr. Peter Below (of Team B) donated these in the old Borland Delphi newsgroups a few years ago:
// NO NEGATIVE NUMBERS either direction.
// BCD to Integer
function BCDToInteger(Value: Integer): Integer;
begin
Result := (Value and $F);
Result := Result + (((Value shr 4) and $F) * 10);
Result := Result + (((Value shr 8) and $F) * 100);
Result := Result + (((Value shr 16) and $F) * 1000);
end;
// Integer to BCD
function IntegerToBCD(Value: Integer): Integer;
begin
Result := Value div 1000 mod 10;
Result := (Result shl 4) or Value div 100 mod 10;
Result := (Result shl 4) or Value div 10 mod 10;
Result := (Result shl 4) or Value mod 10;
end;

As you may know, the ASCII codes of the numerals 0 through 9 are 48 through 57. Thus, if you convert each character in turn to its ASCII equivalent and subtract 48, you get its numerical value. Then you multiply by ten, and add the next number. In pseudo code (sorry, not a delphi guy):
def bcdToInt( string ):
val = 0
for each ch in string:
val = 10 * val + ascii(ch) - 48;
return val;
If your "string" in fact contains "true BCD values" (that is, numbers from 0 to 9, rather than their ASCII equivalent 48 to 57), then don't subtract the 48 in the above code. Finally, if two BCD values are tucked into a single byte, you would access successive members with a bitwise AND with 0x0F (15). But in that case, Ken White's solution is clearly more helpful. I hope this is enough to get you going.

functions below work for 8 digit hexadecimal and BCD values.
function BCDToInteger(Value: DWORD): Integer;
const Multipliers:array[1..8] of Integer=(1, 10, 100, 1000, 10000, 100000, 1000000, 10000000);
var j:Integer;
begin
Result:=0;
for j:=1 to 8 do //8 digits
Result:=Result+(((Value shr ((j-1)*4)) and $0F) * Multipliers[j]);
end;//BCDToInteger
function IntegerToBCD(Value: DWORD): Integer;
const Dividers:array[1..8] of Integer=(1, 10, 100, 1000, 10000, 100000, 1000000, 10000000);
var j:Integer;
begin
Result:=0;
for j:=8 downto 1 do //8 digits
Result:=(Result shl 4) or ((Value div Dividers[j]) mod 10);
end;//IntegerToBCD

Related

How to print code128C?

I am trying to print a code128C (numbers only) but I believe that the way of sending the data is incorrect ... at the time of reading the code the conversion does not result in the data initially informed.
In code128A I submit an ASCCI code, the printer converts to hex and print...the reader convert it back to ASCII.
In code128C if I submit an ASCCI, at the time of reading the reader converts to decimal, which does not result in the initial value.
EX:
128A Input: '1' Printer: 31 Reading: 1
128C Input: '1' Printer: 31 Reading: 49
I imagine that I should submit the input code already in integer .... but as the command is composed of other information I do not know how to send it in integer.
This is the code of code128A:
ComandoAnsiString := tp.cod128A('12'); //Data entry
function TTP650.cod128A(cod: AnsiString): AnsiString;
begin
// Fill out the CODE 128 printing protocol
Result := #29+#107+#73 + chr(length(cod)+2) + #123+#65 + cod;
end;
WritePrinter( HandleImp, PAnsiChar(ComandoAnsiString), Length(ComandoAnsiString),
CaracteresImpressos); //send to printer
This is the code I've been trying with code128C:
ComandoAnsiString := tp.cod128C('12');
function TTP650.cod128C(cod: AnsiString): AnsiString;
begin
Result := #29+#107+#73 + chr(length(cod)+2) + #123+#67 + cod;
end;
WritePrinter( HandleImp, PAnsiChar(ComandoAnsiString), Length(ComandoAnsiString),
CaracteresImpressos);
I'm dealing with a thermal printer and one codebar reader simple, default.
The sending codes(WritePrinter) are from the library WinSpool ... the rest are codes written by me.
Important code information is on pages 47 to 50 of the guide.
Guide
Assuming users will enter the wanted barcodes as a string of digits which may be stored somewhere as string and at the time of printing, passed to the printing function as human readable string.
The printing function will then convert to an array of bytes, packing the digits according to CODE C (each pair of two decimal digits, forming a value 00..99, stored in a byte). Iow, if the entry string of digits is e.g. '123456', then this is represented by three bytes with values 12, 34, 56.
function cod128C(const cod: string): TBytes;
const
GS = 29; // GS - Print bar code
k = 107; // k - -"-
m = 73; // m - CODE128
CS = 123; // { - select code set //}
CC = 67; // C - CODE C
var
i, len, n, x: integer;
s: string;
begin
len := Length(cod);
if len = 0 then exit;
// raise for odd number of digits in cod, ...
// if Odd(len) then
// raise Exception.Create('cod must have even number of digits');
s := cod;
// ... alternatively assume a preceeding zero digit before the first digit
// in cod
if Odd(len) then
begin
s := '0'+s;
inc(len);
end;
len := len div 2; // we pack 2 digits into one byte
SetLength(result, 6 + len);
result[0] := GS;
result[1] := k;
result[2] := m;
result[3] := 2 + len; // length of cod, + 2 for following code set selector
result[4] := CS;
result[5] := CC;
n := length(s);
i := 1; // index to S
x := 6; // index to result
while i < n do
begin
result[x] := StrToInt(MidStr(s, i, 2));
inc(i, 2);
inc(x, 1);
end;
end;
And with a form with a button, edit and memo you can test the function and send it to your printer with the following.
procedure TForm1.Button1Click(Sender: TObject);
var
cmnd: TBytes;
i: integer;
s: string;
begin
cmnd := cod128C(Edit1.Text);
for i := 0 to Length(cmnd)-1 do
s := s+IntToStr(cmnd[i])+', ';
Memo1.Lines.Add(s);
WritePrinter( HandleImp, #cmnd[0], Length(cmnd), CaracteresImpressos);
end;
You may want to add a check for only decimal digits in the input string, but I leave that to you.

`bitpacked` records on the little-endian machine issue

I'm trying to use FreePascal on little-endian machine to read and interpret data from integrated circuit. The data essentially consists tightly bitpacked (mostly) big-endian integer numbers, some of them (a lot, actually) are not aligned to byte boundary. So, I've tried to employ FPC's bitpacked records for that and found myself in the deep deep trouble.
The first structure I'm trying to read has the following format:
{$BITPACKING ON}
type
THeader = bitpacked record
Magic: Byte; // format id, 8 bits
_Type: $000..$FFF; // type specifier, 12 bits
Version: Word; // data revision, 16 bits
Flags: $0..$F // attributes, 4 bits
end;
And here is a reading code:
procedure TForm1.FormCreate(Sender: TObject);
var
F: File;
Header: THeader;
begin
Writeln(SizeOf(Header), #9, BitSizeOf(Header)); // reports correctly
Writeln('SizeOf(Header._Type) = ', SizeOf(Header._Type)); // correctly reports 2 bytes
Writeln('BitSizeOf(Header._Type) = ', BitSizeOf(Header._Type)); // correctly reports 12 bits
AssignFile(F, 'D:\3fd8.dat');
FileMode := fmOpenRead;
Reset(F, SizeOf(Byte));
BlockRead(F, Header, SizeOf(Header));
{ data is incorrect beyond this point already }
//Header._Type := BEtoN(Header._Type);
Writeln(IntToHex(Header.Magic, SizeOf(Header.Magic) * 2));
Writeln(IntToHex(BEtoN(Header._Type), SizeOf(Header._Type) * 2));
Writeln(BEtoN(Header.Version));
end;
But the code is printing totally wrong data.
Here is the data and the interpretation done manually:
0000000000: F1 55 BE 3F 0A ...
Magic = F1
_Type = 55B
Version = E3F0
Flags = A
But FPC sees the data in severely different and incorrect way. Looks like a nibbles (and bits) belonging to field are not contiguous due to little-endianess of host machine (eg: nibble B normally should belong to _Type field and nibble E - to Version). Here is a Watches window from Lazarus:
Please advice what I should do with such behaviour. Is this non-contiguous bitfield issue a bug of FPC? Any workarounds possible?
The bytes
F1 55 BE 3F 0A
have the following consecutive nibbles (lower nibble before higher nibble):
1 F 5 5 E B F 3 A 0
If you group these into 2, 3, 4 and 1 nibbles respectively, you get:
1 F --> $F1
5 5 E --> $E55 // highest nibble last, so E is highest.
B F 3 A --> $A3FB // same again: A is highest nibble
0 --> $0
which corresponds to the result you see in the Watch window, and not what you decoded manually.
Now, if the data is big-endian, then you'll have to decode manually using shifts and masking:
X.Magic := bytes[0];
X._Type := (bytes[1] shl 4) or (bytes[2] shr 4);
X.Version := ((bytes[2] and $0F) shl 12) or
(bytes[3] shl 4) or
(bytes[4] shr 4);
X.Flags := bytes[4] and $0F;
I use this function to convert from IEEE format to FPC single:
Type MyReal = Array [1..4] of Byte;
function IeeeToSingle (src:MyReal):Single;
var x:MyReal;
s:single absolute x;
man:word;
exp:word;
ca:single;
cb:cardinal absolute ca; // 4 byte unsigned long int
begin
x:=src;
ca:=s;
if cb>0 then begin // not zero
man := cb shr 16;
exp := (man and $ff00) - $0200;
if ((exp and $8000) <> (man and $8000)) then
MsToIeee := -1; // exponent overflow
man := (man and $7f) or ((man shl 8) and $8000);// move sign
man := man or (exp shr 1);
cb := (cb and $ffff) or (Cardinal(man) shl 16);
end;
IeeeToSingle := ca;
end;

Convert an Octal number to Decimal and Hexadecimal

I am writing a program that converts an Octal number to Decimal and Hexadecimal. I wrote a function called OctToInt.
function OctToInt(Value: string): Longint;
var
i: Integer;
int: Integer;
begin
int := 0;
for i := 0 to Length(Value) do
begin
int := int * 8 + StrToInt(Copy(Value, i, 1));
end;
Result := int;
end;
I call this function in this way:
var oct:integer;
begin
oct:=OctToInt(Edit13.Text);
Edit15.Text:=IntToStr(oct);
end;
When I type 34 (Octal) the decimal number should be 28 but the program gives me 220. Do you know why?
Also, do you have any idea about a converter OctToHex?
You have to change the start of "your" for with 1.
function OctToInt(Value: string): Longint;
var
i: Integer;
int: Integer;
begin
int := 0;
for i := 1 to Length(Value) do //here you need 1, not 0
begin
int := int * 8 + StrToInt(Copy(Value, i, 1));
end;
Result := int;
end;
The conversion Octal-Hexadecimal could be hard to do, so I suggest you another way:
EditHexadecimal.Text:=(IntToHex(StrToInt(EditInteger.Text),8));
As you can see here, with this code the EditHexadecimal is the Edit where you put the hexadecimal number. With that line I convert a number from decimal to hexadecimal.
You already have the decimal number because you get it with the function OctToInt, so you don't need more code.
This code accepts a string with a base-8 representation of an integer, and returns the corresponding integer:
function IntPower(const N, k: integer): integer;
var
i: Integer;
begin
result := 1;
for i := 1 to k do
result := result * N;
end;
function OctToInt(const Value: string): integer;
var
i: integer;
begin
result := 0;
for i := 1 to Length(Value) do
inc(result, StrToInt(Value[i]) * IntPower(8, Length(Value) - i));
end;
When it comes to converting an integer to a hexadecimal string representation, you already have IntToHex.
i made this formula so you can process octals in batches of 3-digits at a time - it's been tested from 000 through 777 to perfectly generate the decimal integer from octals :
if your octals are in a variable oct and temp placeholder o2
then
(37 < oct % 100)*8 + int(0.08*(oct-(o2=oct%10))+0.7017)*8 + o2
if you wanna further streamline that without the placeholder, then its
(37 < oct%100)*8 + int(0.7017+0.08*(oct-(oct%=10)))*8 + oct
the "0.7017" is possibly sin(-10*π/8) or 1/sqrt(2), or just 2^(1/-2), but since i found the formula via regression i'm not 100% sure on this.
Another fast trick is that if all 3 digits are the same number - 222 333 555 etc, simply take the first digit then multiply by 73 (cuz 73 in octal is 111). The sequence chain of multipliers for 2-6 consecutive matching digits are
9, 73, 585, 4681, 37449
(it also happens that within this list, for each x,
one of {x-2,x+0,x+2} is prime

Converting 20-digit decimal value to Hexadecimal using delphi code

I am trying to convert 20 digit decimal value to hexadecimal using Delphi code. Although I find the code below in C#.
BigInteger bi = new BigInteger("12345678901234567890");
string s = bi.ToHexString();
Can anyone help with equivalent delphi code to achieve this objective?
Please note that using kstools code, I was able to convert 17 digit hexadecimal value to 20 digit decimal but I cannot reverse it to get back the hexadecimal value.
The kstool code is as follows:
var
I: TksInteger;
....
I.FromString('$ABDCF123456789FE');
Result = I.AsString;
Here's a Delphi translation of a C# answer to an identical question:
program DecToHex;
{$APPTYPE CONSOLE}
uses
SysUtils, Generics.Collections;
function DecimalToHex(const Dec: string): string;
var
bytes: Generics.Collections.TList<Byte>;
i, digit, val: Integer;
b: Byte;
c: Char;
begin
bytes := Generics.Collections.TList<Byte>.Create;
try
bytes.Add(0);
for c in Dec do
begin
Assert(CharInSet(c, ['0'..'9']));
val := ord(c)-ord('0');
for i := 0 to bytes.Count-1 do
begin
digit := bytes[i]*10 + val;
bytes[i] := digit and $0F;
val := digit shr 4;
end;
if (val<>0) then
bytes.Add(val);
end;
Result := '';
for b in bytes do
Result := '0123456789ABCDEF'[b+1] + Result;
finally
bytes.Free;
end;
end;
const
test = '56493153725735501823';
begin
WriteLn(test + ' = $' + DecimalToHex(test));
end.
Output:
56493153725735501823 = $30FFFFFFFFFFFFFFF
What's the problem exactly?
Using the example that you've given, it just works here.
Proof
This code converts 12345678901234567890 to a string, and then back to a number.
program Project122; {$APPTYPE CONSOLE}
uses SysUtils;
const SomeBigNumber=12345678901234567890;
var S:String; SomeBigNumber2:UInt64;
begin
WriteLn(SomeBigNumber);
S := '$'+IntToHex(SomeBigNumber, 40);
Writeln('As Hex: ',S);
Writeln;
Writeln('Now let''s convert it back...');
SomeBigNumber2 := StrToInt64(S);
Writeln(SomeBigNumber2);
ReadLn;
end.
output:
12345678901234567890 As Hex:
$AB54A98CEB1F0AD2
Now let's convert it back...
12345678901234567890
If you want to convert any 20-digit number, this won't work, because the largest ones don't fit in a UINT64.
18446744073709551615 is the largest number that you can fit in a UIN64.
12345678901234567890
The same as David Heffernan translation but optimized and compatible with older Delphi versions...
function DecimalToHex(const Dec: AnsiString): AnsiString;
var
ResultArray: array of byte;
n, i: Integer;
val, digit: Byte;
c: AnsiChar;
begin
SetLength(ResultArray, Trunc(Length(Dec) * Ln(10) / Ln(16)) + 1);
n := 0;
for c in Dec do
begin
Assert(CharInSet(c, ['0'..'9']));
val := ord(c) - ord('0');
for i := 0 to n do
begin
digit := ResultArray[i] * 10 + val;
ResultArray[i] := digit and $0F;
val := digit shr 4;
end;
if val <> 0 then
begin
inc(n);
ResultArray[n] := val;
end;
end;
Result := '';
for digit in ResultArray do
Result := AnsiString('0123456789ABCDEF')[digit + 1] + Result;
end;
The following Delphi function converts a decimal number and returns a right-justified hexadecimal number. It is not elegant, but it works.
//Uses STRUTILS Delphi module
function IHEX(x:Double): string; //Returns hex number as right-justified string
var i,k,a,mx:Integer;
y,Z,a1:currency;
s:string;
hxs: array[0..15] of string;
const hx='0123456789ABCDEF';
begin
Y:=abs(X); //Make sure target decimal is not a negative number
mx:=0; //Count number of hex digits derived from conversion
repeat
z:= y / 16; // Divide the decimal number by base 16
a:=Trunc(z); // Get integer part of dividend
if z>=16 then begin //If integer dividend greater than 16
a1:=16 * Frac(z);//Get the dividend carry-over
k:=Trunc(a1); // Base 16 left-to-right placement digit
If k<=9 then hxs[mx]:=IntToStr(k) //if hex digit greater than 9,
else
hxs[mx]:=hx[k+1]; //get hex alpha digit
mx:=mx+1; //increment hex digit placement
y:=a; //Replace decimal with current dividend result
end;
if z<16 then begin //When dividend less than 16,
a1:=16 * Frac(z); //get dividend carry-over
k:=Trunc(a1); //Base 16 left-to-right placement digit
If k<=9 then hxs[mx]:=IntToStr(k) //If hex digit greater than 9,
else
hxs[mx]:=hx[k+1]; //get hex alpha digit
mx:=mx+1; //increment hex digit placement
y:=a; //Replace decimal with current dividend result
end;
until y<16; //When loop ends, last hex digit derived from division
If a<=9 then hxs[mx]:=IntToStr(a) //If last hex digit greater than 9,
else
hxs[mx]:=hx[a+1]; //get hex alpha digit
//Pull HEX digits from placement array in reverse order to create HEX number
s:='';
i:=mx;
repeat
s:=s + hxs[i];
i:=i-1;
until i<0;
s:=s+'H';
//Format HEX number as seven characters RIGHT-JUSTIFIED
repeat
k:=Length(s);
if k<7 then s:='0'+s;
until Length(s)>=7;
Result:=s;
end;

How to keep 2 decimal places in Delphi?

I have selected columns from a database table and want this data with two decimal places only. I have:
SQL.Strings = ('select '#9'my_index '#9'his_index,'...
What is that #9?
How can I deal with the data I selected to make it only keep two decimal places?
I am very new to Delphi.
#9 is the character with code 9, TAB.
If you want to convert a floating point value to a string with 2 decimal places you use one of the formatting functions, e.g. Format():
var
d: Double;
s: string;
...
d := Sqrt(2.0);
s := Format('%.2f', [d]);
function Round2(aValue:double):double;
begin
Round2:=Round(aValue*100)/100;
end;
#9 is the tab character.
If f is a floating-point variable, you can do FormatFloat('#.##', f) to obtain a string representation of f with no more than 2 decimals.
For N Places behind the seperator use
function round_n(f:double; n:nativeint):double;
var i,m : nativeint;
begin
m := 10;
for i := 1 to pred(n) do
m := m * 10;
f := f * m;
f := round(f);
result := f / m;
end;
For Float to Float (with 2 decimal places, say) rounding check this from documentation. Gives sufficient examples too. It uses banker's rounding.
x := RoundTo(1.235, -2); //gives 1.24
Note that there is a difference between simply truncating to two decimal places (like in Format()), rounding to integer, and rounding to float.
Nowadays the SysUtils unit contains the solution:
System.SysUtils.FloatToStrF( singleValue, 7, ffFixed, 2 );
System.SysUtils.FloatToStrF( doubleValue, 15, ffFixed, 2 );
You can pass +1 TFormatSettings parameter if the requiered decimal/thousand separator differ from the current system locale settings.
The internal float format routines only work with simple numbers > 1
You need to do something more complicated for a general purpose decimal place limiter that works correctly on both fixed point and values < 1 with scientific notation.
I use this routine
function TForm1.Flt2str(Avalue:double; ADigits:integer):string;
var v:double; p:integer; e:string;
begin
if abs(Avalue)<1 then
begin
result:=floatTostr(Avalue);
p:=pos('E',result);
if p>0 then
begin
e:=copy(result,p,length(result));
setlength(result,p-1);
v:=RoundTo(StrToFloat(result),-Adigits);
result:=FloatToStr(v)+e;
end else
result:=FloatToStr(RoundTo(Avalue,-Adigits));
end
else
result:=FloatToStr(RoundTo(Avalue,-Adigits));
end;
So, with digits=2, 1.2349 rounds to 1.23 and 1.2349E-17 rounds to 1.23E-17
This worked for me :
Function RoundingUserDefineDecaimalPart(FloatNum: Double; NoOfDecPart: integer): Double;
Var
ls_FloatNumber: String;
Begin
ls_FloatNumber := FloatToStr(FloatNum);
IF Pos('.', ls_FloatNumber) > 0 Then
Result := StrToFloat
(copy(ls_FloatNumber, 1, Pos('.', ls_FloatNumber) - 1) + '.' + copy
(ls_FloatNumber, Pos('.', ls_FloatNumber) + 1, NoOfDecPart))
Else
Result := FloatNum;
End;
Function RealFormat(FloatNum: Double): string;
Var
ls_FloatNumber: String;
Begin
ls_FloatNumber:=StringReplace(FloatToStr(FloatNum),',','.',[rfReplaceAll]);
IF Pos('.', ls_FloatNumber) > 0 Then
Result :=
(copy(ls_FloatNumber, 1, Pos('.', ls_FloatNumber) - 1) + '.' + copy
(ls_FloatNumber, Pos('.', ls_FloatNumber) + 1, 2))
Else
Result := FloatToStr(FloatNum);
End;

Resources