LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-22-2004, 04:33 PM   #1
liguorir
Member
 
Registered: May 2003
Posts: 256

Rep: Reputation: 30
Question Converting int value to char


I need some advise on rand.

Last edited by liguorir; 05-23-2004 at 07:26 PM.
 
Old 05-22-2004, 06:13 PM   #2
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
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.
 
Old 05-22-2004, 09:06 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
char_name[0] = (char)( 97+(int) (25.0*rand()/(RAND_MAX+1.0)) );
 
Old 05-22-2004, 09:12 PM   #4
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
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)) ));
 
Old 05-23-2004, 12:30 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 05-23-2004, 03:54 AM   #6
liguorir
Member
 
Registered: May 2003
Posts: 256

Original Poster
Rep: Reputation: 30
Cool Got it.

Thanks for the advise

Last edited by liguorir; 05-23-2004 at 07:27 PM.
 
Old 05-23-2004, 08:30 AM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 05-23-2004, 06:39 PM   #8
liguorir
Member
 
Registered: May 2003
Posts: 256

Original Poster
Rep: Reputation: 30
Question Excellent, thanks - but why?

I made your suggested change, but why exactly why does it occassionally fault?
 
Old 05-23-2004, 07:21 PM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
char to int: a twist Guru3 Programming 5 12-05-2004 08:49 AM
c++ ERROR: int to const char ashirazi Programming 6 08-06-2004 10:01 PM
int(char(254)) becomes kalleanka Programming 2 02-17-2004 03:35 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
int, long in char * marek Programming 10 09-01-2003 10:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration