Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Hexadecimal
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Division-remainder in source base=== As with all bases there is a simple [[algorithm]] for converting a representation of a number to hexadecimal by doing integer division and remainder operations in the source base. In theory, this is possible from any base, but for most humans, only decimal and for most computers, only binary (which can be converted by far more efficient methods) can be easily handled with this method. Let d be the number to represent in hexadecimal, and the series h<sub>i</sub>h<sub>iβ1</sub>...h<sub>2</sub>h<sub>1</sub> be the hexadecimal digits representing the number. # i β 1 # h<sub>i</sub> β d mod 16 # d β (d β h<sub>i</sub>) / 16 # If d = 0 (return series h<sub>i</sub>) else increment i and go to step 2 "16" may be replaced with any other base that may be desired. The following is a [[JavaScript]] implementation of the above algorithm for converting any number to a hexadecimal in String representation. Its purpose is to illustrate the above algorithm. To work with data seriously, however, it is much more advisable to work with [[bitwise operators]]. <syntaxhighlight lang="javascript"> function toHex(d) { var r = d % 16; if (d - r == 0) { return toChar(r); } return toHex((d - r) / 16) + toChar(r); } function toChar(n) { const alpha = "0123456789ABCDEF"; return alpha.charAt(n); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)