Posts Tagged Conversion
Conventions, Or Should I Say Conversions
Often you have to convert objects from one type to another e.g. from Integer to Text, from Boolean to Text, etc. Up to version 4.0 there was a codeunit, Codeunit 6201 – Conventions, that had a bunch of functions to convert objects from one type to another. This codeunit disappeared from version 5.0 but many of the functions are still useful, if not to use them than at least to give ideas for other functions or, as they did for me, to start learnig some C/AL code.
So have a look at the code or download the attached codeunit Conventions of which I never understood where the name came from.
CodeToText(VarCode : Code[250];MaxLength : Integer) : Text[250]
IF Setup.RECORDLEVELLOCKING THEN
EXIT(VarCode);
EXIT(LeftPadCode(VarCode,MaxLength,’ ‘));
OptionToText(VarOption : Option) : Text[250]
EXIT(FORMAT(VarOption,0,2));
IntegerToText(VarInteger : Integer) : Text[250]
EXIT(FORMAT(VarInteger,0,2));
SQLDecimalToText(VarDecimal : Decimal) ReturnValue : Text[260]
DecimalSepSymbol := ‘.’;
ReturnValue :=
FORMAT(VarDecimal,0,’<Sign><Integer><Decimals><Comma,’ + DecimalSepSymbol + ‘>’);
CSDecimalToText(VarDecimal : Decimal) ReturnValue : Text[260]
WITH CommercePortalSetup DO BEGIN
GET;
CASE “Decimal Symbol” OF
“Decimal Symbol”::”0″:
DecimalSepSymbol := ‘.’;
“Decimal Symbol”::”1″:
DecimalSepSymbol := ‘,’;
END;
END;
ReturnValue :=
FORMAT(VarDecimal,0,’<Sign><Integer><Decimals><Comma,’ + DecimalSepSymbol + ‘>’);
BooleanToText(VarBoolean : Boolean) : Text[1]
EXIT(FORMAT(VarBoolean,1,2));
DateToText(VarDate : Date) : Text[8]
IF VarDate = 0D THEN
VarText := ‘17530101′
ELSE
VarText := FORMAT(VarDate,0,’<Year4><Month,2><Day,2>’);
EXIT(VarText);
TimeToText(VarTime : Time) ReturnTime : Text[16]
ReturnTime := FORMAT(VarTime,0,’<Hours24,2>:<Minutes,2>:<Seconds,2>’);
IF ReturnTime = ” THEN
ReturnTime := ‘00:00:00′;
TextToInteger(VarText : Text[250]) : Integer
EVALUATE(VarInteger,VarText);
EXIT(VarInteger);
TextToDecimal(VarText : Text[250]) : Decimal
BaseVarDecimal := 1.2;
DecimalSymbol := COPYSTR(FORMAT(BaseVarDecimal),2,1);
EVALUATE(VarDecimal,CONVERTSTR(VarText,’.',DecimalSymbol));
EXIT(VarDecimal);
TextToBoolean(VarText : Text[1]) : Boolean
IF NOT EVALUATE(VarBoolean,VarText) THEN
VarBoolean := FALSE;
EXIT(VarBoolean);
TextToDate(VarText : Text[8]) : Date
Day := TextToInteger(COPYSTR(VarText,7,2));
Month := TextToInteger(COPYSTR(VarText,5,2));
Year := TextToInteger(COPYSTR(VarText,1,4));
EXIT(DMY2DATE(Day,Month,Year));
TextToTime(VarText : Text[8]) : Time
BaseVarTime := 111111T;
TimeSeparator := COPYSTR(FORMAT(BaseVarTime),3,1);
EVALUATE(VarTime,CONVERTSTR(VarText,’:',TimeSeparator));
EXIT(VarTime);
SQLEncodeText(NonEncodedText : Text[250]) : Text[250]
EncodedText := ”;
FOR i := 1 TO STRLEN(NonEncodedText) DO BEGIN
EncodedText[STRLEN(EncodedText) + 1] := NonEncodedText[i];
IF NonEncodedText[i] = ”” THEN
EncodedText := EncodedText + ””;
END;
EXIT(”” + EncodedText + ””)
CSEncodeText(NonEncodedText : Text[250]) : Text[250]
EXIT(NonEncodedText);
SQLEncodeCode(VarCode : Code[250];Length : Integer) : Text[250]
EXIT(SQLEncodeText(CodeToText(VarCode,Length)));
CSEncodeCode(VarCode : Code[250]) : Text[250]
EXIT(VarCode);
SQLEncodeOption(VarOption : Option) : Text[250]
EXIT(SQLEncodeText(OptionToText(VarOption)));
CSEncodeOption(VarOption : Option) : Text[250]
EXIT(CSEncodeText(OptionToText(VarOption)));
SQLEncodeInteger(VarInteger : Integer) : Text[250]
EXIT(SQLEncodeText(IntegerToText(VarInteger)));
CSEncodeInteger(VarInteger : Integer) : Text[250]
EXIT(CSEncodeText(IntegerToText(VarInteger)));
SQLEncodeDecimal(VarDecimal : Decimal) : Text[250]
EXIT(SQLEncodeText(SQLDecimalToText(VarDecimal)));
CSEncodeDecimal(VarDecimal : Decimal) : Text[250]
EXIT(CSDecimalToText(VarDecimal));
SQLEncodeBoolean(VarBoolean : Boolean) : Text[5]
EXIT(SQLEncodeText(BooleanToText(VarBoolean)));
CSEncodeBoolean(VarBoolean : Boolean) : Text[5]
EXIT(CSEncodeText(BooleanToText(VarBoolean)));
SQLEncodeDate(VarDate : Date) : Text[12]
EXIT(SQLEncodeText(DateToText(VarDate)));
CSEncodeDate(VarDate : Date) : Text[12]
EXIT(CSEncodeText(DateToText(VarDate)));
SQLEncodeTime(VarTime : Time) : Text[14]
EXIT(SQLEncodeText(TimeToText(VarTime)));
SQLEncodeDateTime(VarDate : Date;VarTime : Time) : Text[19]
EXIT(SQLEncodeText(DateToText(VarDate) + ‘ ‘ + TimeToText(VarTime)));
LeftPadCode(VarCode : Code[250];MaxLength : Integer;FillChar : Text[1]) : Text[250]
Length := MaxLength – STRLEN(VarCode);
IF Length <= 0 THEN
EXIT(VarCode);
EXIT(PADSTR(”,Length,FillChar) + VarCode);
PutSynchMessageValue(VAR SynchMessageQueue : Record Table6224;No : Integer;Name : Text[190];Value : Text[250])
SynchMessageQueue.”No.” := No;
SynchMessageQueue.Name := Name;
SynchMessageQueue.Value := Value;
SynchMessageQueue.INSERT;
HEXEncode(Char : Char) : Text[3]
AsciiValue := Char;
IF AsciiValue > 255 THEN
ERROR(Text000,Char);
REPEAT
a := AsciiValue DIV 16;
b := AsciiValue – a * 16;
AsciiValue := a;
HexValue := COPYSTR(‘0123456789ABCDEF’,b + 1,1) + HexValue;
UNTIL AsciiValue = 0;
IF STRLEN(HexValue) < 2 THEN
HexValue := ‘0′ + HexValue;
EXIT(‘-’ + HexValue);
LinkEncode(WebSiteCode : Code[20];LinkNo : Code[20];TemplateNo : Code[20]) : Text[250]
IF NOT PagePropColl.GET(WebSiteCode,LinkNo,1) THEN
EXIT;
IF (TemplateNo <> ”) AND
(PagePropColl.”Template Prop. Coll. No.” <> 0)
THEN
IF TemplPropColl.GET(
WebSiteCode,PagePropColl.”Template No.”,
PagePropColl.”Template Prop. Coll. No.”)
THEN
IF TemplPropColl.”ASP FileName” <> ” THEN
EXIT(UPPERCASE(TemplPropColl.”ASP FileName”) + ‘,’ + LinkNo);
RemoveBackslashIfThere(VAR Path : Text[256])
IF Path <> ” THEN BEGIN
IF Path[STRLEN(Path)] = ‘\’ THEN
Path := COPYSTR(Path,1,STRLEN(Path) – 1);
END;
MakeFilePath(FileFolder : Text[256];FileName : Text[256]) : Text[250]
IF FileName = ” THEN
ERROR(Text001);
IF FileFolder = ” THEN
EXIT(FileName);
EXIT(FileFolder + ‘\’ + FileName);
ReportNonValidValue(Name : Text[150];Value : Text[256])
IF Value = ” THEN
ERROR(Text002,Name)
ELSE
ERROR(Text003,Value,Name);
CheckForInvCharAndMakeDescript(FieldName : Text[250];FieldValue : Text[250];VAR Description : Text[250])
FOR i := 1 TO STRLEN(FieldValue) DO
IF FieldValue[i] IN ['.',',',' '] THEN
ERROR(Text004,FieldName);
IF FieldValue = ” THEN
Description := ”
ELSE
Description := UPPERCASE(COPYSTR(FieldValue,1,1)) + LOWERCASE(COPYSTR(FieldValue,2));
Download the Codeunit by clicking the link below and renaming the file to .zip:Codeunit 6201, rename from .doc to .zip.
Add comment October 20, 2008