Home » SQL & PL/SQL » SQL & PL/SQL » conversion to 16bit decimal (oracle 11gr2)
conversion to 16bit decimal Fri, 28 October 2016 08:34
 guddu_12 Messages: 208Registered: 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
 BlackSwan Messages: 26204Registered: 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?

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
 Michel Cadot Messages: 65848Registered: March 2007 Location: Nanterre, France, http://... Senior MemberAccount 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
 guddu_12 Messages: 208Registered: 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
 guddu_12 Messages: 208Registered: April 2012 Location: UK Senior Member
hI

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
 Michel Cadot Messages: 65848Registered: March 2007 Location: Nanterre, France, http://... Senior MemberAccount 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
 guddu_12 Messages: 208Registered: 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
 Michel Cadot Messages: 65848Registered: March 2007 Location: Nanterre, France, http://... Senior MemberAccount 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
 guddu_12 Messages: 208Registered: 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
 Michel Cadot Messages: 65848Registered: March 2007 Location: Nanterre, France, http://... Senior MemberAccount 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
 guddu_12 Messages: 208Registered: 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
 Michel Cadot Messages: 65848Registered: March 2007 Location: Nanterre, France, http://... Senior MemberAccount 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
 Solomon Yakobson Messages: 2806Registered: 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
5            IS
6              TABLE OF VARCHAR2(4)
7                INDEX BY VARCHAR2(1);
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
20              END IF;
21        END;
22      BEGIN
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
 Michel Cadot Messages: 65848Registered: March 2007 Location: Nanterre, France, http://... Senior MemberAccount 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
 guddu_12 Messages: 208Registered: April 2012 Location: UK Senior Member
Hi

What 'xxxx' format does

Re: conversion to 16bit decimal [message #657172 is a reply to message #657170] Fri, 28 October 2016 11:58
 Michel Cadot Messages: 65848Registered: March 2007 Location: Nanterre, France, http://... Senior MemberAccount Moderator

Re: conversion to 16bit decimal [message #657374 is a reply to message #657172] Mon, 07 November 2016 10:09
 guddu_12 Messages: 208Registered: 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