Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-22-2004, 04:33 PM
|
#1
|
Member
Registered: May 2003
Posts: 256
Rep:
|
Converting int value to char
I need some advise on rand.
Last edited by liguorir; 05-23-2004 at 07:26 PM.
|
|
|
05-22-2004, 06:13 PM
|
#2
|
Member
Registered: Mar 2004
Location: bursa
Posts: 110
Rep:
|
I'm not familiar with rand() and I'm not on a machine where I can test what you've written (it looks needlessly complicated to me, but I could be missing something), but assuming the expression on the rhs of the sum operator generates a number between zero and 25, what you've got is a perfectly valid char and you may assign it as such (a char being a byte-wide value on my machine, anyway; 0-255).
Also, for readability you could change 97 to 'a'. Hard-coding sucks!
Last edited by rkef; 05-22-2004 at 06:22 PM.
|
|
|
05-22-2004, 09:06 PM
|
#3
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
char_name[0] = (char)( 97+(int) (25.0*rand()/(RAND_MAX+1.0)) );
|
|
|
05-22-2004, 09:12 PM
|
#4
|
Member
Registered: Aug 2002
Distribution: Debian
Posts: 540
Rep:
|
I think he means a string???
Like so if you mean an int-string value:
sprintf(char_name, "%d", ( 97+(int) (25.0*rand()/(RAND_MAX+1.0)) ));
|
|
|
05-23-2004, 12:30 AM
|
#5
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Quote:
Originally posted by The_Nerd
I think he means a string???
Like so if you mean an int-string value:
sprintf(char_name, "%d", ( 97+(int) (25.0*rand()/(RAND_MAX+1.0)) ));
|
He wants it to show up as a character I think...he doesn't want a string that prints out as, for example, "105".
If he wants a string then just add char_name[1] = '\0'; to my previous example or use %c instead of %d in The_Nerd's example.
|
|
|
05-23-2004, 03:54 AM
|
#6
|
Member
Registered: May 2003
Posts: 256
Original Poster
Rep:
|
Got it.
Thanks for the advise
Last edited by liguorir; 05-23-2004 at 07:27 PM.
|
|
|
05-23-2004, 08:30 AM
|
#7
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
You'll want to change declaration for salt to char salt[3]; and after your for() loop add salt[2] = '\0';. Otherwise you'll get the occasional segfault.
|
|
|
05-23-2004, 06:39 PM
|
#8
|
Member
Registered: May 2003
Posts: 256
Original Poster
Rep:
|
Excellent, thanks - but why?
I made your suggested change, but why exactly why does it occassionally fault?
|
|
|
05-23-2004, 07:21 PM
|
#9
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
It segfaults because it's not a real string unless it's NUL-terminated. All string functions know that the end of the string will be a NUL-terminator. If it's not present, then your string had 2 valid chars and then garbage after it. String functions would keep on scanning right through the garbage and cause all sorts of junk to happen 
|
|
|
All times are GMT -5. The time now is 12:06 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|