Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Convertir Hexa en character

13 réponses
Avatar
nabil kasmi
Bonjour,

je souhaite avoir une fonction javascript qui convertit les hexa en texte :

ex: Papeterie

en "Papeterie".

Cordialement,

Nabil.

3 réponses

1 2
Avatar
Dr J R Stockton
En fr.comp.lang.javascript <460a63c6$0$3426$,
2007-03-28 14:52:04, nabil kasmi a ecrit:

je souhaite avoir une fonction javascript qui convertit les hexa en texte :

ex: &#x50;&#x61;&#x70;&#x65;&#x74;&#x65;&#x72;&#x69;&#x65;

en "Papeterie".



X = "&#x50;&#x61;&#x70;&#x65;&#x74;&#x65;&#x72;&#x69;&#x65;"
X = X.replace(/&#(xd+);/g, function (a, b)
{ return String.fromCharCode("0"+b) } ) // donne "Papeterie"

Voyez aussi <URL:http://www.merlyn.demon.co.uk/js-other.htm#Safe>, pour
les *decimals*

<span ID=UnCo></span> <!-- Espace de travail -->

function Wryt(ID, S) { document.getElementById(ID).innerHTML = S }

function Dec(S) { Wryt('UnCo', S) ; S = UnCo.innerText
Wryt('UnCo', "") ; return S }

Lisez newsgroup c.l.j et sa FAQ. Vide Infra.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Avatar
Elegie
Dr J R Stockton wrote:

Hello John,

X = "&#x50;&#x61;&#x70;&#x65;&#x74;&#x65;&#x72;&#x69;&#x65;"
X = X.replace(/&#(xd+);/g, function (a, b)
{ return String.fromCharCode("0"+b) } ) // donne "Papeterie"


[lang=fr]
J'avais complètement oublié la méthode fromCharCode; votre idée est
élégante, John, mais la regexp n'est pas correcte, comme expliqué par
Olivier, les nombres hexadécimaux comportent également des [a-f]; par
ailleurs, un indicateur ignore-case rendrait le tout plus robuste.
[/lang]

[lang=en]
I had completely forgotten about the fromCharCode method, good insight
John, however the regexp isn't correct, as explained by Olivier, hex
numbers are also composed of [a-f]; an ignore-case flag would also be safer.
[/lang]

X.replace(/&#(x[0-9a-f]+);/gi, function (a, b)
{ return String.fromCharCode("0"+b) } )

Vide Infra.


I've just done that, and my girlfriend just slapped me... :$


Cheers,
Elegie.

Avatar
nabil kasmi
Merci beaucoup,
celà fonctionne impécablement.
Je vous souhaite une exelente journée a tous.
Cordialement,
Nabil

"Elegie" a écrit dans le message de
news:460e29b4$0$11939$
Dr J R Stockton wrote:

Hello John,

X = "&#x50;&#x61;&#x70;&#x65;&#x74;&#x65;&#x72;&#x69;&#x65;"
X = X.replace(/&#(xd+);/g, function (a, b)
{ return String.fromCharCode("0"+b) } ) // donne "Papeterie"


[lang=fr]
J'avais complètement oublié la méthode fromCharCode; votre idée est
élégante, John, mais la regexp n'est pas correcte, comme expliqué par
Olivier, les nombres hexadécimaux comportent également des [a-f]; par
ailleurs, un indicateur ignore-case rendrait le tout plus robuste.
[/lang]

[lang=en]
I had completely forgotten about the fromCharCode method, good insight
John, however the regexp isn't correct, as explained by Olivier, hex
numbers are also composed of [a-f]; an ignore-case flag would also be
safer.

[/lang]

X.replace(/&#(x[0-9a-f]+);/gi, function (a, b)
{ return String.fromCharCode("0"+b) } )

Vide Infra.


I've just done that, and my girlfriend just slapped me... :$


Cheers,
Elegie.




1 2