### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: How to convert ascii to Dec ?  (Read 7768 times)

#### ahmetnurideniz

• Full Member
• Posts: 110
• As you sow, you shall reap.
##### How to convert ascii to Dec ?
« on: September 05, 2010, 04:21:31 am »
Hi,
my device send mi ascii data. i sometimes i can read data. But sometines i can't read data.

my device send me
'#232#3#13#10' for  1000 and i can read it 1000
'#219 for 65499 and i can read it 219
is it normal?

is anyone has asciitodec function?
« Last Edit: September 05, 2010, 06:24:41 am by ahmetnurideniz »

#### typo

• Hero Member
• Posts: 3051
##### Re: How to convert ascii to Dec ?
« Reply #1 on: September 05, 2010, 05:41:44 am »
Code: [Select]
`ShowMessage(IntToStr(Ord('A')));`
or

Code: [Select]
`uses LCLProc;var  CharLen :integer;begin  ShowMessage(IntToStr(UTF8CharacterToUnicode(PChar('ت'), CharLen)));  end;`
« Last Edit: September 05, 2010, 06:13:40 am by typo »

#### ahmetnurideniz

• Full Member
• Posts: 110
• As you sow, you shall reap.
##### Re: How to convert ascii to Dec ?
« Reply #2 on: September 05, 2010, 06:49:28 am »
Code: [Select]
`ShowMessage(IntToStr(Ord('A')));`
i test this the result is 2196232 it should be 65499

or

Code: [Select]
`uses LCLProc;var  CharLen :integer;begin  ShowMessage(IntToStr(UTF8CharacterToUnicode(PChar('ت'), CharLen)));  end;`when i test this lazarus is crash.
my code is
Code: [Select]
`      if len = 3 then begin      result := result +   (IntToStr(UTF8CharacterToUnicode(PChar(b), len)));      end;  `len =3
and b = 219

#### ahmetnurideniz

• Full Member
• Posts: 110
• As you sow, you shall reap.
##### Re: How to convert ascii to Dec ?
« Reply #3 on: September 05, 2010, 06:52:30 am »
i have c# code for my device
the code is
Code: [Select]
`                            // Word                            UInt16 b16 = BitConverter.ToUInt16(m_buffer, 0);                            resultL.Text = b16.ToString();`how can i port this lines Lazarus?

#### Laksen

• Hero Member
• Posts: 720
##### Re: How to convert ascii to Dec ?
« Reply #4 on: September 05, 2010, 11:24:40 am »
result.Text := IntToStr(PWord(@buffer[0])^);

#### bobc

• New Member
• Posts: 41
##### Re: How to convert ascii to Dec ?
« Reply #5 on: September 05, 2010, 11:59:25 am »
Hi,
my device send mi ascii data. i sometimes i can read data. But sometines i can't read data.

my device send me
'#232#3#13#10' for  1000 and i can read it 1000
'#219 for 65499 and i can read it 219
is it normal?
I would suggest if you received just #219, it is only part of the message. From your description of the PLC protocol, I would expect to see #219#255#13#10 for 65499. (219 + 255*256 = 65499)

#### bobc

• New Member
• Posts: 41
##### Re: How to convert ascii to Dec ?
« Reply #6 on: September 05, 2010, 12:16:02 pm »
i have c# code for my device
the code is
Code: [Select]
`                            // Word                            UInt16 b16 = BitConverter.ToUInt16(m_buffer, 0);                            resultL.Text = b16.ToString();`how can i port this lines Lazarus?

I would write
Code: [Select]
`var  b16: word;  resultL: TLabel; // a label on the form ...  b16 := m_buffer[0] + m_buffer[1] * 256;  resultL.Caption := IntToStr (b16);`

#### ahmetnurideniz

• Full Member
• Posts: 110
• As you sow, you shall reap.
##### Re: How to convert ascii to Dec ?
« Reply #7 on: September 06, 2010, 04:52:25 pm »
i tested that
Code: [Select]
`  var  b,bb: byte;  buf: pchar;    b16: word; begin  if len = 3 then begin    b16 :=  (buf[0]) + (buf[1]) + 256 ;    result := IntToStr(b16) ;  end;         end;`there is error message "Error: Element zero of an ansi/wide- or longstring can't be accessed, use (set)length instead"

then i tested this code
Code: [Select]
`  var  b,bb: byte;  buf: pchar;    b16: word;       if len = 3 then begin        b16 :=  ord(buf[0]) + ord(buf[1]) + ord(buf[2]) + 256;        result := IntToStr(b16);      end;    `then result is 569 when plc send 65499
i check
buf[0] = 219
buf[1]= 62
buf[2]= 32