This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[Caml-list] How do I convert 0x hexadecimal integers to decimal integers ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2003-03-28 (13:46) From: Jun Furuse Subject: Re: [Caml-list] How do I convert 0x hexadecimal integers to decimal integers ?
```> > I'd  need a function to turn hexadecimal integers like
> > 	0xb8f99c
> > ( a Color in hexadecimal notation) to integer 3ples (e.g. `RGB
> > (184*256,249*256,156*256), always a color).
> >
> > Shall I use Scanf functions and then some function converting hexadecimal
> > numbers to decimal? what would this function be?

let hex = 0xb8f99c in
let b = hex land 0xff in
let g = (hex lsr 8) land 0xff in
let r = (hex lsr 16) land 0xff in
`RGB (r lsl 8, g lsl 8, b, lsl 8)

BTW, if I prefere the following to get the conversion [0..255] => [0..65535]:

`RGB (r lsl 8 + r, g lsl 8 + g, b lsl 8 + b)

--
jun

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

```