/    Sign up×
Community /Pin to ProfileBookmark

Translate decimals into hexadecimals.

Hi !

and thank you for writing in a thread of mine.

Just now, i am trying to write a function that could translate decimal numbers into hexadecimal numbers.

Here is the mess :

<!– hexadecimeur –>

<script type=”text/javascript”>

function hexadecimer(decimal)
{
thesaurus = ‘0123456789abcdef’
hexadec_carax = thesaurus.split(”)

var nouveau_caractere = ”

if( decimal-decimal%Math.pow(16, 1) = 0 )
nouveau_caractere = decimal%16
else
reste = decimal – decimal%Math.pow(16, 1)

if(reste-decimal%Math.pow(16, 2) = 0)
nouveau_caractere = 0
else
reste = decimal-decimal%Math.pow(16, 2)

i = 6
while( i > 0 )
{
if( decimal-decimal%Math.pow(16, i) = 0 )
hexadec_carac = hexadec_carax[decimal%16

hexadecimal += hexadec_carac[decimal%Math.pow(16, i)]
i–
}
return hexadecimal

}

alert(hexadecimer(256))

</script>

<!–fin hexadecimeur –>

If you want to give a hand !

Grand Mamamouch

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@KorSep 22.2004 — Try this:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
function hexCon(f){
v = f.dec.value*1;
w = Math.floor(v/16);
v %= 16;
cha = '0123456789ABCDEF';
f.hex.value=cha.substring(w,w+1)+cha.substring(v,v+1);
}
//-->
</script>
</head>
<body>
<form>
<input type="text" name="dec">
<input type="button" onclick="hexCon(this.form)" value="convert to hexa">
<input type="text" name="hex" value="(hexadecimal)">
</form>
</body>
</html>
[/code]


... use 0-255 decimals to convert then in hexa...
Copy linkTweet thisAlerts:
@KorSep 22.2004 — To convert backwards is easier, using parseInt(hexaval,base) method:

var hex = 'FF';

var dec = parseInt(hex,16);
Copy linkTweet thisAlerts:
@CharlesSep 22.2004 — [font=monospace]<script type="text/javascript">

<!--

alert (Number (255).toString(16))

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@sciguyryanSep 22.2004 — Or, a user made function like this:


<i>
</i>&lt;script type="text/javascript"&gt;
&lt;!--
function toHex(Decimal){
var HexChars = "0123456789ABCDEF";
if (Decimal &gt; 255){
window.alert("Sorry, numbers over 255 are not available in hexdecimal.");
}
else{
var P1 = Decimal % 16;
var P2 = (Decimal - P1) / 16
var Result = HexChars.charAt(P2);
var Result = HexChars.charAt(P1);
window.alert(Decimal + " in hedidecimal is: " + Result);
}
}
//--&gt;
&lt;/script&gt;



Hope that helps,

RyanJ
Copy linkTweet thisAlerts:
@Grand_MamamouchauthorSep 23.2004 — Thanx to all of you !

Dominique
Copy linkTweet thisAlerts:
@sciguyryanSep 23.2004 — [i]Originally posted by Grand Mamamouch [/i]

[B]Thanx to all of you !



Dominique [/B]
[/QUOTE]


No problem, hope I helped a bit ?


RyanJ
×

Success!

Help @Grand_Mamamouch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...