* IntToChar Template Function with Corrections
@ 2002-12-30 5:14 Joseph D. Wagner
0 siblings, 0 replies; only message in thread
From: Joseph D. Wagner @ 2002-12-30 5:14 UTC (permalink / raw)
To: linux-c-programming; +Cc: redhat-devel-list
[-- Attachment #1: Type: text/plain, Size: 783 bytes --]
I've corrected a few quirks and bugs in my original file.
Just to remind you in case you forgot:
I created a template function that will convert an integer of any form
(unsigned/signed, short/long) to its char array equivalent.
For example,
int myInt = 768;
is converted into
char myChar[] = {"768"};
This isn't exactly how it works but I wrote that little bit just to complete
your mental picture.
You pass to the function IntToChar an integer of any type and it will return
a pointer to a char array.
For example:
const char *myChar = IntToChar(myInt);
I tested it, and it works. It's fully operational, but I'm sure somebody
out there will find quirks that can be tweaked.
Any such comments would be appreciated!
TIA
Joseph Wagner
[-- Attachment #2: IntToChar.hpp --]
[-- Type: text/plain, Size: 2638 bytes --]
// Template Function: IntToChar
// Copyright © 2002 Joseph Wagner. All rights reserved.
// Email: wagnerjd@users.sourceforge.net
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#pragma once
// Allows Ports to Non-Windows
#if !defined(_WIN32) && !defined(_WIN64)
typedef long long int __int64;
#endif
#include <bitset>
using std::bitset;
#include <typeinfo>
#include <cstring>
#include <cmath>
template<typename IntegerType>
const char *IntToChar(IntegerType number) {
// Supports 64-bit integers
const bitset<64> number64 = number;
char dataType[64] = {0};
static char character64[22] = {0}; // 64-bit numbers never have more than 20 digits
unsigned short pointer = 0;
unsigned short digitHolder = 0;
const unsigned short AsciiOffset = 48;
// Determines size in bits
const unsigned short sizeInBits = sizeof(IntegerType) * 8;
const unsigned short lastBit = sizeInBits - 1;
// Obtains type information
strncpy(dataType, typeid(IntegerType).name(), 64);
// Determines if signed or unsigned
bool isUnsigned = false;
char *tokenDataType = strtok(dataType, " ");
while(tokenDataType != NULL) {
if(strcmp(tokenDataType, "unsigned")) {
isUnsigned = true;
}
tokenDataType = strtok(NULL, " ");
}
// Deals with sign
if(!isUnsigned && number64[lastBit] == 1) {
character64[pointer] = '-';
pointer++;
}
// Converts individual digits to characters
for(unsigned __int64 divisor = 10000000000000000000; divisor >= 1; divisor /= 10) {
if(divisor <= number) {
digitHolder = static_cast<unsigned short>(floor(number / static_cast<double>(divisor)));
character64[pointer] = static_cast<char>(digitHolder + AsciiOffset);
number -= static_cast<IntegerType>(digitHolder * divisor);
pointer++;
}
}
// Finishes up with terminating null character
character64[pointer] = '\0';
return character64;
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2002-12-30 5:14 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-12-30 5:14 IntToChar Template Function with Corrections Joseph D. Wagner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).