/** * WinPR: Windows Portable Runtime * Data Conversion * * Copyright 2012 Marc-Andre Moreau * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include /* Data Conversion: http://msdn.microsoft.com/en-us/library/0heszx3w/ */ #ifndef _WIN32 errno_t _itoa_s(int value, char* buffer, size_t sizeInCharacters, int radix) { int length; length = sprintf_s(NULL, 0, "%d", value); if (length < 0) return -1; if (sizeInCharacters < (size_t)length) return -1; sprintf_s(buffer, length + 1, "%d", value); return 0; } errno_t _ltoa_s(long value, char* buffer, size_t sizeInCharacters, int radix) { int length; if (radix == 16) { length = sprintf_s(NULL, 0, "%lx", value); } else { length = sprintf_s(NULL, 0, "%ld", value); } if (sizeInCharacters < length) return -1; if (radix == 16) { sprintf_s(buffer, length + 1, "%lx", value); } else { sprintf_s(buffer, length + 1, "%ld", value); } return 0; } char* _itoa(int value, char* buffer, int radix) { char* p; unsigned int a; //int len; char* b; char temp; unsigned int u; p = buffer; if (value < 0) { *p++ = '-'; value = 0 - value; } u = (unsigned int)value; b = p; do { a = u % radix; u /= radix; *p++ = a + '0'; } while (u > 0); //len = (int)(p-buffer); *p-- = 0; do { temp = *p; *p = *b; *b = temp; --p; ++b; } while (b < p); return buffer; } char* _ultoa(unsigned long value, char* pstring, int radix) { char tmp[33] = { 0 }; char* tp = tmp; long i; unsigned long v = value; char* sp; if (radix > 36 || radix <= 1 || NULL == pstring) { return 0; } while (v || tp == tmp) { i = v % radix; v = v / radix; if (i < 10) *tp++ = i + '0'; else *tp++ = i + 'a' - 10; } sp = pstring; while (tp > tmp) *sp++ = *--tp; *sp = 0; return pstring; } #endif