Author |
Message
|
N |
Posted: Tue Feb 19, 2008 12:53 am Post subject: Convert Hexa to Binary |
|
|
Acolyte
Joined: 21 Jul 2007 Posts: 64
|
Hi Guys,
Is this the ideal way to convert hexadecimal to binary?
CREATE PROCEDURE HexToBinary(IN X CHARACTER, OUT Y CHARACTER) BEGIN
-- HEX TO BINARY
IF X=0 THEN
SET Y = '0000';
ELSEIF X=1 THEN
SET Y = '0001';
ELSEIF X=2 THEN
SET Y = '0010';
ELSEIF X=3 THEN
SET Y = '0011';
..............................
ELSEIF X='F' THEN
SET Y = '1111';
END IF; |
|
Back to top |
|
 |
mgk |
Posted: Tue Feb 19, 2008 1:09 am Post subject: |
|
|
 Padawan
Joined: 31 Jul 2003 Posts: 1642
|
Have you tries CASTing it to a BIT datatype? _________________ MGK
The postings I make on this site are my own and don't necessarily represent IBM's positions, strategies or opinions. |
|
Back to top |
|
 |
fjb_saper |
Posted: Tue Feb 19, 2008 3:06 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
Java has a very efficient converter see Integer.decode(string)
 _________________ MQ & Broker admin |
|
Back to top |
|
 |
N |
Posted: Tue Feb 19, 2008 5:41 pm Post subject: |
|
|
Acolyte
Joined: 21 Jul 2007 Posts: 64
|
Hi,
I'd tried BIT data type, however the outcome prefix with B'<binary>'.
Meanwhile, i tried not to use java in my ESQL.
Thanks. |
|
Back to top |
|
 |
fschofer |
Posted: Wed Feb 20, 2008 1:00 am Post subject: |
|
|
 Knight
Joined: 02 Jul 2001 Posts: 524 Location: Mainz, Germany
|
Hi,
why not simply remove the B' at the beginning and the ' at the end ?
Greetings
Frank |
|
Back to top |
|
 |
mgk |
Posted: Wed Feb 20, 2008 2:58 am Post subject: |
|
|
 Padawan
Joined: 31 Jul 2003 Posts: 1642
|
You can try some ESQL like the following:
Code: |
DECLARE input CHAR 'F'; /* value = F */
SET input = '0x' || input; /* value = 0xF */
DECLARE tmpInt1 INT CAST( input AS INT); /* value = 15 */
DECLARE binStr BIT CAST( tmpInt1 AS BIT ); /*value = B'0000000000000000000000000000000000000000000000000000000000001111' */
DECLARE output CHAR CAST( binStr AS CHAR); /*value = 'B'0000000000000000000000000000000000000000000000000000000000001111'' */
SET output = TRIM( LEADING 'B' FROM output ); /*value = ''0000000000000000000000000000000000000000000000000000000000001111'' */
SET output = TRIM( BOTH '''' FROM output ); /*value = '0000000000000000000000000000000000000000000000000000000000001111' */
SET output = TRIM( LEADING '0' FROM output ); /*value = '1111' */ |
Regards, _________________ MGK
The postings I make on this site are my own and don't necessarily represent IBM's positions, strategies or opinions. |
|
Back to top |
|
 |
N |
Posted: Wed Feb 20, 2008 11:37 pm Post subject: |
|
|
Acolyte
Joined: 21 Jul 2007 Posts: 64
|
HI,
Thanks for your snippet. Does this code cater for Z/OS EBCDIC and WIN32 ASCII? |
|
Back to top |
|
 |
fjb_saper |
Posted: Thu Feb 21, 2008 2:28 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
N wrote: |
HI,
Thanks for your snippet. Does this code cater for Z/OS EBCDIC and WIN32 ASCII? |
Hex is hex and does not care as long as you keep it in a byte array.
Now if you are talking about CHAR (as in char representation of the hex value of the byte array) you'll have to use CAST to move from one CCSID to the other, or a GET with Translate.
Enjoy  _________________ MQ & Broker admin |
|
Back to top |
|
 |
|