Discussion:
Decimal to Hexadecimal - But not in the way you think!
ka1gt
2018-11-15 19:05:11 UTC
Permalink
For reasons I won't go into (another application), I need to convert a
decimal number into a hexadecimal number - but not in the usual way.

As an example I need to convert the integer "*14*" into the hexadecimal
number "0x*14*"

Is there a simple way to do this? I thought of number -> string -> number,
but there's no string to hex conversion as far as I can see.

I could do it by taking the integer "14", splitting it up into 1 and 4,
taking (1*16)+(4) = 20 and then converting 20 to a hex number which gives me
"0x14", but is there a simpler way to do this that I'm missing?



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Richardson, Anthony
2018-11-15 21:46:36 UTC
Permalink
Subject: Decimal to Hexadecimal - But not in the way you think!
For reasons I won't go into (another application), I need to convert a decimal
number into a hexadecimal number - but not in the usual way.
As an example I need to convert the integer "*14*" into the hexadecimal
number "0x*14*"
Is there a simple way to do this? I thought of number -> string -> number,
but there's no string to hex conversion as far as I can see.
I could do it by taking the integer "14", splitting it up into 1 and 4, taking
(1*16)+(4) = 20 and then converting 20 to a hex number which gives me
"0x14", but is there a simpler way to do this that I'm missing?
n = sscanf(num2str(14), "x")
N = 20

Tony Richardson
Ian McCallion
2018-11-15 21:47:20 UTC
Permalink
Is this what you want?

a= dec2base(14,10)
base2dec(a,16)

Cheers... Ian
Post by ka1gt
For reasons I won't go into (another application), I need to convert a
decimal number into a hexadecimal number - but not in the usual way.
As an example I need to convert the integer "*14*" into the hexadecimal
number "0x*14*"
Is there a simple way to do this? I thought of number -> string -> number,
but there's no string to hex conversion as far as I can see.
I could do it by taking the integer "14", splitting it up into 1 and 4,
taking (1*16)+(4) = 20 and then converting 20 to a hex number which gives me
"0x14", but is there a simpler way to do this that I'm missing?
--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Loading...