Recent

Author Topic: degree symbol won't display correctly  (Read 4721 times)

ghostmaiden

  • Newbie
  • Posts: 5
degree symbol won't display correctly
« on: July 27, 2015, 03:12:53 am »
I'm currently writing a program to convert numbers to longititude and latitude. It's a console application.

The problem is it will not display the degree symbol (I know the character is alt-0176).
I'm on windows 7, and can get the degree symbol to display correctly. It's only when I run lazarus/delphi that I get some oddball character replacing the degree symbol.

Code: [Select]
program degrees_long_lat;

uses sysutils, windows;

label start;

var v : integer;
  key : char;

begin
start:
  key := 'q';
  key := 'c';
  write('please enter a value from -180 to 180 degrees: ');
  readln(v);
    if (v < 0) and (v > -180) then
      writeln(v, ' = ',not v+1 , ' ° west ')
    else if (v > 0) and (v < 180) then
      writeln(v,' = ', v , ' ° east ')
  else
    writeln('you have entered an invalid value.');
  writeln('press ''q'' to quit or ''c'' to continue..:');
    readln(key);
      if key = 'c' then goto start
      else if key = 'q' then
        writeln('quitting...');
      exit;

end. 


what would be the fix for this (besides converting it to an actual form application)?

balazsszekely

  • Guest
Re: degree symbol won't display correctly
« Reply #1 on: July 27, 2015, 08:57:48 am »
Hi,

This should work on windows:
Code: [Select]
uses sysutils, windows;

function UTF8ToWideString(const AStr: String): WideString;
var
  Len: Integer;
begin
  Result := '';
  if AStr <> '' then
  begin
    Len := MultiByteToWideChar(CP_UTF8, 0, PChar(AStr), -1, nil, 0);
    SetLength(Result, Len - 1);
    if Len > 1 then
      MultiByteToWideChar(CP_UTF8, 0, PChar(AStr), -1, PWideChar(Result), Len - 1);
  end;
end;

var
  lpNumberOfCharsWritten: DWord;
  Degree: WideString;
begin
  Degree := UTF8ToWideString('°');
  WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), PWideChar(Degree), Length(Degree), lpNumberOfCharsWritten, nil);
  Readln;
end.     

Alternatively you can set the console code page with SetConsoleCP(CP_UTF8) command. You can also try CP_WINUNICODE.

regards,
GetMem



fred

  • Full Member
  • ***
  • Posts: 201
Re: degree symbol won't display correctly
« Reply #2 on: July 27, 2015, 08:58:28 am »
It depends on the codepage you are using.
You could try set the codepage to 437 - United States using the comamand chcp 437

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: degree symbol won't display correctly
« Reply #3 on: July 27, 2015, 02:00:34 pm »
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686013%28v=vs.85%29.aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686036%28v=vs.85%29.aspx

I think a small comment is in order. The code page a console is using for output uses by default the oem code page which depends on the country installed and can be any number of ascii code pages (actually dos era since ascii is a 7bit standard). Assuming that it will always be in the same code page you are seeing inside windows, as you discover, is not correct.
« Last Edit: July 27, 2015, 02:14:58 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

ghostmaiden

  • Newbie
  • Posts: 5
Re: degree symbol won't display correctly
« Reply #4 on: July 28, 2015, 01:02:45 am »
thank you for responding.
as soon as I removed the 'crt' unit, and invoked the command 'setconsoleoutputcp', it displayed correctly.



« Last Edit: July 28, 2015, 02:11:59 am by ghostmaiden »

 

TinyPortal © 2005-2018