[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Integer to string function doesn't account for 0

Posted on 2006-04-11
Medium Priority
371 Views
I've been using this function to convert integers into wide character strings, but I realized the function fails if the integer to be converted is 0.  This happens because the for loop breaks if it finds that val = 0.

What are some workarounds to this problem?

wchar_t* itowcs(int val, int base)
{
static wchar_t buf[32] = {0};
int i = 30;
for(; val && i ; --i, val /= base) {
buf[i] = L"0123456789abcdef"[val % base];
}
return &buf[i+1];
}
0
Question by:chsalvia

LVL 85

Accepted Solution

ozo earned 1000 total points
ID: 16433650
for(; (buf[i] = L"0123456789abcdef"[val % base]) && (val/=base) && --i ; ) {

}
return &buf[i];
0

LVL 16

Expert Comment

ID: 16434200
Neat code ozo!
0

LVL 12

Expert Comment

ID: 16435864
Why not using this function instead of re-inventing the wheel
wchar_t * _itow( int value, wchar_t *string, int radix );
Or,
wchar_t * itow( int value, wchar_t *string, int radix );
0

## Featured Post

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month19 days, 2 hours left to enroll