Unique file name using UUID - c

I need to create some binary data files from Object Identifiers, which is a variable-length binary void* buffer, upto 64 bytes and can contain any bytes corresponding to non-printable characters as well. I can't use Object Identifier as my file name as it contains non-printable letters.
Any suggestions to create the unique filename. How can the UUID be derived or used in this case ?

You can convert the bytes into a hexadecimal string.
#define tohex(x) ("0123456789abcdef"[(x) & 0x0f])
char buf[129];
assert(objid_len <= 64);
for (int i = 0; i < objid_len; ++i) {
buf[2*i] = tohex(objid[i] >> 4);
buf[2*i+1] = tohex(objid[i]);
}
buf[2*objid_len] = '\0';
You can make the filenames have universal length by using a padding character that is outside the alphabet used to to represent the object id. If a shorter filename is desired, then a higher base could be used. For example, Base64.
const char * const base64str =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define tob64(x) base64str[(x) & 0x3f]
void objid_to_filename (const unsigned char *objid, int objid_len,
char *buf) {
memset(buf, '-', 88);
buf[88] = '\0';
int i = 0, j = 0;
int buflen = 4 * ((objid_len + 2)/3);
while (i < objid_len) {
unsigned x = 0;
x |= (i < objid_len) ? objid[i++] << 16 : 0;
x |= (i < objid_len) ? objid[i++] << 8 : 0;
x |= (i < objid_len) ? objid[i++] << 0 : 0;
buf[j++] = tob64(x >> 18);
buf[j++] = tob64(x >> 12);
buf[j++] = tob64(x >> 6);
buf[j++] = tob64(x >> 0);
}
int pad = (3 - (objid_len % 3)) % 3;
for (i = 0; i < pad; ++i) buf[buflen - 1 - i] = '=';
}

Related

Convert ASCII String to 7-bit GSM coding scheme

A simple routine i wrote for converting ASCII string to corresponding 7-bit GSM coding scheme:
#include <stdio.h>
#include <process.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
/* convert ascii input string to 7-bit GSM alphabet */
bool ascii_to_gsm(const char* in, uint8_t len, uint8_t* out, uint8_t start_indx) {
if (in == NULL || out == NULL || len == 0)
return false;
uint8_t nshift = 7;
memcpy(out + start_indx, in, len);
for (size_t i = 0; i < len - 1; i++) {
nshift = (nshift == 255) ? 7 : nshift;
uint16_t l = out[start_indx + i];
uint16_t h = out[start_indx + i + 1];
h = (h << nshift--) | l;
out[start_indx + i] = h;
out[start_indx + i + 1] = h >> 8;
}
return true;
}
int main() {
char data[] = "ASCIIASCII";
uint8_t buff[sizeof(data) - 1];
memset(buff, 0, sizeof(buff));
ascii_to_gsm(data, sizeof(buff), buff, 0);
for (size_t i = 0; i < sizeof(buff); i++) {
printf("\n buff[%d]=%02x", i, buff[i]);
}
system("pause");
return 0;
}
For strings like ASCII or TEST it's working fine and output is C1E9309904 and D4E2940Arespectively.
But for string ASCIIASCII some output byte is wrong:
C1E930990C4E87498024
The result should be: C1E930990C4E87C924
Don't know what part, i'm wrong.
Concepts about GSM coding can be found here.
I use this online encoder to compare results
But for string ASCIIASCII some output byte is wrong:
C1E930990C4E87498024
The result should be:
C1E930990C4E87C924
OP's code does not take into account the output may have a shorter length than the input.
If the input is 10 ASCII characters, that is 70 bits. The output needs to be ceiling(70/8) or 9 bytes. Also see #Steve Summit.
A simplified code for reference that lacks a start_indx. Since input is a string ("converting ASCII string"), the input length is not needed.
bool ascii_to_gsmA(const char* in, uint8_t* out) {
unsigned bit_count = 0;
unsigned bit_queue = 0;
while (*in) {
bit_queue |= (*in & 0x7Fu) << bit_count;
bit_count += 7;
if (bit_count >= 8) {
*out++ = (uint8_t) bit_queue;
bit_count -= 8;
bit_queue >>= 8;
}
in++;
}
if (bit_count > 0) {
*out++ = (uint8_t) bit_queue;
}
return true;
}

C parse dec to hex and output as char[]

I need some help please. I am looking to modify the DecToHex function.
For input decimalNumber = 7 :
Actual output :
sizeToReturn = 2;
hexadecimalNumber[1] = 7;
hexadecimalNumber[0] = N/A ( is garbage );
Desired output :
sizeToReturn = 3
hexadecimalNumber[2] = 0
hexadecimalNumber[1] = 7
hexadecimalNumber[0] = N/A ( is garbage )
The function :
void DecToHex(int decimalNumber, int *sizeToReturn, char* hexadecimalNumber)
{
int quotient;
int i = 1, temp;
quotient = decimalNumber;
while (quotient != 0) {
temp = quotient % 16;
//To convert integer into character
if (temp < 10)
temp = temp + 48; else
temp = temp + 55;
hexadecimalNumber[i++] = temp;
quotient = quotient / 16;
}
(*sizeToReturn) = i;
}
This will append each u8 to an array :
for (int k = size - 1;k > 0;k--)
AppendChar(Str_pst, toAppend[k]);
You are really close, you can reverse in the array and add a '0' to the beginning with little effort, or you can leave it the way you have it and take care of it in main. Where I think you are getting wound around the axle is in the indexing of hexadecimalNumber in your function. While 7 produces one hex-digit, it should be at index zero in hexadecimalNumber (except you initialize i = 1) That sets up confusion in handling your conversion to string indexes. Just keep the indexes straight, initializing i = 0 and using hexadecimalNumber initialized to all zeros, if you only have a single character at index 1, pad the string with 0 at the beginning.
Here is a short example that may help:
#include <stdio.h>
#include <stdlib.h>
#define NCHR 32
void d2h (int n, char *hex)
{
int idx = 0, ridx = 0; /* index & reversal index */
char revhex[NCHR] = ""; /* buf holding hex in reverse */
while (n) {
int tmp = n % 16;
if (tmp < 10)
tmp += '0';
else
tmp += '7';
revhex[idx++] = tmp;
n /= 16;
}
if (idx == 1) idx++; /* handle the zero pad on 1-char */
while (idx--) { /* reverse & '0' pad result */
hex[idx] = revhex[ridx] ? revhex[ridx] : '0';
ridx++;
}
}
int main (int argc, char **argv) {
int n = argc > 1 ? atoi (argv[1]) : 7;
char hbuf[NCHR] = "";
d2h (n, hbuf);
printf ("int : %d\nhex : 0x%s\n", n, hbuf);
return 0;
}
The 0x prefix is just part of the formatted output above.
Example Use/Output
$ ./bin/h2d
int : 7
hex : 0x07
$ ./bin/h2d 26
int : 26
hex : 0x1A
$ ./bin/h2d 57005
int : 57005
hex : 0xDEAD
If you do want to handle the reversal in main() so you can tack on the 0x07 if the number of chars returned in hexadecimalNumber are less than two, then you can do something similar to the following:
void d2h (int n, int *sz, char *hex)
{
int idx = 0;
while (n) {
int tmp = n % 16;
if (tmp < 10)
tmp += '0';
else
tmp += '7';
hex[idx++] = tmp;
n /= 16;
}
*sz = idx;
}
int main (int argc, char **argv) {
int n = argc > 1 ? atoi (argv[1]) : 7, sz = 0;
char hbuf[NCHR] = "";
d2h (n, &sz, hbuf);
printf ("int : %d\nhex : 0x", n);
if (sz < 2)
putchar ('0');
while (sz--)
putchar (hbuf[sz]);
putchar ('\n');
return 0;
}
Output is the same
Look it over and let me know if you have further questions.

Base64 Encoding Padding in C

I am writting a base64 encoder and decoder and have it almost completly functional, I just need to be able to pad the encoded data with equal signs if the number of input bytes does not round out to a multiple of 3. I am relativly new to C and am not sure how I would detect the number of bytes or pad the output accordingly. This is what I have so far
void encode(char* src, char* dest) {
char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned char first = (src[0] >> 2);
unsigned char second = (src[0] << 4) | (src[1] >> 4);
second = (second << 2);
second = (second >> 2);
unsigned char third = (src[1] << 2) | (src[2] >> 6);
third = (third << 2);
third = (third >> 2);
unsigned char fourth = (src[2]);
fourth = (fourth << 2);
fourth = (fourth >> 2);
dest[0] = base64[first];
dest[1] = base64[second];
dest[2] = base64[third];
dest[3] = base64[fourth];
}
And my decoder method...
void decode(char* src, char* dest) {
char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int i;
int j;
int index;
for(i = 0; i < 4; i++) {
index = 0;
for(j = 0; j < 64; j++) {
if (src[i] == base64[j]) {
src[i] = index;
} else {
index++;
}
}
}
char first = (src[0] << 2) | (src[1] >> 4);
char second = (src[1] << 4) | (src[2] >> 2);
char third = (src[2] << 6) | (src[3]);
dest[0] = first;
dest[1] = second;
dest[2] = third;
}
If this is not enough and I need to also provide the rest of my code I can do so.

Using arrays for BCD conversion in C

I'm working on a project where I need to convert some long variables into BCD.
I already have some code that works but I feel that it can be improved...
void main(void){
unsigned long input = 0;
unsigned long convert = 0;
float convert2 = 0;
char buffer[200];
unsigned char Ones, Tens, Hundreds, Thousands, TenThousands, HundredThousands;
printf("Input: ");
scanf("%d", &input);
convert = input*12;
convert2 = input * 0.0001224896;
BCD(convert, &Ones, &Tens, &Hundreds, &Thousands, &TenThousands, &HundredThousands);
sprintf(buffer, "%d%d%dKG", HundredThousands, TenThousands, Thousands);
printf("\n\nInputted: %d", input);
printf("\nADC Conversion: %d", convert);
printf("\nBCD Conversion: %s", buffer);
printf("\nFloat Conversion: %f", convert2);
getchar();
getchar();
}
void BCD (unsigned long Pass, unsigned char *Ones, unsigned char *Tens, unsigned char *Hundreds, unsigned char *Thousands, unsigned char *TenThousands, unsigned char *HundredThousands){
unsigned char temp1, temp2, temp3, temp4, temp5, temp6;
unsigned int count = 0;
*Ones = 0;
*Tens = 0;
*Hundreds = 0;
*Thousands = 0;
*TenThousands = 0;
*HundredThousands = 0;
temp1 = 0;
temp2 = 0;
temp3 = 0;
temp4 = 0;
temp5 = 0;
temp6 = 0;
for(count = 0; count <= 31; count++){
if (*Ones >= 5){
*Ones = (*Ones + 3)&0x0F;
}
if (*Tens >= 5){
*Tens = (*Tens + 3)&0x0F;
}
if (*Hundreds >= 5){
*Hundreds = (*Hundreds + 3)&0x0F;
}
if (*Thousands >= 5){
*Thousands = (*Thousands + 3)&0x0F;
}
if (*TenThousands >= 5){
*TenThousands = (*TenThousands + 3)&0x0F;
}
if (*HundredThousands >= 5){
*HundredThousands = (*HundredThousands + 3)&0x0F;
}
temp1 = (Pass & 2147483648) >> 31;
temp2 = (*Ones & 8) >> 3;
temp3 = (*Tens & 8) >> 3;
temp4 = (*Hundreds & 8) >> 3;
temp5 = (*Thousands & 8) >> 3;
temp6 = (*TenThousands & 8) >> 3;
Pass = Pass << 1;
*Ones = ((*Ones << 1) + temp1) & 15;
*Tens = ((*Tens << 1) + temp2) & 15;
*Hundreds = ((*Hundreds << 1) + temp3) & 15;
*Thousands = ((*Thousands << 1) + temp4) & 15;
*TenThousands = ((*TenThousands << 1) + temp5) & 15;
*HundredThousands = ((*HundredThousands << 1) + temp6) & 15;
printf("\n\nLoop: %d\nOnes: %d\n", count, *Ones);
printf("Tens: %d\n", *Tens);
printf("Hundreds: %d\n", *Hundreds);
printf("Thousands: %d\n", *Thousands);
printf("TenThousands: %d\n", *TenThousands);
printf("HundredThousands: %d\n",*HundredThousands);
}
}
The problem I have with this is that it seems messy and inefficient. I was think that instead of using multiple variable for each BCD unit (Ones, Tens, etc), I could use an arrays to carry out the same process. I have implemented this in code but I'm running into a few problems. The code only seems to display "Ones" equivalent element. I've stepped through the code as well and found that the other elements are not being populated during the conversion process. Any guidance on what is going on?
Array implementation:
void main(void){
unsigned long input = 0;
unsigned long convert = 0;
char buffer[200];
unsigned char BCD_Units[6];
unsigned char temp[6];
unsigned int count = 0;
unsigned int count1 = 0;
unsigned char buff_store = 0;
unsigned char buff_store2 = 0;
printf("Input: ");
scanf("%d", &input);
convert = input;
memset(temp, 0, sizeof(temp));
memset(BCD_Units, 0, sizeof(BCD_Units));
for(count = 0; count <= 31; count++){
for (count1 = 0; count1 < 6; count1++){
if (BCD_Units[count1] >= 5){
buff_store = BCD_Units[count1];
buff_store = ((buff_store + 3) & 15);
BCD_Units[count1] = buff_store;
}
}
temp[0] = (convert & 2147483648) >> 31;
for (count1 = 0; count1 < 5; count1++){
buff_store = BCD_Units[count1];
temp[(count+1)] = (buff_store & 8) >> 3;
}
convert = convert << 1;
for(count1 = 0; count1 < 6; count1++){
buff_store = BCD_Units[count1];
buff_store2 = temp[count1];
buff_store = ((buff_store << 1) + buff_store2) & 15;
BCD_Units[count1] = buff_store;
temp[count1] = buff_store2;
}
printf("\n\nLoop: %d\nOnes: %d\n", count, BCD_Units[0]);
printf("Tens: %d\n", BCD_Units[1]);
printf("Hundreds: %d\n", BCD_Units[2]);
printf("Thousands: %d\n", BCD_Units[3]);
printf("TenThousands: %d\n", BCD_Units[4]);
printf("HundredThousands: %d\n", BCD_Units[5]);
}
sprintf(buffer, "%d%d%dKG", BCD_Units[5], BCD_Units[4], BCD_Units[3]);
printf("\n\nInputted: %d", input);
printf("\nBCD Conversion: %s", buffer);
getchar();
getchar();
}
PS. I'm just playing around, at the moment, with ideas. I plan to compartmentalise the code into functions at a later date.
this code seems enormously complicated. YOu just need to do the following
make a buffer
loop till n = 0
get n % 10 (get digit)
or digit into left or right nibble of curretn buffer byte (need a toggle for left or right)
increment buffer pointer if filled left nibble
n = n / 10
try this Convert integer from (pure) binary to BCD

Convert integer to string without access to libraries

I recently read a sample job interview question:
Write a function to convert an integer
to a string. Assume you do not have
access to library functions i.e.,
itoa(), etc...
How would you go about this?
fast stab at it: (edited to handle negative numbers)
int n = INT_MIN;
char buffer[50];
int i = 0;
bool isNeg = n<0;
unsigned int n1 = isNeg ? -n : n;
while(n1!=0)
{
buffer[i++] = n1%10+'0';
n1=n1/10;
}
if(isNeg)
buffer[i++] = '-';
buffer[i] = '\0';
for(int t = 0; t < i/2; t++)
{
buffer[t] ^= buffer[i-t-1];
buffer[i-t-1] ^= buffer[t];
buffer[t] ^= buffer[i-t-1];
}
if(n == 0)
{
buffer[0] = '0';
buffer[1] = '\0';
}
printf(buffer);
A look on the web for itoa implementation will give you good examples. Here is one, avoiding to reverse the string at the end. It relies on a static buffer, so take care if you reuse it for different values.
char* itoa(int val, int base){
static char buf[32] = {0};
int i = 30;
for(; val && i ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
return &buf[i+1];
}
The algorithm is easy to see in English.
Given an integer, e.g. 123
divide by 10 => 123/10. Yielding, result = 12 and remainder = 3
add 30h to 3 and push on stack (adding 30h will convert 3 to ASCII representation)
repeat step 1 until result < 10
add 30h to result and store on stack
the stack contains the number in order of | 1 | 2 | 3 | ...
I would keep in mind that all of the digit characters are in increasing order within the ASCII character set and do not have other characters between them.
I would also use the / and the% operators repeatedly.
How I would go about getting the memory for the string would depend on information you have not given.
Assuming it is in decimal, then like this:
int num = ...;
char res[MaxDigitCount];
int len = 0;
for(; num > 0; ++len)
{
res[len] = num%10+'0';
num/=10;
}
res[len] = 0; //null-terminating
//now we need to reverse res
for(int i = 0; i < len/2; ++i)
{
char c = res[i]; res[i] = res[len-i-1]; res[len-i-1] = c;
}
An implementation of itoa() function seems like an easy task but actually you have to take care of many aspects that are related on your exact needs. I guess that in the interview you are expected to give some details about your way to the solution rather than copying a solution that can be found in Google (http://en.wikipedia.org/wiki/Itoa)
Here are some questions you may want to ask yourself or the interviewer:
Where should the string be located (malloced? passed by the user? static variable?)
Should I support signed numbers?
Should i support floating point?
Should I support other bases rather then 10?
Do we need any input checking?
Is the output string limited in legth?
And so on.
Convert integer to string without access to libraries
Convert the least significant digit to a character first and then proceed to more significant digits.
Normally I'd shift the resulting string into place, yet recursion allows skipping that step with some tight code.
Using neg_a in myitoa_helper() avoids undefined behavior with INT_MIN.
// Return character one past end of character digits.
static char *myitoa_helper(char *dest, int neg_a) {
if (neg_a <= -10) {
dest = myitoa_helper(dest, neg_a / 10);
}
*dest = (char) ('0' - neg_a % 10);
return dest + 1;
}
char *myitoa(char *dest, int a) {
if (a >= 0) {
*myitoa_helper(dest, -a) = '\0';
} else {
*dest = '-';
*myitoa_helper(dest + 1, a) = '\0';
}
return dest;
}
void myitoa_test(int a) {
char s[100];
memset(s, 'x', sizeof s);
printf("%11d <%s>\n", a, myitoa(s, a));
}
Test code & output
#include "limits.h"
#include "stdio.h"
int main(void) {
const int a[] = {INT_MIN, INT_MIN + 1, -42, -1, 0, 1, 2, 9, 10, 99, 100,
INT_MAX - 1, INT_MAX};
for (unsigned i = 0; i < sizeof a / sizeof a[0]; i++) {
myitoa_test(a[i]);
}
return 0;
}
-2147483648 <-2147483648>
-2147483647 <-2147483647>
-42 <-42>
-1 <-1>
0 <0>
1 <1>
2 <2>
9 <9>
10 <10>
99 <99>
100 <100>
2147483646 <2147483646>
2147483647 <2147483647>
The faster the better?
unsigned countDigits(long long x)
{
int i = 1;
while ((x /= 10) && ++i);
return i;
}
unsigned getNumDigits(long long x)
{
x < 0 ? x = -x : 0;
return
x < 10 ? 1 :
x < 100 ? 2 :
x < 1000 ? 3 :
x < 10000 ? 4 :
x < 100000 ? 5 :
x < 1000000 ? 6 :
x < 10000000 ? 7 :
x < 100000000 ? 8 :
x < 1000000000 ? 9 :
x < 10000000000 ? 10 : countDigits(x);
}
#define tochar(x) '0' + x
void tostr(char* dest, long long x)
{
unsigned i = getNumDigits(x);
char negative = x < 0;
if (negative && (*dest = '-') & (x = -x) & i++);
*(dest + i) = 0;
while ((i > negative) && (*(dest + (--i)) = tochar(((x) % 10))) | (x /= 10));
}
If you want to debug, You can split the conditions (instructions) into
lines of code inside the while scopes {}.
I came across this question so I decided to drop by the code I usually use for this:
char *SignedIntToStr(char *Dest, signed int Number, register unsigned char Base) {
if (Base < 2 || Base > 36) {
return (char *)0;
}
register unsigned char Digits = 1;
register unsigned int CurrentPlaceValue = 1;
for (register unsigned int T = Number/Base; T; T /= Base) {
CurrentPlaceValue *= Base;
Digits++;
}
if (!Dest) {
Dest = malloc(Digits+(Number < 0)+1);
}
char *const RDest = Dest;
if (Number < 0) {
Number = -Number;
*Dest = '-';
Dest++;
}
for (register unsigned char i = 0; i < Digits; i++) {
register unsigned char Digit = (Number/CurrentPlaceValue);
Dest[i] = (Digit < 10? '0' : 87)+Digit;
Number %= CurrentPlaceValue;
CurrentPlaceValue /= Base;
}
Dest[Digits] = '\0';
return RDest;
}
#include <stdio.h>
int main(int argc, char *argv[]) {
char String[32];
puts(SignedIntToStr(String, -100, 16));
return 0;
}
This will automatically allocate memory if NULL is passed into Dest. Otherwise it will write to Dest.
Here's a simple approach, but I suspect if you turn this in as-is without understanding and paraphrasing it, your teacher will know you just copied off the net:
char *pru(unsigned x, char *eob)
{
do { *--eob = x%10; } while (x/=10);
return eob;
}
char *pri(int x, char *eob)
{
eob = fmtu(x<0?-x:x, eob);
if (x<0) *--eob='-';
return eob;
}
Various improvements are possible, especially if you want to efficiently support larger-than-word integer sizes up to intmax_t. I'll leave it to you to figure out the way these functions are intended to be called.
Slightly longer than the solution:
static char*
itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0)
n = -n;
i = 0;
do
{
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
return s;
}
Reverse:
int strlen(const char* str)
{
int i = 0;
while (str != '\0')
{
i++;
str++;
}
return i;
}
static void
reverse(char s[])
{
int i, j;
char c;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
And although the decision davolno long here are some useful features for beginners. I hope you will be helpful.
This is the shortest function I can think of that:
Correctly handles all signed 32-bit integers including 0, MIN_INT32, MAX_INT32.
Returns a value that can be printed immediatelly, e.g.: printf("%s\n", GetDigits(-123))
Please comment for improvements:
static const char LARGEST_NEGATIVE[] = "-2147483648";
static char* GetDigits(int32_t x) {
char* buffer = (char*) calloc(sizeof(LARGEST_NEGATIVE), 1);
int negative = x < 0;
if (negative) {
if (x + (1 << 31) == 0) { // if x is the largest negative number
memcpy(buffer, LARGEST_NEGATIVE, sizeof(LARGEST_NEGATIVE));
return buffer;
}
x *= -1;
}
// storing digits in reversed order
int length = 0;
do {
buffer[length++] = x % 10 + '0';
x /= 10;
} while (x > 0);
if (negative) {
buffer[length++] = '-'; // appending minus
}
// reversing digits
for (int i = 0; i < length / 2; i++) {
char temp = buffer[i];
buffer[i] = buffer[length-1 - i];
buffer[length-1 - i] = temp;
}
return buffer;
}
//Fixed the answer from [10]
#include <iostream>
void CovertIntToString(unsigned int n1)
{
unsigned int n = INT_MIN;
char buffer[50];
int i = 0;
n = n1;
bool isNeg = n<0;
n1 = isNeg ? -n1 : n1;
while(n1!=0)
{
buffer[i++] = n1%10+'0';
n1=n1/10;
}
if(isNeg)
buffer[i++] = '-';
buffer[i] = '\0';
// Now we must reverse the string
for(int t = 0; t < i/2; t++)
{
buffer[t] ^= buffer[i-t-1];
buffer[i-t-1] ^= buffer[t];
buffer[t] ^= buffer[i-t-1];
}
if(n == 0)
{
buffer[0] = '0';
buffer[1] = '\0';
}
printf("%s", buffer);
}
int main() {
unsigned int x = 4156;
CovertIntToString(x);
return 0;
}
This function converts each digits of number into a char and chars add together
in one stack forming a string. Finally, string is formed from integer.
string convertToString(int num){
string str="";
for(; num>0;){
str+=(num%10+'0');
num/=10;
}
return str;
}

Resources