Aditional String Utils

General discussion on mikroPascal.
Author
Message
yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

Aditional String Utils

#1 Post by yo2lio » 14 Aug 2007 07:53

Hello,

Aditional string library :

Code: Select all

Available types :
  type IpHeader = array[4] of byte;
  type MacHeader = array[6] of byte;

- Procedure Byte2Hex(data_hex : byte; var hex : string[2]);
  //Convert Byte to Hex
  //Ex. Byte2Hex(255,data_out);
  
- Function Hex2Byte(var hex : string[2]) : byte;
  //Convert Hex string format (must be string[2]) into Byte
  //Ex. res_8 := Hex2Byte('AA');
  
- Function Str2Byte(var byte_in : string[3]) : byte;
  //Convert Byte string format (must be string[1] to string[3]) into Byte
  //Ex. res_8 := Str2Byte('6');
  //Ex. res_8 := Str2Byte('66');
  //Ex. res_8 := Str2Byte(' 66');
  //Ex. res_8 := Str2Byte('  6');
  
- Function Str2Word(var word_in : string[5]) : word;
  //Convert Word string format (must be string[1] to string[5]) into Word
  //Ex. res_16 := Str2Word('9');
  //Ex. res_16 := Str2Word(' 669');
  //Ex. res_16 := Str2Word('96');
  //Ex. res_16 := Str2Word('  669');
  
- Procedure Ip2Str(var user_ip_addr : IpHeader; var str_out : string[15]);
  //Convert ip ( array[4] of byte ) into string. Lenght of string is variable from 7 to 15.
  //Ex. Ip2Str(Ip_address,data_out);

- Procedure Mac2Str(var mac_address : MacHeader; var str_out : string[12]);
  //Convert mac ( array[6] of byte ) into string[12].
  //Ex. Mac2Str(Mac_address, data_out);
  
- Procedure Str2Ip(var str_in : string[15]; var user_ip_addr : IpHeader);
  //Convert string (lenght of string must be from 7 to 15 and must include 3 '.') into Ip format array[4] of byte
  //Ex. Str2Ip('192.168.1.155', Ip_address);
  
- Procedure Str2Mac(var str_in : string[12]; var mac_address : MacHeader);
  //Convert string (lenght of string must be 12) into Mac format array[6] of byte
  //Ex. Str2Mac('00AA80FF457F', mac_address);
You can download code and examples from : http://www.microelemente.ro/String_Util.zip

Library source is not available. Sorry !
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

Teo
Posts: 20
Joined: 01 Jun 2006 09:21
Location: Bucuresti

#2 Post by Teo » 14 Aug 2007 21:10

TNX!!! multumesc!!

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#3 Post by yo2lio » 27 Aug 2007 13:25

Library updated . Folowing functions and procedures are available :

mem_cpy
mem_cmp
mem_set
mem_chr
Str_Len
Str_Cut_Chr
Str_Cat
Str_Cpy
Str_Chr
Str_Replase_Chr
Byte2StrWithZeros
Byte2Str
Short2StrWithZeros
Short2Str
Word2StrWithZeros
Word2Str
Int2StrWithZeros
Int2Str
dWord2StrWithZeros
dWord2Str
Long2StrWithZeros
Long2Str
Byte2Hex
Hex2Byte
Str2Byte
Str2Word
Ip2Str
Mac2Str
Str2Ip
Str2Mac



This routines are faster and most important save FSR register and restore after used.
About dWord type , will be available in next release. Till then use Longint type.

You found descriptions in the program.
Code are available here : http://www.microelemente.ro/String_Util.zip
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#4 Post by yo2lio » 30 Sep 2007 20:29

Hi,

Library updated , added Float2Str.

- Procedure Float2Str(data_in : Real; var data_str : string[17]; digits : byte);
// Convert Real value to string, lenght of string is variable from 1 to 17.
// Ex. Float2Str(-116.12345, txt17, 0); // txt17 will be '-116'
// Ex. Float2Str(-116.12345, txt17, 3); // txt17 will be '-116.123'
// Ex. Float2Str(-116.12345, txt17, 5); // txt17 will be '-116.12345'

This function is smaller and faster . Float2Str(3.1, txt17, 5) // txt17 will be '3.10000' :wink:

Code are available here : http://www.microelemente.ro/String_Util.zip
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

rainer
Posts: 320
Joined: 07 Dec 2006 11:00
Location: Vienna, Austria
Contact:

#5 Post by rainer » 01 Oct 2007 08:59

Great!
What I wonder about is: Why is no function available which works in the manner of TurboPascal's "str()"-function, allowing to specify length and number of decimal places and getting so a already correctly sized string as result? (Would possibly cover all this particular converting functions.)
It often happens to me that I must use float-type or other much too big var types, because calculations need that in earlier steps of calculations. So the final conversion often is too big for the display mask. A formatting function/procedure would be excellent here.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#6 Post by yo2lio » 01 Oct 2007 09:03

Thanks for suggestion !
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#7 Post by yo2lio » 02 Oct 2007 14:35

Library updated. Added 5 new functions.
Folowing functions and procedures are available :

mem_cpy
mem_cmp
mem_set
mem_chr
Str_Len
Str_Cut_Chr
Str_Cat
Str_Cpy
Str_Chr
Str_Replase_Chr
Byte2StrWithZeros
Byte2Str
Short2StrWithZeros
Short2Str
Word2StrWithZeros
Word2Str
Int2StrWithZeros
Int2Str
dWord2StrWithZeros
dWord2Str
Long2StrWithZeros
Long2Str
Float2Str
Str2Byte
Str2Word
Str2dWord
Str2Short
Str2Int
Str2Long
Str2Float
Ip2Str
Mac2Str
Str2Ip
Str2Mac
Byte2Hex
Hex2Byte


You found description in program.

Code and examples are available here : http://www.microelemente.ro/String_Util.zip

If you have any suggestion , please tell me , because I want to close this chapter.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

FRM
Posts: 381
Joined: 20 May 2005 18:58
Location: UK

#8 Post by FRM » 02 Oct 2007 15:17

Impressive, and thankyou.
If you have any suggestion , please tell me , because I want to close this chapter.
:lol:

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#9 Post by janni » 03 Oct 2007 23:59

Impressive and extensive library, Florin :D . Could be optimized a bit, but I liked the dWord to string algorithm. Saving and restorig FSRs is a bit of an overkill (especially when not all of them are used in a routine), but you've written the lib for your applications, so it probably makes sense to you.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#10 Post by yo2lio » 04 Oct 2007 05:50

Thanks Janni,

I don't know why , but I feel better when after this code :

Code: Select all

var data,res : byte;

begin
  data := 100;
  FSR0Ptr := @data;
  FSR1Ptr := @data;
  FSR2Ptr := @data;

...function or procedure here .... 

  res := INDF0 and INDF1 and INDF2;
end.
my data value equal with res value. (FSR registers remain intact)

Two more functions will be added, BcdToDec and DecToBcd , thanks to Rotary_Ed.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#11 Post by janni » 04 Oct 2007 12:09

yo2lio wrote:Thanks Janni,

I don't know why , but I feel better when ... (FSR registers remain intact)
Yes, well, this will work only as long as one doesn't use data structures, pointers or libraries other than yours.

javor.pas
Posts: 98
Joined: 16 Mar 2007 11:25
Contact:

#12 Post by javor.pas » 29 Oct 2007 07:44

Hallo yo2lio.

I've tested this library, and especially the 'dword2strwithzeroes' and 'longint2strwithzeroes' functions. Neither of them worked properly :(
When first convertion occur - they both give correct result. But on second and so on convertions - the result is wrong. Tested the same code but with mE's LongIntToStr function and everything works correct. My test was on 18F4620. Any clue why this doesn't work? Or any update of the lib? Or shall I use the conv2 library the you both with janni wrote? Is there any look ahead to implement such functions (xxxWithZeroes) ?

Thanks in advance.

Regards,

Javor

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#13 Post by yo2lio » 29 Oct 2007 08:50

Hi,

I can't reproduce the error, at me works, please sent to my mail florin@microelemente.ro a piece of code with this error.

BTW , what version of compiler you have ?

Thanks.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

javor.pas
Posts: 98
Joined: 16 Mar 2007 11:25
Contact:

#14 Post by javor.pas » 29 Oct 2007 14:23

Sent. Please take a look @ it - the program is simple, but I'm confused - why mE's function works fine and the other not?

javor.pas
Posts: 98
Joined: 16 Mar 2007 11:25
Contact:

#15 Post by javor.pas » 29 Oct 2007 14:55

Dang :!:
I found the error but the only reason (for me at least) for it is that functions work with pointers... Clearing the variable right after the convertion but just before outputting it to the UART is the reason... but it's not that much clear to me why is this error happening... :oops:

Post Reply

Return to “mikroPascal General”