Advertisement

03.26.2008 at 07:47AM PDT, ID: 23270567
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Convert a base 60 ID stored as string back to int
Tags: C++, string
Hi,

See the attached function. I would like to have another function that will reverse the process - it would ignore the first nInitials chars, and then compute the correct int value of the remaining string, 0's being ignored (place holders).

The function below takes a string, a numeric value and a length and returns a string assembled with them. So if I will pass "DK", 3, and 8 it will return "DK000003", and for "DK", 10, and 8 "DK00000a".

I would want to avoid the use of STL/MFC.

Stilgar.
1:
2:
3:
4:
5:
6:
7:
8:
9:
LPCTSTR GenerateArticleID(LPCTSTR pInitial,DWORD dwNum, DWORD dwSize, LPTSTR pBuf) {
		TCHAR buf[64];
		DWORD dwPad = dwSize - _tcslen(pInitial);
		_itot(dwNum,buf,36);
		DWORD dwLen = (DWORD)_tcslen(buf);
		if (dwLen + _tcslen(pInitial) > 8) return NULL;
		_stprintf(pBuf,_T("%s%0*.*s"),pInitial,dwPad,dwPad,buf);
		return pBuf;
	}
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: _Stilgar_
Solution Provided By: itsmeandnobodyelse
Participating Experts: 1
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
03.26.2008 at 09:27AM PDT, ID: 21213380

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.26.2008 at 09:33AM PDT, ID: 21213462

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.26.2008 at 10:19AM PDT, ID: 21214024

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.26.2008 at 01:37PM PDT, ID: 21215938

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 12:51AM PDT, ID: 21219301

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 02:47AM PDT, ID: 21219741

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 02:52AM PDT, ID: 21219760

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 04:32AM PDT, ID: 21220222

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 08:35AM PDT, ID: 21222505

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 10:40AM PDT, ID: 21223962

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 10:52AM PDT, ID: 21224088

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 10:58AM PDT, ID: 21224154

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 11:06AM PDT, ID: 21224222

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.26.2008 at 09:27AM PDT, ID: 21213380

Rank: Genius

You may take the below. It compiles but I didn't tested it.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
bool getArticleNumAndSizeFromId(LPCTSTR szArticleId, 
                                LPTSTR szArticle, DWORD dwArticleSiz, 
                                DWORD & dwNum, DWORD & dwSiz)
{
   dwSiz        = _tcslen(szArticleId);
   DWORD dwOff  = _tcscspn(szArticleId, "0123456789abcdef");
   if (dwOff == 0 || dwOff >= dwSiz) 
         return false;   // no hex digits
   char* sentinel = NULL;
   dwNum = _tcstol(&szArticleId[dwOff], &sentinel, 16);
   if (sentinel == NULL || *sentinel != '\0') 
      return false;  // non-digits stopped conversion
   if (dwArticleSiz <= dwOff)
      return false;  // buffer given is too small
   _tcsncpy(szArticle, szArticleId, dwOff);
   szArticle[dwOff] = TCHAR(0);
   return true;
}
Open in New Window
 
03.26.2008 at 09:33AM PDT, ID: 21213462
Err... This is not a base 60 code (my bad) but rather base 36 (10 digits + 26 letters). My intention is to have g-z computed as well. So this is not a Hex value as well....

Will this work for my case?

Stilgar.
 
03.26.2008 at 10:19AM PDT, ID: 21214024

Rank: Genius

>>>> Err... This is not a base 60 code (my bad)
No, it was my fault. I didn't recognize the base60.

 
You may use the following code

 ...
 dwSiz        = _tcslen(szArticleId);
 LPTSTR ptsz = _tcschr(szArticleId, TCHAR('0'));
 if (ptsz == NULL) return false;
 DWORD dwOff  = (DWORD)(ptsz - szArticleId);
 if (dwOff == 0 || dwOff >= dwSiz)
      return false;   // no '0' character
   char* sentinel = NULL;
 _tcstol(&szArticleId[dwOff], &sentinel, 36);
 ...

though I actually never used a base greater 16.

Note, checking for a range of 36 'digits' doesn't make so much sense as the article code is alpha as well. Hence I checked for a '0' char only cause you can't restore the tokens if not at least one '0' was inserted as a filler.

 
 
03.26.2008 at 01:37PM PDT, ID: 21215938
I'm not sure why you are looking for a 0? What if I have a code of length 3, which has 2 letters as initials, so it looks like "DKc" ? What I need is to pass an int value which will be used to skip over the initials (I will normally pass 2, but this might change), and then the code should skip all 0's until the first alphanumeric char, and then begin conversion.

Also, can you please post the full function? I'm not sure where to paste the above (not that noob, just not to concentrated atm...).

Thanks.
 
03.27.2008 at 12:51AM PDT, ID: 21219301

Rank: Genius

>>>>> I'm not sure why you are looking for a 0?
If both the 'number' and the prefix consist of alphabetic letters, how will you decide where the one begins and the other ends?

If the base 36 digits were small letters only (is it?) and the prefix only has capital letters we could go for that?
 
03.27.2008 at 02:47AM PDT, ID: 21219741
>>>> If both the 'number' and the prefix consist of alphabetic letters, how will you decide where the one begins and the other ends?

By using nInitials. If >0, then skip the nInitials first letters and start converting from there. I will always know how many initials were used, by definition (and even if I didn't, I could do the scan for the first 0 on my own before calling this function).
Anyway, the article ID is case insensitive.

I hope this makes sense to you now. If not please let me know.

Stilgar.
 
03.27.2008 at 02:52AM PDT, ID: 21219760
The function signature should be something like:

int GetArticleNumber (int nInitials, LPCTSTR szArticleID)

szArticleID is the string holding the ID, nInitials would be the number of characters to skip (the initials) in the string.

Stilgar.
 
03.27.2008 at 04:32AM PDT, ID: 21220222

Rank: Genius

int GetArticleNumber (int nInitials, LPCTSTR szArticleID)
{
   char* sentinel = NULL;
   int num = (int)_tcstol(&szArticleID[nInitials], &sentinel, 36);
   if (sentinel == NULL || *sentinel != TCHAR('\0'))
      return -1;  // non-digits stopped conversion
   return num;
}
Accepted Solution
 
03.27.2008 at 08:35AM PDT, ID: 21222505
Thanks, that seems to do the job (still testing). How could the sentinel == NULL after _tcstol (it should be TCHAR* btw)?

Stilgar.
 
03.27.2008 at 10:40AM PDT, ID: 21223962

Rank: Genius

>>>> How could the sentinel == NULL
It shouldn't. But I generally was testing pointers returned by functions on NULL before accessing them. In case of a NULL pointer dereferencing by *sentinel would cause an access violation.
 
03.27.2008 at 10:52AM PDT, ID: 21224088
In that case, you should have made the if statement to be a two-stage check; in its current form it will AV as well if the pointer was NULL...

Sorry about digging into this, but I'm new to _tcstol :)

Stilgar.
 
03.27.2008 at 10:58AM PDT, ID: 21224154

Rank: Genius

>>>> in its current form it will AV as well

No, an if condition is guaranteed to be processed from left to right. If the left expression of a logical or (||) is true, the right expression will not be evaluated.
 
03.27.2008 at 11:06AM PDT, ID: 21224222

Rank: Genius

>>>> but I'm new to _tcstol :)
The _tcstol is the "T" version of strtol, i. e. it either was mapped to strtol in case of ANSI strings or to wcstol in case of UNICODE strings.

strtol - contrary to atoi - can convert numbers of different bases *and* checks for the character that terminates conversion. The sentinel would point to the first position within the char array passed in which wasn't a valid digit (within the range of digits defined by the base given). That should be the terminating zero character or something was wrong.
 
 
20080236-EE-VQP-29 / EE_QW_EXPERT_20070906