• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

Matthew123

Long to string

9 posts in this topic

HI How do you convert a unsigned long to a string. Is there a function that i have overlooked or what. Thanks
0

Share this post


Link to post
Share on other sites
Hello.

I actually anwered an almost identical question a few days ago. The ANSI C way to do this is to use sprintf from stdio.h. Just do this:

#include <stdio.h> // To use sprintf

...

unsigned long int x;
x = 5;
char *temp[2];
sprintf(temp, "%d\0", x);
printf("%s", temp);

You could also use the plain ASCII set and do some calculations but it seems to be just harder work for the exact same result.

Minion
0

Share this post


Link to post
Share on other sites
  
...
int num = 37;
char str = char(num)


If you can''t do that in string class (string.h) (if they haven''t defined a string operator long()(), then you''re out of luck with that, unless you define your own (if you know how))
0

Share this post


Link to post
Share on other sites
You could do it with sprintf, but it''s probably better to use itoa.

char Str[10];
itoa(Number, Str, 10);
0

Share this post


Link to post
Share on other sites
you could always do something like this using remainders...

char * getNumberString(unsigned long * pDigitSpan, long number) {
/*
* get the number of digits in the number you''re converting
*/
unsigned long digitSpan = 0;

long tempNum = number;

while (tempNum > 0) {
tempNum /= 10;
digitSpan++;
}

/*
* actually build up the string of number
*/
char * numberString = new char[digitSpan];

tempNum = number;

for (unsigned long i = digitSpan; i > 0; i--) {
//see note below about this line
numberString[i-1] = tempNum % 10;

tempNum /= 10;
}

//i know you could probably incriment the *pDigitSpan
//instead of doing this assignment down here
//but i ran into some wierd problems doing something similar
//in another program so...
*pDigitSpan = digitSpan;


return numberString;
}

this also gives you the digit length of the number.

i think you need to convert tempNum % 10 from a single digit number into a char. i don''t remember exactly how to do that but i think it might be:

numberString[i-1] = ((char) tempNum % 10) - (char) 0;

damn, been way too long since i converted numerals into chars...

anyway, you can also use this function to convert to a binary number if you change the 10 to a 2 in all the % and /= statements. or even better would be to generecize the function by adding a passed int of the base number system you want to convert to. then use that instead of /= 10; (i.e. /= base

whatever, i know this function works for converting to binary. cause i wrote it for that purpose cept used a return type of bool * to conserve memory space.

-me
0

Share this post


Link to post
Share on other sites
...that does not work if number == 0, or if you want to calculate the number in hex. Try this:

  
const char char_values[] = "0123456789ABCDEF";
char *IntToChar(int num, int base) {
int length = 0;
int temp_num = num;

do { // while(temp_num > 0)

++length;
temp_num /= base;
} while(temp_num > 0);

char *str = (char*)malloc(length + 1);
str += length + 1;
*str = ''\0'';

do { // while(num > 0)

--str;
*str = char_values[num % base];
num /= base;
} while(num > 0);

return str;
}
0

Share this post


Link to post
Share on other sites