8

I have string 00B0 which is Unicode of degree sign how can I convert in to symbol?

var str = "00B0";
var degreeSign = convesion(str);
console.log(degreeSign);

How it possible ?

Paresh Thummar
  • 933
  • 7
  • 20

4 Answers4

13
console.log('\u00B0');​​​​​​ // outputs ° in my chrome console

so basically: '\u00B0'

Gert Van de Ven
  • 997
  • 6
  • 6
  • 2
    This is how it is done. [MDN - Values, Variables and Literals: Unicode escape sequences](https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals#Unicode_escape_sequences). You can add this link and remove the "This works for me" to improve your answer. – Alan Gutierrez Apr 11 '12 at 12:02
  • The problem with this approach is that a string `'00B0'` can't easily be converted into a string `'\u00B0'`. The string literal `'\u00B0'` is a single character, but `'\u' + str` doesn't work to produce that. You would need to create code for the string literal and then execute the code to get the string. – Guffa May 01 '15 at 16:22
5

Parse the string into a number to get the character code, then use the String.fromCharCode method to create a string from it:

var degreeSign = String.fromCharCode(parseInt(str, 16));
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks, Now I have String that contain certain special character(in Unicode string). How can i decode it. E.g var str = "002E002C002D0021003F0040007E005F000A005C002F002600220027003B005E007C003A00280029003C007B007D003E005"; any idea ? please... – Paresh Thummar Apr 11 '12 at 11:57
  • @PareshThummar: Take four characters at a time from the string and parse them as above: http://jsfiddle.net/Guffa/DXf3R/ – Guffa Apr 11 '12 at 12:12
2

You can use Unicode characters in HTML by following this example

°

The degrees sign will appear like so: °

edit: Except this question is tagged as Node.js. What is the purpose of getting the symbol? Is it front-end or back-end?

Josh Davenport-Smith
  • 5,456
  • 2
  • 29
  • 40
2

If you're sure your string is well formed, you could also do it like this:

var degreeSign = eval("('\\u" + str + "')");
GOTO 0
  • 42,323
  • 22
  • 125
  • 158