Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: newline hex values

Tags:

c

newline

hex

I've got some code which reads lines from a txt file and then does adds to a database.

If you type data straight into the txt file, then this is sufficient to recognise a newline: if (ch == '\n')

However, if you cut & paste out of Microsoft Word, checking for \n doesn't work.

If I dump out the hex values / characters one by one, the actual data looks like this:

2e .    [ Last char of line ]
 d 
58 X    [ First char on next line ]

The full stop is the last character on one line. The 'X' is the first character on the next line. The hex 'd' causes the newline.

What is going on? And how can I test my ch variable against > d<, given that it's space-d in the hex?

Thanks.

like image 741
Nick Avatar asked May 10 '12 07:05

Nick


2 Answers

Windows uses a pair of characters to end a line: a carriage return (0x0d) followed by a line feed (0x0a).

You can write these in C as '\r' and '\n', respectively.

like image 156
unwind Avatar answered Sep 28 '22 00:09

unwind


Hexadecimal literals in C are prefixed with 0x or 0X, so in your case you could use 0xd or 0XD or 0xD.

like image 31
Sebastian Mach Avatar answered Sep 27 '22 23:09

Sebastian Mach