linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "J." <mailing-lists@xs4all.nl>
To: linux-c-programming@vger.kernel.org
Subject: convert INT to CHAR but print's BEEP...
Date: Wed, 12 Nov 2003 05:35:20 +0100 (CET)	[thread overview]
Message-ID: <Pine.LNX.4.21.0311120519210.31743-100000@hestia> (raw)
In-Reply-To: <20031110173743.GA975@velka.phys.uoa.gr>

Wednesday, November 12 05:19:21

Hello, I have kind of a problem with converting an int to a char.
The get_rand_str() function returns an string build of random 
charaters with a max length of int `MAX'.

This seems to work at first glance: 
~: ./program 
qqxfrd

However if I examen the output closer:
~: ./program | od -a
0000000   q   q soh   x stx   f etx   r eot   d enq  nl
0000014

Now I know why my computer keeps beeping everytime it
outputs a string :-)

What is the correct way of changing the int value to a
char value so that I can append it to the return string ?

char *get_rand_str(int max) {
 int i = 0;
 char value;
 char *retval = NULL;

 for(; i < max; i++) {
  value = get_ascii_code(97, 122);
  retval = (char *)realloc(retval, sizeof(char));
  strcat(retval, &value);
 }

 return retval;
 free(retval);
}

int get_ascii_code(int low, int high) {
 int k = 0;
 double d = 0;

 d = (double)rand() / ((double)RAND_MAX + 1);
 k = (int)(d * (high - low + 1));
 return(low + k);
}

Thank you.... 

J.

--
KonkyFong


  reply	other threads:[~2003-11-12  4:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-08  9:30 mixing C/C++ Elias Athanasopoulos
2003-11-08 13:36 ` James Stevenson
2003-11-10 15:00 ` Matthew Studley
2003-11-10 17:37   ` Elias Athanasopoulos
2003-11-12  4:35     ` J. [this message]
2003-11-12  7:11       ` convert INT to CHAR but print's BEEP Mikael Aronsson
2003-11-13  1:26         ` convert INT to CHAR - SOLVED J.
2003-11-13  6:36       ` convert INT to CHAR but print's BEEP Jeff Woods

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.21.0311120519210.31743-100000@hestia \
    --to=mailing-lists@xs4all.nl \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).