integer represenation of ASCii to char for XML web service

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);
            
}
LVL 5
MichaelSFullerAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
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
 
jkrCommented:
Are you thinking of remodelling the functionality of

int __toascii( int c );

?
0
 
jkrCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Jaime OlivaresSoftware ArchitectCommented:
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
 
jkrCommented:
>> 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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
jkrCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
The fact is that this is not the best solution among all the possibilities. Just a capricious preference.
0
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
MichaelSFullerAuthor Commented:
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
 
jkrCommented:
You mean a numeric base of 36? You can do that using

char *_itoa( int value, char *string, int radix );
0
 
jkrCommented:
E.g.

   char buffer[20];
   int  i = 5;

   _itoa( i, buffer, 36 );
   printf( "String of integer %d (radix 36): %s\n", i, buffer );
0
 
MichaelSFullerAuthor Commented:
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
 
jkrCommented:
Well, if '_itoa()' is still available in .NET, it should be easy, just like the above lines.
0
 
MichaelSFullerAuthor Commented:
Verifying right now.
0
 
jkrCommented:
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
 
MichaelSFullerAuthor Commented:
Thanks, appreciate it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.