Recent

Author Topic: [SOLVED] convert all char(Unicode) to integer(or HEX) and inverse??  (Read 25220 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: convert char to integer and inverse??
« Reply #15 on: August 12, 2017, 04:45:17 pm »
Why do you need to convert unicode string to integer before saving it to a database? As far as I know, they all support unicode string.

Also my code already convert unicode string to TBytes. TBytes is basically an array of Byte. You should able to use it.

majid.ebru

  • Hero Member
  • *****
  • Posts: 505
Re: convert char to integer and inverse??
« Reply #16 on: August 12, 2017, 04:50:55 pm »
Why do you need to convert unicode string to integer before saving it to a database? As far as I know, they all support unicode string.

Also my code already convert unicode string to TBytes. TBytes is basically an array of Byte. You should able to use it.

i don't want anyone see my databank strnig ( like encrypt data)

your code only convert char(Unicode)  to byte.

how can i convert byte to char(Unicode)?

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: convert char to integer and inverse??
« Reply #17 on: August 12, 2017, 04:52:22 pm »
See line #55 on the code.

majid.ebru

  • Hero Member
  • *****
  • Posts: 505
Re: convert char to integer and inverse??
« Reply #18 on: August 12, 2017, 05:00:36 pm »
Thank you

so sorry I did not look carefully.

but last char doesn't convert .Why?

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: convert char to integer and inverse??
« Reply #19 on: August 12, 2017, 05:01:22 pm »
Which one, sample please?

majid.ebru

  • Hero Member
  • *****
  • Posts: 505
Re: convert char to integer and inverse??
« Reply #20 on: August 12, 2017, 05:09:07 pm »
NO
NO
NO

your code convert "خ" to :

HEX : 2E 06 
Dec : 46 6

every thing is OK

now

 how can i convert (HEX : 2E 06  ) or (Dec : 46 6) to  "خ"
« Last Edit: August 12, 2017, 05:15:26 pm by majid.ebru »

majid.ebru

  • Hero Member
  • *****
  • Posts: 505
Re: convert char to integer and inverse??
« Reply #21 on: August 12, 2017, 05:16:11 pm »
Which one, sample please?

i write 'سلام'(means Hello) but show 'سلا'

Lulu

  • Full Member
  • ***
  • Posts: 230
Re: convert char to integer and inverse??
« Reply #22 on: August 12, 2017, 05:28:37 pm »
Hello,

I've made an utility for manage UTF8 string easily, it work for my use but I don't know if it will work for any purpose...

Because an utf8 can have from 1 to 4 bytes length, I consider to use the string type to store one utf8 character.

As in the example attached, you can use
  - TS8 class to easily split each UTF8 character in an array and accessing by TS8.Char[index]
  - UTF8CharToCode to convert an UTF8 character to DWORD
  - CodeToUTF8Char to convert a DWORD code to an UTF8 character

Of course, this is not appropriate if you handle large utf8 string.
« Last Edit: August 12, 2017, 05:36:43 pm by Lulu »
wishing you a nice life

majid.ebru

  • Hero Member
  • *****
  • Posts: 505
Re: convert char to integer and inverse??
« Reply #23 on: August 12, 2017, 05:37:04 pm »
Hello,

I've made an utility for manage UTF8 string easily, it work for my use but I don't know if it will work for any purpose...

Because an utf8 can have from 1 to 4 bytes length, I consider to use the string type to store one utf8 character.

As in the example attached, you can use
  - TS8 class to easily split each UTF8 character in an array of string
  - UTF8CharToCode to convert an UTF8 character to DWORD
  - CodeToUTF8Char to convert a DWORD code to an UTF8 character

Of course, this is not appropriate if you handle large utf8 string.

Thank you very much

your code convert "س" to integer 55475.

now

how can i convert 55475 to "س"

Lulu

  • Full Member
  • ***
  • Posts: 230
Re: convert char to integer and inverse??
« Reply #24 on: August 12, 2017, 05:40:50 pm »
line 51 to 54 in Unit1 in attached project.
wishing you a nice life

majid.ebru

  • Hero Member
  • *****
  • Posts: 505
Re: convert char to integer and inverse??
« Reply #25 on: August 12, 2017, 06:02:21 pm »
sorry

YES

its works.

but it is so big (you use unit UTF8Utily and unit lazutf8)  :o %)

Thaddy

  • Hero Member
  • *****
  • Posts: 14383
  • Sensorship about opinions does not belong here.
Re: convert char to integer and inverse??
« Reply #26 on: August 12, 2017, 07:38:20 pm »
but it is so big (you use unit UTF8Utily and unit lazutf8)  :o %)

That's because of all the Lazarus people confusing themselves and you with their use of UTF8, while your wiki examples are UTF16.
You don't need those silly units.

Try this (I took Bart's example on purpose  8-) and corrected it):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.  T:UnicodeChar;
  4. begin
  5.  T:=UnicodeChar($00E1) ;  // simple typecast...to two byte unicode UTF16
  6.  Showmessage(T);  // compiler will convert, if any is needed
  7.  ShowMessage(IntToHex(Word(T),4)); //Simple typecast...to word.... from UTF16
  8. end;

Simple, isn't it? Now wait for the UTF8 adepts fury  :D :D >:D 8-) O:-) There's a bit more to it, but this will work most of the time...because it is UTF16...  :P

And it works with any of the characters from your wikipedia example.....
« Last Edit: August 12, 2017, 08:02:54 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

majid.ebru

  • Hero Member
  • *****
  • Posts: 505
Re: convert char to integer and inverse??
« Reply #27 on: August 12, 2017, 09:01:09 pm »
but it is so big (you use unit UTF8Utily and unit lazutf8)  :o %)

That's because of all the Lazarus people confusing themselves and you with their use of UTF8, while your wiki examples are UTF16.
You don't need those silly units.

Try this (I took Bart's example on purpose  8-) and corrected it):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.  T:UnicodeChar;
  4. begin
  5.  T:=UnicodeChar($00E1) ;  // simple typecast...to two byte unicode UTF16
  6.  Showmessage(T);  // compiler will convert, if any is needed
  7.  ShowMessage(IntToHex(Word(T),4)); //Simple typecast...to word.... from UTF16
  8. end;

Simple, isn't it? Now wait for the UTF8 adepts fury  :D :D >:D 8-) O:-) There's a bit more to it, but this will work most of the time...because it is UTF16...  :P

And it works with any of the characters from your wikipedia example.....

thank you very much

but i have question:

i don't know what text user input in tedit and i sbould use like this code
Code: Pascal  [Select][+][-]
  1. t:=unicodechar(edit1.text[1]);
  2.  

is not true code (i don't know).what is true code

your code convert Hex(that is const!!!!!) to char then your code convert char to hex .it is very good thank you very much you are good man.

now please

convert char to hex then convert hex to char.please.

Thaddy

  • Hero Member
  • *****
  • Posts: 14383
  • Sensorship about opinions does not belong here.
Re: convert char to integer and inverse??
« Reply #28 on: August 12, 2017, 10:33:56 pm »
My code works. And will correctly store it like you wanted....
RTM.
I can't help the ff'ing UTF8 Lazarus default... TEdit = Ansi and sometimes, but only sometimes true utf8 (for some reason). Simply assign to a unicodechar first.
Actually this is a demonstration of the stupidity of using utf8 and assume it has Ansi size..... Plain FPC does not suffer from it.. Because it supports UTF16...
« Last Edit: August 12, 2017, 10:49:45 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

majid.ebru

  • Hero Member
  • *****
  • Posts: 505
Re: convert char to integer and inverse??
« Reply #29 on: August 13, 2017, 07:08:16 am »
Hi

finally i solved  :-* :-[

@Handoko   i change your code. thank you

please see this code and say your idea

thank you

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     Edit1: TEdit;
  18.     Edit2: TEdit;
  19.     Edit3: TEdit;
  20.     Edit4: TEdit;
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure Button2Click(Sender: TObject);
  23.   private
  24.     { private declarations }
  25.   public
  26.     { public declarations }
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.Button1Click(Sender: TObject);
  39. var
  40.   TB: TBytes;
  41.   WS: WideString;
  42. begin
  43.   WS := Edit1.Caption;  //  Edit1.MaxLength = 1
  44.   TB := WideBytesOf(WS);
  45.   WS := '';
  46.   WS := IntToHex(TB[1], 2);
  47.   WS := WS + IntToHex(TB[0], 2);
  48.   //-----------------------
  49.   Edit2.Text := WS;
  50.   Edit3.Text := WS;
  51. end;
  52.  
  53. procedure TForm1.Button2Click(Sender: TObject);
  54. begin
  55.    Edit4.Text:= WideChar( StrToInt('$'+Edit3.Text));
  56. end;
  57.  
  58. end.
  59.  
« Last Edit: August 13, 2017, 07:11:04 am by majid.ebru »

 

TinyPortal © 2005-2018