Solved

integer represenation of ASCii to char for XML web service

Posted on 2004-08-26
18
314 Views
Last Modified: 2013-11-20
I have been stuck on this for a day. What I am trying to do used to be simple. I want to take an int and convert it to it's ASCii character value. This is to be returned from a web service in xml.

char RGeneratorClass::GetChar(int c)
{

      if(c < 10)
            return (c + 48);
      else
            return (c + 55);
            
}
0
Comment
Question by:MichaelSFuller
  • 9
  • 5
  • 4
18 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11905431
Are you thinking of remodelling the functionality of

int __toascii( int c );

?
0
 
LVL 86

Expert Comment

by:jkr
ID: 11905467
Ooops, my fault, you're doing something different.

Shoudln't that be

char RGeneratorClass::GetChar(int c)
{

    if(c < 10)
         return (c + 48);
    else
         return 0;
}

You cannot represent a 2-digit number in a single char. Or are you thinking about

char* RGeneratorClass::GetChar(int c)
{
   static char ac [ 12];

    wsprintf ( ac, "%d", i);

   return ac;
}

?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11905574
There are many situations where static buffer is not recommended. It would be better:

char* RGeneratorClass::GetChar(int c, char *buffer)
{
    sprintf ( buffer, "%d", i);

   return buffer;
}

Of course you have to reserve proper space for buffer before calling GetChar()
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 86

Expert Comment

by:jkr
ID: 11905605
>> There are many situations where static buffer is not recommended

There are as many situations where it is useful, and even more where passing a buffer without specifying it's length is even more dangerous.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11905623
Look at this recent discussion, it is very similar to yours. As you will see, there are many possible answers.
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21105715.html
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11905649
jkr, you know that GetChar can't be called twice because you will overwrite the 'ac' buffer. At least you have to advice to Michael.
Using your code:

char *value1, *value2;
value1 = obj.GetChar(5);
value2 = obj.GetChar(7);
printf ("%s %s", value1, value2);

Will return 7 7, not 5 7 as expected.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11905715
The whole discussion is *pointless* whithout knowing in what context it is used:

CString value1, value2;
value1 = obj.GetChar(5);
value2 = obj.GetChar(7);

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11905769
The fact is that this is not the best solution among all the possibilities. Just a capricious preference.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11905786
About the "context", user can re-use the code in a different context without noticing the problem.
Also, other EE members can read the answer and assume it always will work.
0
 
LVL 5

Author Comment

by:MichaelSFuller
ID: 11905974
Sorry I was at lunch. What I am doing is generating number on a 36 base. For this method I was converting the decimal into 36 base. The caller of the web service needs a key. Here is some of the code:

//Calling function
String* RGeneratorClass::LongToBase(long lPassed, int iBase,int iKeyLength )
    {
      //This just for testing
            long lBasePow[] = new long[iKeyLength];
            long lBase __gc[]=new long __gc[5];
            lBase[0] =1679616;
            lBase[1] =46656;
            lBase[2] =1296;
            lBase[3] =36;
            lBase[4] =1;
      
      return Counter(lPassed,5,0,lBase);
      
      
     }

      char RGeneratorClass::GetChar(int c)
{
      
      if(c < 10)
            return (c + 48);
      else
            return (c + 55);
            
}


      String* RGeneratorClass::Counter(long lAccum, int iSizeof,int iPos, long lBasePow __gc[])
      {

      String* s;
      StringBuilder* sb = new StringBuilder();
      
      
      if(iPos < iSizeof) //Make sure value is in bounds
            sb = sb->Append(GetChar((lAccum /(lBasePow[iPos]))))->Append(Counter((lAccum % lBasePow[iPos]), iSizeof, iPos++, lBasePow));
      
      s=sb->ToString();

      return s; //stop
      }
0
 
LVL 86

Expert Comment

by:jkr
ID: 11906038
You mean a numeric base of 36? You can do that using

char *_itoa( int value, char *string, int radix );
0
 
LVL 86

Expert Comment

by:jkr
ID: 11906046
E.g.

   char buffer[20];
   int  i = 5;

   _itoa( i, buffer, 36 );
   printf( "String of integer %d (radix 36): %s\n", i, buffer );
0
 
LVL 5

Author Comment

by:MichaelSFuller
ID: 11906116
Yes numeric base 36, from 00001-ZZZZZ. I just want to return from the GetChar function which I pass the integer value of an ASCii character the character not the number. For some reason I keep getting the number back in XML. It used to be easy to do but in .Net 2003 nothing seems to be availible anymore. I'm not sure if the problem is because of unicode or what, but I've tried just about everything.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11906165
Well, if '_itoa()' is still available in .NET, it should be easy, just like the above lines.
0
 
LVL 5

Author Comment

by:MichaelSFuller
ID: 11906183
Verifying right now.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11906227
According to http://support.microsoft.com/default.aspx?scid=kb;en-us;313836 ("INFO: Map the C Run-Time (CRT) Library Functions to .NET Functions"), there's at least an equivalent:

_itoa, _i64toa, _itow, _i64tow   System.Convert.ToString
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 11906312
Yup, here ist is: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemconvertclasstostringtopic27.asp ("Convert.ToString Method (Int32, Int32)"):

public: static String* ToString(
   int value,
   int toBase
);

// Example of the Convert::ToString( int, int ) method.
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void RunToStringDemo( )
{
    int values __gc [ ] = {
        Int32::MinValue,
        -100,
        0,
        99999,
        Int32::MaxValue };
    int  radices[ 4 ] = { 2, 8, 10, 16 };

    // Implement foreach( int value in values ).
    IEnumerator*    myEnum = values->GetEnumerator( );
    while( myEnum->MoveNext( ) )
    {
        int value = Convert::ToInt32( myEnum->Current );

        // Iterate through the radices.
        for( int i = 0; i < 4; i++ )
        {
            // Convert a value with a radix.
            int radix = radices[ i ];
            String* valueString =
                Convert::ToString( value, radix );

            Console::WriteLine( S"{0,12} {1,3}   {2}",
                __box( value ), __box( radix ), valueString );
        }
    }
}

void main()
{
    Console::WriteLine(
        S"This example of Convert::ToString( int, int ) " 
        S"generates \nthe following output. It converts several " 
        S"int values to \nstrings using the radixes supported " 
        S"by the method." );
    Console::WriteLine(
        S"\n    Value    Radix   String"
        S"\n    -----    -----   ------" );

    RunToStringDemo( );
}
0
 
LVL 5

Author Comment

by:MichaelSFuller
ID: 11906340
Thanks, appreciate it
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how do i create updater to My Activex application? 3 86
Hibernate methods 2 71
matchUp  challenge 6 60
canBalance challenge 34 80
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question