B4J Question CRLF Strangeness

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Hi all. Some strangeness I ran into...

The code below is written to a BAT file

B4X:
    Dim baseGIT As StringBuilder : baseGIT.Initialize
    baseGIT.Append("CD " & "C:\source\b4a_libs").Append(CRLF)
    baseGIT.Append($"git fetch -v --progress "origin" master"$).Append(CRLF)
    baseGIT.Append("CD " & "C:\source\b4a_shared_mods").Append(CRLF)
    baseGIT.Append($"git fetch -v --progress "origin" master"$).Append(CRLF)
    baseGIT.Append("CD " & mProjectFolder).Append(CRLF)
    baseGIT.Append($"git fetch -v --progress "origin""$).Append(CRLF)
    baseGIT.Append("git checkout ").Append($""${mTag}""$).Append(CRLF)
    baseGIT.Append("CD " & mWorkFolder).Append(CRLF)


But if you look at the output in Windows Notepad it looks like this.

upload_2015-9-23_10-31-36.png


It looks fine in Notepad++
 

LucaMs

Expert
Licensed User
Longtime User
Yes, it should look ok also in Wordpad.

If I remember well, CrLf should be ascii 13 + ascii 10; probably Notepad uses other code for "new line".


[a childhood memory: Cr = Carriage Return - Lf = Line Feed :D]
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Yes, LucaMS is correct. The Windows newline character is different from the Unix/Linux newline character (which is what you get with CRLF). Almost all Windows programs will recognize the Unix/Linux newline character, Notepad being the notable exception.

To use newline characters in a truly platform-independent way (as is the Java spirit), try this:
B4X:
Dim LS as String = GetSystemProperty("line.separator", CRLF)
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I too have enountered this when trying to send Http requests and Erel pointed out CRLF is only chr(10) as documented in the help.
Original post can be found here... [SOLVED] Network UDPSocket
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
Just to put my 5 cents in... CRLF should in fact be 0x0D 0x0A. It's two characters!! It doesn't matter which operating system it is, it's still two characters. It all comes down to a bit of history. For those of us 'blessed' to remember line printers the significance of CR and LF was important not only for printing but also for display systems. The CR (Carriage Return) told the printer (or the VDU -Visual Display Unit) to go back to the beginning of the current line. While the LF told the printer to move down to the next line (the VDU also did the same). So to go back to the beginning of the line and start on the next line you had to send both CR and LF which later was joined as CRLF. This is where the meaning and separating of the two began to be forgotten and started a reign of debates about the meaning.

For someone like myself, a living fossil that remembers the good old days of punch cards, representing CRLF as a single character of 0x0A is incorrect as it is simply a LF with no return to the start of the line.

Notepad expects the CRLF at the end of a line and it must also be in that order CR followed by LF. Get them around the wrong way and it will not recognise it. WordPad on the other hand, doesn't care. If a CRLF is used in a document then it treats it the same as it would the LF (not sure what it would do if there was just a CR at the end of the line...might have to check that one out).

It's time for my nap.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Just to put my 5 cents in... CRLF should in fact be 0x0D 0x0A. It's two characters!!
I completely agree and that's why I've fallen foul of this before. You kind of expect CRLF to be a carriage return AND a line-feed. But it's not my IDE and at least Erel has documented the fact that is only Chr(10).
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4X:
    Log(CRLF.Length)
For i = 0 To CRLF.Length - 1
    Log(Asc(CRLF.SubString2(i,i+1)))
Next

1
10
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
CRLF = Chr(10). This is true for B4J, B4A and B4i.

I agree that it can be confusing and it should have been named different (EndOfLine maybe). However CRLF value (chr 10) is the "correct" end of line character on Android iOS and Java apps.

Luca you can use CharAt instead.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
0x10 is correct for most systems since it's linux style line feeds.

Only windows needs both as far as I know.

A decent editor (notepad2 or so) will display it right.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I installed Notepad++ few days ago... I have read several times this your suggestion, but I'm lazy by now is well known.

It is very usefull, thank you.
 
Upvote 0

dilettante

Active Member
Licensed User
Longtime User
Since it is documented I wouldn't whinge about it, but calling it CRLF is misleading at best. Why not something like NEWLINE if that was the intent? So we live with the small curse and move on.

Also, there is nothing wrong with Notepad which follows the OS text stream convention properly. Telling people to go use another text editor is hardly any kind of answer. It might solve one user's temporary problem but interoperability with other software is still impacted.

Also: Line printers don't have a "carriage" since they print whole lines at a time, thus the name. So the CR character isn't relevant to them, and for that matter neither is the LF. The paper transport isn't controlled that way unless you have front-ended the printer with some sort of converter device that accepts ASCII text stream input.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Since it is documented I wouldn't whinge about it, but calling it CRLF is misleading at best. Why not something like NEWLINE if that was the intent? So we live with the small curse and move on.

Also, there is nothing wrong with Notepad which follows the OS text stream convention properly. Telling people to go use another text editor is hardly any kind of answer. It might solve one user's temporary problem but interoperability with other software is still impacted.

Also: Line printers don't have a "carriage" since they print whole lines at a time, thus the name. So the CR character isn't relevant to them, and for that matter neither is the LF. The paper transport isn't controlled that way unless you have front-ended the printer with some sort of converter device that accepts ASCII text stream input.
Agreed, but if you haven't tried Notepad++ before then you really should give it a go. It's an excellent text editor. I use the find feature a lot for another programming language which I use that doesn't have a very friendly IDE.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Also, there is nothing wrong with Notepad which follows the OS text stream convention properly
Developers should use a text editor that allows them to understand the file content. Notepad++ allows you to see the end of line characters, the encoding and many other useful features. This is a personal recommendation. Everyone are free to use whichever text editor they like.

When you build applications that run on multiple platforms then you should be able to work with text files that end with any type of EOL characters.
 
Upvote 0

dilettante

Active Member
Licensed User
Longtime User
True enough, I have nothing bad to say about Notepad++ at all and I find it useful. I just don't like a constant named CRLF to be something else.

What next? A "/" operator that multiplies? ;)
 
Upvote 0

emexes

Expert
Licensed User
True enough, I have nothing bad to say about Notepad++ at all and I find it useful. I just don't like a constant named CRLF to be something else.

What next? A "/" operator that multiplies? ;)
Interestingly: you can do multiplication with divisions, but not division with multiplications.

eg: x/(1/y) = x*y

or numerically eg: 5/(1/6) = 5*6 = 30

unless y is zero in which case... well, actually, IEE754 might actually still get you across the finish line, if the 1/0 = ∞ and x/∞ = 0 don't get trapped as errors 🤔
 
Upvote 0
Top