Displaying the degree symbol in code

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Hi All,

Does anyone here knows how to display the 'degree' symbol in a string (C, C#, Java,...)?

e.g. To display 22 degree celsuis, I want to display it this way: 22 0C (Note the symbol is not displayed properly here)

Many Thanks
Eric
 

OBW0549

Joined Mar 2, 2015
3,566
Does anyone here knows how to display the 'degree' symbol in a string (C, C#, Java,...)? e.g. To display 22 degree celsuis, I want to display it this way: 22 0C (Note the symbol is not displayed properly here)
According to this table, the degree symbol corresponds to extended ASCII code 0xB0 (decimal 176).
 

jpanhalt

Joined Jan 18, 2008
11,087
Your display may only have 128 characters (including null). In that situation, I have defined a special character for the degree symbol right after power on reset. What type and model display are you using?

John
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Straight from my uppermost - leftmost lower case key: º

Typing Alt+0176, I get the same: °
Yeah but no! I meant in code so I did something like this: string FahrenheitValue = String.Format("{0} \u00B0C = {1} \u00B0F", CelsiusValue, convertCelsiusToFahrenheit(CelsiusValue));

Thanks anyway as your Alt+0176 would be useful in word or Notepad.

Eric.
 

jpanhalt

Joined Jan 18, 2008
11,087
Depending on your compiler, i.e., if it accepts ascii radix, you can just put the symbol in single quotes like this: '°' The compiler will do the work of looking up the character value for you. You can do that with any ascii character.

John
 

atferrari

Joined Jan 6, 2004
4,769
Yeah but no! I meant in code so I did something like this: string FahrenheitValue = String.Format("{0} \u00B0C = {1} \u00B0F", CelsiusValue, convertCelsiusToFahrenheit(CelsiusValue));

Thanks anyway as your Alt+0176 would be useful in word or Notepad.

Eric.
Now I see, reading the John's post below that I failed to include the ASCII "°" expresion which is what you were asking for. Excuse moi!
 

WBahn

Joined Mar 31, 2012
30,055
Hi All,

Does anyone here knows how to display the 'degree' symbol in a string (C, C#, Java,...)?

e.g. To display 22 degree celsuis, I want to display it this way: 22 0C (Note the symbol is not displayed properly here)

Many Thanks
Eric
Not in any portable way. You have a couple of issue -- first is representing it to the compiler. To use the single-quotes approach, such as '°', the ° character has to first be in the source-code alphabet, which it probably isn't. You get around that by escaping it explicitly using the backslash and the character code. But that brings up the other issue in that escaping the character won't help if the character isn't in the execution character set or, probably more to the point, if the O/S's console font doesn't happen to include that symbol mapped to whatever code you are using for that symbol.

So in C you a pretty well screwed (at least doing it portably).

Since Java's native character set is Unicode (well, UTF-16), you almost certain can do it there -- I've just never done it so I don't know the ins and outs.
 

jpanhalt

Joined Jan 18, 2008
11,087
Not in any portable way. You have a couple of issue -- first is representing it to the compiler. To use the single-quotes approach, such as '°', the ° character has to first be in the source-code alphabet, which it probably isn't. You get around that by escaping it explicitly using the backslash and the character code. But that brings up the other issue in that escaping the character won't help if the character isn't in the execution character set or, probably more to the point, if the O/S's console font doesn't happen to include that symbol mapped to whatever code you are using for that symbol.

So in C you a pretty well screwed (at least doing it portably).

Since Java's native character set is Unicode (well, UTF-16), you almost certain can do it there -- I've just never done it so I don't know the ins and outs.
"Probably isn't" means it just may be. I guess millions of MPLab users aren't counted in the "probably."

upload_2016-12-5_4-34-41.png

Program on right. Disassembly on left. Compiler MPLab.

John
 

Picbuster

Joined Dec 2, 2013
1,047
Hi All,

Does anyone here knows how to display the 'degree' symbol in a string (C, C#, Java,...)?

e.g. To display 22 degree celsuis, I want to display it this way: 22 0C (Note the symbol is not displayed properly here)

Many Thanks
Eric
depending on display use printf(" temperature = %2.2f%dC", temp,223); // the 223 does the trick.
This works for sure with a matrix type display (HD 44780 most common)
Picbuster
 

WBahn

Joined Mar 31, 2012
30,055
"Probably isn't" means it just may be. I guess millions of MPLab users aren't counted in the "probably."

View attachment 116417

Program on right. Disassembly on left. Compiler MPLab.

John
Yes, "probably isn't" does not preclude "it just might be". I'm talking about portability. If you want to encourage writing code that "might work", go right ahead.
 

WBahn

Joined Mar 31, 2012
30,055
depending on display use printf(" temperature = %2.2f%dC", temp,223); // the 223 does the trick.
This works for sure with a matrix type display (HD 44780 most common)
Picbuster
So anytime a person prints out a decimal value that happens to be 223 they get a degree symbol?

I could see this working on "some" displays if a "%c" format specifier were used. But you would need to be sure that your users only ran your program on platforms that used those displays.

Unless your code is tightly bundled with the hardware -- which embedded code frequently is -- using "tricks" that are non-portable is usually a band aid that results in much more serious wounds to the patient when finally ripped off.
 

jpanhalt

Joined Jan 18, 2008
11,087
Yes, "probably isn't" does not preclude "it just might be". I'm talking about portability. If you want to encourage writing code that "might work", go right ahead.
It is not a matter of wanting to write non-portable code, it is a question of knowing the limitations of your compiler. If your compiler can only handle one radix, find a better compiler.

Here is a little code piece exactly as listed on PICList:
upload_2016-12-5_12-17-14.png

Notice that ascii radix is used instead of hex in parts. While I suspect Dattalo did that just for fun, there is nothing wrong or non-portable about it.

John
 

WBahn

Joined Mar 31, 2012
30,055
It is not a matter of wanting to write non-portable code, it is a question of knowing the limitations of your compiler. If your compiler can only handle one radix, find a better compiler.
You are showing examples from assembly -- there IS no compiler, there is an assembler. Assemblers are notoriously nonportable -- it is rather difficult to write code of any complexity that will assemble and run on another assembler precisely because of the plethora of assembler-specific directives and pseudoinstructions and macro constructs and the list goes on that end up being used.

The TS asked about high-level languages in which the notion of portability is directly tied to the ability to write code that does NOT depend on which compiler you use, provided the compiler is sufficiently compliant with the LANGUAGE standard. By recommending that someone go find a compiler that supports what they want to do, you are recommending that they NOT write portable code. Think about it -- if you share your code with someone, you have to also tell them which compiler(s) they HAVE to use if they want that code to compile successfully -- that's the antithesis of the very aim of writing portable code!
 

jpanhalt

Joined Jan 18, 2008
11,087
WBahn said:
You are showing examples from assembly -- there IS no compiler, there is an assembler.
Wrong. The Assembler is a type of compiler. Please stop your obnoxious quibbling about words.

You have taken a comment I made about using ascii as a way to find its hex equivalent or simply to substitute for looking up the equivalent and made it into a major issue. My only intent was to be helpful. If you disagree, then you need to use your moderator powers and delete my post.

I don't care what language you are using, I find a construct such as, <Print "Hi"> far more readable and concise than <Print ox48> <Print 0x69> .

Thank you.

John
 

WBahn

Joined Mar 31, 2012
30,055
You are the one that is insisting on making it a major issue. I merely observed that what you recommended generally results in code that is not portable. I can't help it if you don't understand what is involved in "portable code".

My role as a mod has absolutely nothing to do with any of this -- unless you are maintaining that a mod does not have the right to interact on the forums like any other member.
 

Picbuster

Joined Dec 2, 2013
1,047
So anytime a person prints out a decimal value that happens to be 223 they get a degree symbol?

I could see this working on "some" displays if a "%c" format specifier were used. But you would need to be sure that your users only ran your program on platforms that used those displays.

Unless your code is tightly bundled with the hardware -- which embedded code frequently is -- using "tricks" that are non-portable is usually a band aid that results in much more serious wounds to the patient when finally ripped off.
No, it prints char(223) followed by a C when using the display as stated.
This will take two positions at your display. if however you want one character with the complete degree sign you have to build the character matrix.
Picbuster
 

WBahn

Joined Mar 31, 2012
30,055
No, it prints char(223) followed by a C when using the display as stated.
This will take two positions at your display. if however you want one character with the complete degree sign you have to build the character matrix.
Picbuster
Look at the code carefully:

printf(" temperature = %2.2f%dC", temp,223); // the 223 does the trick.

You are using the "%d" format specifier to print the value 223. This will print 223 as a decimal integer. Try it.

If you want to print the character symbol whose code is 223, then you need to use the "%c" format specifier.

But there is no guarantee that char(233) is the degree symbol. It just happens to be the degree symbol in SOME extended ASCII character sets, but certainly not all -- there are LOTS of incompatible extended ASCII character sets. For example, in the ISO-8859-1 (ISO Latin 1), char(233) yields the lowercase letter e with an acute accent mark; the degree symbol is char(175) in this extended ASCII set. In the character set used by this forum's software char(233) is the character Θ, while the degree symbol, °, is char(248). The C language standard imposes no requirement that the degree symbol even be in the execution character set and, if it is, that its code must be 233. Beyond the requirements for the minimum characters that must be in the execution character set, anything else is implementation defined.
 

Picbuster

Joined Dec 2, 2013
1,047
oops,
You are correct mistyped should be %c
Sorry to quick no control.
Any way I used it with batton 42012 4 lines x 20 char's
Here's part of my working code.
Ship=sprintf(Lcd_Buff2,"Set:%2.2f T1:%2.2f%cC",Setpoint_Temp,Tem_Mcp,223);
lcd_goto(DR1);
lcd_puts(Lcd_Buff2);

Picbuster
 
Top