Recent

Author Topic: How to output #13 char?  (Read 985 times)

martinrame

  • Full Member
  • ***
  • Posts: 119
How to output #13 char?
« on: July 20, 2019, 03:15:39 pm »
Hi, I need to output CR (ascii 13) on a very simple Linux command line program. To do that I just do this:

Code: Pascal  [Select][+][-]
  1. write('Hello ' + #13 + 'world.' + #13);

When I examine the output with xxd I get this:

Code: Pascal  [Select][+][-]
  1. 00000000: 4865 6c6c 6f20 776f 726c 642e            Hello world.

As you can see, there isn's any 0d (0d is 13 in hex) char in the output.

Why the CR is removed?.

Thaddy

  • Hero Member
  • *****
  • Posts: 14380
  • Sensorship about opinions does not belong here.
Re: How to output #13 char?
« Reply #1 on: July 20, 2019, 03:25:15 pm »
Because a linebreak in Linux is #10, not #13.
use
Code: Pascal  [Select][+][-]
  1.   writeln('Hello',LineEnding,'World. ,Proof it is #10: #',Ord(LineEnding));  
  2.  
  3.  

This is the cross platform way.
« Last Edit: July 20, 2019, 03:27:45 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

martinrame

  • Full Member
  • ***
  • Posts: 119
Re: How to output #13 char?
« Reply #2 on: July 20, 2019, 03:27:13 pm »
I know that, but I need to return #13 to be read by another program that expect it instead of #10. How can I do that?.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: How to output #13 char?
« Reply #3 on: July 20, 2019, 03:28:36 pm »
Check with strace/ktrace what the application actually writes.

Otherwise it might be a problem of other parts of the system, like xxd, filtering the #13s

Thaddy

  • Hero Member
  • *****
  • Posts: 14380
  • Sensorship about opinions does not belong here.
Re: How to output #13 char?
« Reply #4 on: July 20, 2019, 03:29:10 pm »
write #13#10. The #13 will be ignored on unix systems but #13 will be used on dos/windows.
Code: Pascal  [Select][+][-]
  1.   writeln('Hello',#13#10,'World.');

Reason this works cross platform is that these are non-printable characters so #13 is ignored on linux. 
Code: Bash  [Select][+][-]
  1. Hello
  2. World.

Code: Bash  [Select][+][-]
  1. xxd testlineending | grep Hello
  2. 00035990: 0548 656c 6c6f 0000 020d 0a00 0657 6f72  .Hello.......Wor

See the 0d0a?


« Last Edit: July 20, 2019, 03:42:38 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

martinrame

  • Full Member
  • ***
  • Posts: 119
Re: How to output #13 char?
« Reply #5 on: July 20, 2019, 03:39:35 pm »
Yes, #10 is in the response, but the caller expects #13.

The caller is an integration engine (Mirth Connect) that expects it, this is the response I'm getting:

Code: Pascal  [Select][+][-]
  1. ERROR MESSAGE: Error receiving response: End of message bytes (<F5><CR>) not detected. Last bytes received: <FS><LF>

martinrame

  • Full Member
  • ***
  • Posts: 119
Re: How to output #13 char?
« Reply #6 on: July 20, 2019, 03:41:59 pm »
If I just do this:

Code: Pascal  [Select][+][-]
  1. echo "Hello\rWorld"|xxd

The output is correct:

Code: Pascal  [Select][+][-]
  1. 00000000: 4865 6c6c 6f0d 576f 726c 640a            Hello.World.

The 0d after 6f is the #13 char.

Thaddy

  • Hero Member
  • *****
  • Posts: 14380
  • Sensorship about opinions does not belong here.
Re: How to output #13 char?
« Reply #7 on: July 20, 2019, 03:43:33 pm »
I edited my response.
See above I added xxd output.

This *should* be acceptable
« Last Edit: July 20, 2019, 03:46:20 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

martinrame

  • Full Member
  • ***
  • Posts: 119
Re: How to output #13 char?
« Reply #8 on: July 20, 2019, 03:48:30 pm »
Well, it seems to be some unit in my program doing something strange, because using this very simple program the output is correct:

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. begin
  4.   writeln('Hello', #13, 'World');
  5. end.

Output:

Code: Pascal  [Select][+][-]
  1. 00000000: 4865 6c6c 6f0d 576f 726c 640a            Hello.World.

martinrame

  • Full Member
  • ***
  • Posts: 119
Re: How to output #13 char?
« Reply #9 on: July 20, 2019, 03:50:40 pm »
I found it!, in one of the millions of tests I did I included the crt unit. I removed it and it started to work as expected.
« Last Edit: July 20, 2019, 04:04:06 pm by martinrame »

Thaddy

  • Hero Member
  • *****
  • Posts: 14380
  • Sensorship about opinions does not belong here.
Re: How to output #13 char?
« Reply #10 on: July 20, 2019, 03:50:56 pm »
On my side - but I do not have sufficient information - the output handled as string is only correct when #13#10 is used. Both on Windows and Linux.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018