ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » convert alphanumeric to int

Post new topic  Reply to topic
 convert alphanumeric to int « View previous topic :: View next topic » 
Author Message
scravr
PostPosted: Wed Apr 25, 2012 11:15 am    Post subject: convert alphanumeric to int Reply with quote

Partisan

Joined: 03 Apr 2003
Posts: 388
Location: NY NY USA 10021

Does anyone have a quick magic way to convert ESQL alphanumeric CHAR to INT?

Something like:

DECLARE mychar CHAR ’abcABC123’;
DECLARE myint INT;

SET myint=SomeMagic(mychar);


Can BASE64ENCODE, BASE64DECODE, ASBITSTREAM, or other functions can be used?
Back to top
View user's profile Send private message Send e-mail MSN Messenger
mqjeff
PostPosted: Wed Apr 25, 2012 11:27 am    Post subject: Re: convert alphanumeric to int Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

scravr wrote:
Does anyone have a quick magic way to convert ESQL alphanumeric CHAR to INT?

So is "onetwothreefour" a valid alphanumeric string that can be converted to an INT?

scravr wrote:
Can BASE64ENCODE, BASE64DECODE, ASBITSTREAM, or other functions can be used?

Do those functions have any bearing on your requirement?
have you thought about the actual problem you're looking to solve?

Is it the same as the problem of removing characters that do not represent valid numeric values from a string? Are there ESQL functions that help you alter strings?
Back to top
View user's profile Send private message
Vitor
PostPosted: Wed Apr 25, 2012 11:53 am    Post subject: Re: convert alphanumeric to int Reply with quote

Grand High Poobah

Joined: 11 Nov 2005
Posts: 26093
Location: Texas, USA

scravr wrote:
Does anyone have a quick magic way to convert ESQL alphanumeric CHAR to INT?

Something like:

DECLARE mychar CHAR ’abcABC123’;
DECLARE myint INT;

SET myint=SomeMagic(mychar);


So in this example you want 123? What about 'abcABC123xxx456'? Is that 123, 456 or 123456?

You can do it in 1 line of ESQL, if you're ingenious.

scravr wrote:
Can BASE64ENCODE, BASE64DECODE, ASBITSTREAM, or other functions can be used?


IHMO 3 functions that convert from 1 format to another with no transformational aspect are unlikely to be of assistance to you. I'm uncertain how you picked on these...
_________________
Honesty is the best policy.
Insanity is the best defence.
Back to top
View user's profile Send private message
scravr
PostPosted: Wed Apr 25, 2012 12:42 pm    Post subject: Reply with quote

Partisan

Joined: 03 Apr 2003
Posts: 388
Location: NY NY USA 10021

I am trying to get a numeric value for an alphanumeric string.
Something like this java function:

Int HashValue = (long)0;
for ( int i = 0; i < CharString.length(); ++i )
{
char c = CharString.charAt(i);
HashValue = HashValue + (long)c;
}
Back to top
View user's profile Send private message Send e-mail MSN Messenger
kimbert
PostPosted: Thu Apr 26, 2012 1:11 am    Post subject: Reply with quote

Jedi Council

Joined: 29 Jul 2003
Posts: 5542
Location: Southampton

Try the CAST function. Please look up the details in the info center.
Back to top
View user's profile Send private message
McueMart
PostPosted: Thu Apr 26, 2012 1:38 am    Post subject: Reply with quote

Chevalier

Joined: 29 Nov 2011
Posts: 490
Location: UK...somewhere

Here's a quick attempt. Someone will be along shortly no doubt to do it in a single line!

Code:

   DECLARE zeroPadBLOB CONSTANT BLOB X'00000000000000';
   CREATE FUNCTION SomeMagic(IN mychar CHAR) RETURNS INT BEGIN
      DECLARE count INTEGER 1;
      DECLARE l INTEGER LENGTH(mychar);
      DECLARE ret INTEGER 0;
      WHILE count <= l DO
         SET ret = ret + CAST(zeroPadBLOB || CAST(SUBSTRING(mychar FROM count FOR 1) AS BLOB CCSID 1208) AS INTEGER);
         SET count = count+1;
      END WHILE;
      RETURN ret;
   END;


p.s. It seems in ESQL an integer is represented with 8 bytes... that took me off guard; but makes sense seeing as there is no 'long' type.
Back to top
View user's profile Send private message
mqjeff
PostPosted: Thu Apr 26, 2012 6:22 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

McueMart wrote:
Someone will be along shortly no doubt to do it in a single line!

Wow, you took the long way around to get there. There's an example floating around somewhere in the archives using translate. or maybe it was replace?
Back to top
View user's profile Send private message
Esa
PostPosted: Thu Apr 26, 2012 6:39 am    Post subject: Reply with quote

Grand Master

Joined: 22 May 2008
Posts: 1387
Location: Finland

mqjeff wrote:
McueMart wrote:
Someone will be along shortly no doubt to do it in a single line!

Wow, you took the long way around to get there. There's an example floating around somewhere in the archives using translate. or maybe it was replace?


No, the code is creating a numeric hashcode by summing up hexadecimal values of all characters in a string. I don't think you can do it with translate or replace.

The value will not be unique, because 'ABCabc123' will produce the same number as '321cbaCBA' and any other combination of the same characters. But perhaps uniqueness is not required here...
Back to top
View user's profile Send private message
mqjeff
PostPosted: Thu Apr 26, 2012 6:49 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

Esa wrote:
No, the code is creating a numeric hashcode by summing up hexadecimal values of all characters in a string.

oh yeah, lookitthat.
ret is an INTEGER, not a CHAR.
Back to top
View user's profile Send private message
McueMart
PostPosted: Thu Apr 26, 2012 6:52 am    Post subject: Reply with quote

Chevalier

Joined: 29 Nov 2011
Posts: 490
Location: UK...somewhere

Yes i was struggling to see how it could be done with translate (and trying to search,unsuccessfully, for what mqjeff was referring to). Agree with Esa that this is a poor method of 'hashing' a character string.
Back to top
View user's profile Send private message
mqjeff
PostPosted: Thu Apr 26, 2012 6:59 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

There's an example somewhere that uses one of the string functions to remove everything that's a legitimate integer character, 0-9 from a string, and then uses the result as the input to remove everything left over from the original string.

You're then left with a string that only holds characters that represent 0-9 values, which you can cast to an integer.
Back to top
View user's profile Send private message
Esa
PostPosted: Thu Apr 26, 2012 7:11 am    Post subject: Reply with quote

Grand Master

Joined: 22 May 2008
Posts: 1387
Location: Finland

I think you can make the hashcode more unique for example by modifying the function so that it produces a temporary sum of every ten characters, then calculates modulus 11 of it, multiplies it with 1000 * (10 exp n) where n is incremented after each group, and adds that to the hashcode. You just need to take care of stopping before ESQL integer flows over.
Back to top
View user's profile Send private message
scravr
PostPosted: Thu Apr 26, 2012 8:55 am    Post subject: Reply with quote

Partisan

Joined: 03 Apr 2003
Posts: 388
Location: NY NY USA 10021

you all are getting very close.

I need a function returning a unique INT for a given STRING (with all in it; not only the numeric chars).

So
when calling func with "abc123", I need return int of (let's say) 500.
when calling func with "xyz", I need return int of (let's say) 800.
when calling func with "a1", I need return int of (let's say) 400.

when calling func with "xyz", I need the same retun int of 800.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
lancelotlinc
PostPosted: Thu Apr 26, 2012 9:30 am    Post subject: Reply with quote

Jedi Knight

Joined: 22 Mar 2010
Posts: 4941
Location: Bloomington, IL USA

If its too difficult in ESQL, you could always call a Java function to do same.
_________________
http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER
Back to top
View user's profile Send private message Send e-mail
kimbert
PostPosted: Thu Apr 26, 2012 12:14 pm    Post subject: Reply with quote

Jedi Council

Joined: 29 Jul 2003
Posts: 5542
Location: Southampton

Quote:
I need a function returning a unique INT for a given STRING (with all in it; not only the numeric chars).
There's a name for that. It's called a 'perfect hash'. See http://en.wikipedia.org/wiki/Perfect_hash_function

I hope you know the full set of strings that you want to hash - otherwise it is going to be tricky to come up with a perfect hash function

There must be loads of implementations of string hashing functions out there in the world of Java. If you require a *perfect* hash, then I suspect it's going to be tricky doing it in ESQL. A rough-and-ready non-perfect hash is easy, and you've already been given a couple of ideas for those. There are others, if you Google for them.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » convert alphanumeric to int
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.