Home » SQL & PL/SQL » SQL & PL/SQL » conversion to 16bit decimal (oracle 11gr2)
conversion to 16bit decimal [message #657142] Fri, 28 October 2016 08:34 Go to next message
guddu_12
Messages: 208
Registered: April 2012
Location: UK
Senior Member
Dear Gurus,

I have to convert 32 bit hexadecimal value to 16 bit decimal

e.g

convert hexadeciaml 'FFFD' TO decimal 32 is 65533, i want to convert this 32 bit decimal to 16 bit decimal which should return 3.

Kindly let me know if there any function or procedure or any formula.

Thanks
Re: conversion to 16bit decimal [message #657144 is a reply to message #657142] Fri, 28 October 2016 08:50 Go to previous messageGo to next message
BlackSwan
Messages: 26204
Registered: January 2009
Location: SoCal
Senior Member
"16 bit decimal" is an oxymoron & does not exist.
"32 bit decimal" is an oxymoron & does not exist.
A decimal integer is a base 10 value & does NOT contain any "bits" (binary digit).

What problem are you really trying to solve & how & why is Oracle involved?

Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read


Re: conversion to 16bit decimal [message #657150 is a reply to message #657142] Fri, 28 October 2016 09:15 Go to previous messageGo to next message
Michel Cadot
Messages: 65848
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
convert hexadeciaml 'FFFD' TO decimal 32 is 65533, i want to convert this 32 bit decimal to 16 bit decimal which should return 3.
Why 3 and not 5?

Note that hexadecimal 'FFFD' is a 16 bits figure, so the whole sentence is pointless.

Re: conversion to 16bit decimal [message #657151 is a reply to message #657144] Fri, 28 October 2016 09:17 Go to previous messageGo to next message
guddu_12
Messages: 208
Registered: April 2012
Location: UK
Senior Member
Hi Swan,

We have a electronic system which logs the information into file in hexadecimal, we have to decode the file and read the converted value in 16 bit.
I am able to convert into 32 bit but not sure how to convert into 16 bit. there are website where one can input hexadecimal value and can return 16 bit decimal value to see the converted value.

I have a function which can convert hexadecimal to decimal but in 32 bit.

I gave a example of hexadecimal 'FFFD' TO 65553 DECIMAL . i want to know the formula if any to convert it to 16 bit decimal
Re: conversion to 16bit decimal [message #657152 is a reply to message #657150] Fri, 28 October 2016 09:27 Go to previous messageGo to next message
guddu_12
Messages: 208
Registered: April 2012
Location: UK
Senior Member
hI

Please use below link
https://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html

input FFFD IN hexadecimal text box and select binary type signed 16 bit form drop down.
Re: conversion to 16bit decimal [message #657154 is a reply to message #657151] Fri, 28 October 2016 09:42 Go to previous messageGo to next message
Michel Cadot
Messages: 65848
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
I have a function which can convert hexadecimal to decimal but in 32 bit.
What does this mean, you convert hexadecimal to decimal, that's all, number of bits is irrelevant.

Say you have "123ABD" hexadecimal, what is the value for "16 bit decimal" as you say?

You are unclear with your inputs and outputs.
Are the inputs all 16-bits hexadecimal values that is all string values between '0000' and 'FFFF' with always 4 characters from '0' to '9' and 'A' to 'F', or can there be other input values?

Re: conversion to 16bit decimal [message #657155 is a reply to message #657154] Fri, 28 October 2016 09:52 Go to previous messageGo to next message
guddu_12
Messages: 208
Registered: April 2012
Location: UK
Senior Member
Hi Michael,

The input will be always 16bit hexadecimal that is between '0000' and 'FFFF'.

In my last post where i have mentioned that i have a function which converts hexadecimal to decimal 32 bit , so i thought the 32 bit decimal now can be converted to 16 bit.

Please ignore the conversation regarding function which converts.

I would always get 16 bit hexadecimal ('0000' and 'FFFF') value and i need to convert it to 16 bit decimal.
Re: conversion to 16bit decimal [message #657158 is a reply to message #657155] Fri, 28 October 2016 09:58 Go to previous messageGo to next message
Michel Cadot
Messages: 65848
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Use TO_NUMBER with format mask 'XXXX'.

Re: conversion to 16bit decimal [message #657159 is a reply to message #657158] Fri, 28 October 2016 10:08 Go to previous messageGo to next message
guddu_12
Messages: 208
Registered: April 2012
Location: UK
Senior Member
Hi

How to_number will convert hexadecimal to decimal value. My input is hexadecimal 16 bit and desire output is 16 bit decimal
Re: conversion to 16bit decimal [message #657160 is a reply to message #657159] Fri, 28 October 2016 10:10 Go to previous messageGo to next message
Michel Cadot
Messages: 65848
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Explain what is "16 bit decimal".

Re: conversion to 16bit decimal [message #657161 is a reply to message #657159] Fri, 28 October 2016 10:29 Go to previous messageGo to next message
guddu_12
Messages: 208
Registered: April 2012
Location: UK
Senior Member
Hi Michael,

I might be putting my question in confusing ways

all i want is

32 bit binary representation (1111111111111101) hexadecimal is FFFD decimal representation is 65533.
16 bit binary representation (1111111111111101) hexadecimal value is FFFD decimal representation is -3.

SO I want to convert any hexadecimal value to deicmal in 16 bit binary representation as shown in example above. If you look at the above example you will see that for same binary value in different (32 bit or 16 bit) the hexadecimal is same but decimal value is changing. I want to know how to get that value.
Re: conversion to 16bit decimal [message #657162 is a reply to message #657161] Fri, 28 October 2016 11:20 Go to previous messageGo to next message
Michel Cadot
Messages: 65848
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Ah, you mean 16-bit SIGNED INTEGER.
It is just the unsigned one minus 65536 if the unsigned integer >= 32768.


Re: conversion to 16bit decimal [message #657163 is a reply to message #657161] Fri, 28 October 2016 11:33 Go to previous messageGo to next message
Solomon Yakobson
Messages: 2806
Registered: January 2010
Location: Connecticut, USA
Senior Member
Oracle doesn't provide a built-in function for that. You need to write your own. Something like:

SQL> CREATE OR REPLACE
  2    PACKAGE HEX
  3      IS
  4        FUNCTION TO_BIT(
  5                        P_HEX VARCHAR2
  6                       )
  7      RETURN VARCHAR2;
  8  END;
  9  /

Package created.

SQL> CREATE OR REPLACE
  2    PACKAGE BODY HEX
  3      IS
  4        TYPE TETRADE_TYPE
  5            IS
  6              TABLE OF VARCHAR2(4)
  7                INDEX BY VARCHAR2(1);
  8        G_TETRADES TETRADE_TYPE;
  9        FUNCTION TO_BIT(
 10                        P_HEX VARCHAR2
 11                       )
 12          RETURN VARCHAR2
 13          IS
 14          BEGIN
 15              IF LENGTH(P_HEX) IS NULL
 16                THEN
 17                  RETURN NULL;
 18                ELSE
 19                  RETURN G_TETRADES(SUBSTR(P_HEX,1,1)) || TO_BIT(SUBSTR(P_HEX,2));
 20              END IF;
 21        END;
 22      BEGIN
 23          G_TETRADES('0') := '0000';
 24          G_TETRADES('1') := '0001';
 25          G_TETRADES('2') := '0010';
 26          G_TETRADES('3') := '0011';
 27          G_TETRADES('4') := '0100';
 28          G_TETRADES('5') := '0101';
 29          G_TETRADES('6') := '0110';
 30          G_TETRADES('7') := '0111';
 31          G_TETRADES('8') := '1000';
 32          G_TETRADES('9') := '1001';
 33          G_TETRADES('A') := '1010';
 34          G_TETRADES('B') := '1011';
 35          G_TETRADES('C') := '1100';
 36          G_TETRADES('D') := '1101';
 37          G_TETRADES('E') := '1110';
 38          G_TETRADES('F') := '1111';
 39          G_TETRADES('a') := '1010';
 40          G_TETRADES('b') := '1011';
 41          G_TETRADES('c') := '1100';
 42          G_TETRADES('d') := '1101';
 43          G_TETRADES('e') := '1110';
 44          G_TETRADES('f') := '1111';
 45          G_TETRADES(' ') := NULL;
 46  END;
 47  /

Package body created.

SQL> COLUMN BITS FORMAT A16
SQL> SELECT  65533 DEC,
  2          TO_CHAR(65533,'XXXX') HEX,
  3          HEX.TO_BIT(TO_CHAR(65533,'XXXX')) BITS
  4    FROM  DUAL
  5  /

       DEC HEX   BITS
---------- ----- ----------------
     65533  FFFD 1111111111111101

SQL> 

SY.
Re: conversion to 16bit decimal [message #657165 is a reply to message #657163] Fri, 28 October 2016 11:46 Go to previous messageGo to next message
Michel Cadot
Messages: 65848
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Er, I think the input is 'FFFD' and the output should be -3, not the bit representation of 'FFFD'.
Like:
SQL> with data as (select 'FFFD' in_val from dual union all select '0FFF' from dual)
  2  select in_val,
  3         case when to_number(in_val,'XXXX') < 32768 then to_number(in_val,'XXXX')
  4              else to_number(in_val,'XXXX')-65536
  5         end out_val
  6  from data
  7  /
IN_V    OUT_VAL
---- ----------
FFFD         -3
0FFF       4095
Re: conversion to 16bit decimal [message #657170 is a reply to message #657165] Fri, 28 October 2016 11:53 Go to previous messageGo to next message
guddu_12
Messages: 208
Registered: April 2012
Location: UK
Senior Member
Hi

What 'xxxx' format does

Please let me know
Re: conversion to 16bit decimal [message #657172 is a reply to message #657170] Fri, 28 October 2016 11:58 Go to previous messageGo to next message
Michel Cadot
Messages: 65848
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Read the links I gave you.

Re: conversion to 16bit decimal [message #657374 is a reply to message #657172] Mon, 07 November 2016 10:09 Go to previous message
guddu_12
Messages: 208
Registered: April 2012
Location: UK
Senior Member
Hi Michael,

to_number with x format is the simplest way to convert which i was unaware

Thanks for the help and we are using this in our project instead of using funtion to convert hex to decimal.
Previous Topic: Conditional based default value
Next Topic: Procedure execution issue and trace
Goto Forum:
  


Current Time: Sat Oct 20 22:41:42 CDT 2018