Recent

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

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Hi
.
i read about unicode.but i almoste do not underestand about unicode?!?! :'( :'(
.
can anyone help and guide me , please %) %)
.
i want to convert unicode  Character to integer and then convert integer to unicode?
.
but i can't :( :(

thank you
« Last Edit: August 13, 2017, 10:22:07 am by majid.ebru »

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: unicode convert??
« Reply #1 on: August 11, 2017, 03:35:17 pm »
Which unicode? UTF7?, UTF8?, UTF16-LE? UTF16-BE? UTF32? There's nothing uni in unicode.... O:-)
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: unicode convert??
« Reply #2 on: August 11, 2017, 04:08:26 pm »
Briefly explain what you want to achieve, with an example.  8-)
Holiday season is online now. :-)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4467
  • I like bugs.
Re: unicode convert??
« Reply #3 on: August 11, 2017, 04:55:39 pm »
i read about unicode.but i almoste do not underestand about unicode?!?! :'( :'(
You are not the only one.  :)
One hint: The encodings are not the most complex part of Unicode.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: unicode convert??
« Reply #4 on: August 12, 2017, 10:43:52 am »
oh my god

so sorry

i change the title topic
 
i know and all of you say true,but my question is simple and i don't say correctly ?!?!


i use this code and convert char to integer:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Edit1.Text := inttostr(ord(Edit2.Text[1]));
  4. end;
  5.  

and use this code for inverse (integer to char ) :

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. begin
  3.   Edit2.Text := chr(strtoint(Edit1.Text));
  4. end;
  5.  

these codes work correctly for English char

but i want to use Farsi char , i cant convert ??



when i type for example "خ" i see:

Code: Pascal  [Select][+][-]
  1.  
  2. // If Edit2.Tex:= 'خ';
  3.  
  4. procedure TForm3.Button1Click(Sender: TObject);
  5. begin
  6.   Edit1.Text := inttostr(ord(Edit2.Text[1]));      // ==> Edit1.Text = 216
  7.   Edit3.Text := inttostr(ord(Edit2.Text[2]));      // ==> Edit3.Text = 176
  8. end;
  9.  

in above link (from Wikipedia) i see :
Quote
Name    Persian    Unicode    Arabic    Unicode
0    ۰    U+06F0    ٠    U+0660
1    ۱    U+06F1    ١    U+0661
2    ۲    U+06F2    ٢    U+0662
3    ۳    U+06F3    ٣    U+0663
4    ۴    U+06F4    ٤    U+0664
5    ۵    U+06F5    ٥    U+0665
6    ۶    U+06F6    ٦    U+0666
7    ۷    U+06F7    ٧    U+0667
8    ۸    U+06F8    ٨    U+0668
9    ۹    U+06F9    ٩    U+0669
ye    ی    U+06CC    ي    U+064A
kāf    ک    U+06A9    ك    U+0643

but i don't what is this and i don't know howi can  use this?!
« Last Edit: August 12, 2017, 10:52:01 am by majid.ebru »

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: convert char to integer and inverse??
« Reply #5 on: August 12, 2017, 11:30:06 am »
There are functions in LazUtf8 unit that convert a UTF codepoint to a Unicode number, I think the function is called Utf8ToUnicode (I'm currently not at a computer with Lazarus).
Is that what you want?

Bart

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: convert char to integer and inverse??
« Reply #6 on: August 12, 2017, 11:57:06 am »
There are functions in LazUtf8 unit that convert a UTF codepoint to a Unicode number, I think the function is called Utf8ToUnicode (I'm currently not at a computer with Lazarus).
Is that what you want?

Bart

i don't know !?!
how can i use it ?
it has very input?!?!
Code: Pascal  [Select][+][-]
  1. function Utf8ToUnicode(Dest: PUnicodeChar; MaxDestChars: SizeUInt; Source: PChar; SourceBytes: SizeUInt): SizeUInt;

can you say a sample or more guide me ,Please

Handoko

  • Hero Member
  • *****
  • Posts: 5150
  • My goal: build my own game engine using Lazarus
Re: convert char to integer and inverse??
« Reply #7 on: August 12, 2017, 01:12:20 pm »
I am not familiar with unicode things, but maybe this is what you want:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   SysUtils, Forms, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Label1: TLabel;
  18.     Label2: TLabel;
  19.     Label3: TLabel;
  20.     procedure Button1Click(Sender: TObject);
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. var
  34.   TB: TBytes;
  35.   WS: WideString;
  36.   S:  string;
  37.   i:  Integer;
  38. begin
  39.   WS := Edit1.Caption;
  40.   TB := WideBytesOf(WS); // <<<----- use TBytes to access the characters' value
  41.  
  42.   // WideString to Hexadecimal
  43.   S := '';
  44.   for i := Low(TB) to High(TB) do
  45.     S := S + IntToHex(TB[i], 2)+' ';
  46.   Label1.Caption := 'Hexadecimal values: ' +#13 + S;
  47.  
  48.   // WideString to Decimal
  49.   S := '';
  50.   for i := Low(TB) to High(TB) do
  51.     S := S + IntToStr(TB[i])+' ';
  52.   Label2.Caption := 'Decimal values: ' + #13 + S;
  53.  
  54.   // Convert TBytes to WideString
  55.   Label3.Caption := 'TBytes to WideString: ' + #13 + WideString(TB);
  56.  
  57. end;
  58.  
  59. end.

The code seems to work but it has 2 warnings when compiling:
- Warning: Implicit string type conversion from "TTranslateString" to "WideString"
- Warning: Implicit string type conversion with potential data loss from "UnicodeString" to "TTranslateString"

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: convert char to integer and inverse??
« Reply #8 on: August 12, 2017, 02:03:30 pm »
@Handoko O:-)

thank you very very much. :-* :-* :-*

but my big problem is convert integer to char.

how can i do that.

help me please.

thank you very much  again

Handoko

  • Hero Member
  • *****
  • Posts: 5150
  • My goal: build my own game engine using Lazarus
Re: convert char to integer and inverse??
« Reply #9 on: August 12, 2017, 02:12:35 pm »
You cannot use char type variable to store that unicode (or Persian alphabet) because those single alphabet consists more than a single character. That why, I used TBytes.

Did you mean you want to create a TEdit for user to provide value input but using Persian number? If yes, please provide me the Persian alphabet of 0 - 9 because I need them to write a PersianStringToInt and IntToPersianString functions manually.

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: convert char to integer and inverse??
« Reply #10 on: August 12, 2017, 03:08:54 pm »
Try this variant record approach:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  6.  
  7. type
  8.     Tutf16conv=record
  9.     case byte of
  10.     0:(C:UnicodeChar);
  11.     1:(W:word);
  12.     end;
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.Button1Click(Sender: TObject);
  34. var
  35.  T:Tutf16conv;
  36. begin
  37.  T.w:=$06A9 ;
  38.  Showmessage(T.c); // prints  u-06A9 correctly
  39. end;
  40. end.
  41.  

You can use lo()/hi() to access the byte values from the word if needed too. And you can cast a UnicodeChar to the record type.... Just an idea...
« Last Edit: August 12, 2017, 03:29:37 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: convert char to integer and inverse??
« Reply #11 on: August 12, 2017, 04:08:52 pm »
You cannot use char type variable to store that unicode (or Persian alphabet) because those single alphabet consists more than a single character. That why, I used TBytes.

Did you mean you want to create a TEdit for user to provide value input but using Persian number? If yes, please provide me the Persian alphabet of 0 - 9 because I need them to write a PersianStringToInt and IntToPersianString functions manually.

No

i don't want to save only Persian char and i want to save all Unicode(UTF8)  char(number or alphabet or special char) like Arabic or Chinese and ... all char.
 

i want to convert char(all Unicode char )  to integer and then save to data Base. i just want to save integer (all field my bank is integer). after i save integer to bank .i want when read them and convert to Unicode char

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: convert char to integer and inverse??
« Reply #12 on: August 12, 2017, 04:13:29 pm »
Maybe you are looking for UTF8CharacterToUnicode function (unit LazUtf8)

For "á"  (a with accent ecu, UTF8: C3 A1) it will give $00E1, which means it is Unicode codepoint U+0x00E1.

Here's a snippet from oneofmy programs sing it:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormUTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
  2. var
  3.   p: PChar;
  4.   Unicode: Cardinal;
  5.   CharLen, i: integer;
  6. begin
  7.   p := @Utf8Key[1];
  8.   Unicode := UTF8CharacterToUnicode(p,CharLen);
  9.  ...
  10.  

Bart

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: convert char to integer and inverse??
« Reply #13 on: August 12, 2017, 04:16:05 pm »
Try this variant record approach:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  6.  
  7. type
  8.     Tutf16conv=record
  9.     case byte of
  10.     0:(C:UnicodeChar);
  11.     1:(W:word);
  12.     end;
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.Button1Click(Sender: TObject);
  34. var
  35.  T:Tutf16conv;
  36. begin
  37.  T.w:=$06A9 ;
  38.  Showmessage(T.c); // prints  u-06A9 correctly
  39. end;
  40. end.
  41.  

You can use lo()/hi() to access the byte values from the word if needed too. And you can cast a UnicodeChar to the record type.... Just an idea...

it shows "ک";

very good

how can you  convert "ک" to $06A9?

how can i convert "ک" (and all char )  to (integer code )  $06A9?

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: convert char to integer and inverse??
« Reply #14 on: August 12, 2017, 04:38:04 pm »
it is my problem :

how can i write "Function  1" and "Function  2"

what is code of them

 

TinyPortal © 2005-2018