linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff Woods <kazrak+kernel@cesmail.net>
To: "J." <mailing-lists@xs4all.nl>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: convert INT to CHAR but print's BEEP...
Date: Wed, 12 Nov 2003 22:36:48 -0800	[thread overview]
Message-ID: <5.2.1.1.0.20031112211508.02f5c098@no.incoming.mail> (raw)
In-Reply-To: <Pine.LNX.4.21.0311120519210.31743-100000@hestia>

At 11/12/2003 05:35 AM +0100, J. wrote:
>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'.

I'm sure there are faster algorithms, but how about something simple like:

#include <stdlib.h>

typedef unsigned char   U8;
typedef unsigned short  U16;

/* Set <buf_len> bytes at <buf> to random values from <low> to <high>. */
void set_rand_buf(char *buf, size_t buf_len, U8 low, U8 high) {
     U8 range = high - low + 1;
     while (buf_len--)
         *buf++ = low + (U8)((range*rand())/(RAND_MAX+1.0));
}

/* Sample calls */

#include <assert.h>

int main(void) {
     char        buf[80];
     size_t      k;

EXAMPLE_1:
     /* Completely fill buf with random data; may include embedded nulls */
     set_rand_buf(buf, sizeof buf, 0, 255);
     for (k = 0; k < sizeof buf; k++)
         printf("byte[%3d]=%3hu\n", k, (U16)buf[k]);

EXAMPLE_2:
     set_rand_buf(buf, 50, '0', '9');    /* Get 50 random ASCII digits. */
     buf[50] = 0;        /* Null-terminate the string. */
     printf("50 digits: %s\n", buf);

EXAMPLE_3:
     set_rand_buf(buf, 26, 'a', 'z');    /* Get 26 random lower-case 
letters. */
     buf[26] = 0;        /* Null-terminate the string. */
     printf("26 letters: %s\n", buf);

EXAMPLE_4:
#DEFINE LEN 27
     /* Get LEN random hex digits in ASCII */
     assert(LEN < sizeof buf);
     set_rand_buf(buf, LEN, 0, 15);      /* First get LEN hex values */
     {   /* Convert hex values from binary to ASCII */
         char *ptr = buf + LEN;
         while (ptr-- > buf)
             *ptr = (*ptr >= 10) ? ('a' - 10 + *ptr) : ('0' + *ptr);
     }
     buf[LEN] = 0;       /* Null-terminate as a string. */
     printf("hex digits %s\n", buf);


     return 0;   /* No-error program exit */
}

P.S.  The above code has never been compiled, so there may be all kinds of 
typos or other errors present.

--
Jeff Woods <kazrak+kernel@cesmail.net> 



      parent reply	other threads:[~2003-11-13  6:36 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     ` convert INT to CHAR but print's BEEP J.
2003-11-12  7:11       ` Mikael Aronsson
2003-11-13  1:26         ` convert INT to CHAR - SOLVED J.
2003-11-13  6:36       ` Jeff Woods [this message]

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=5.2.1.1.0.20031112211508.02f5c098@no.incoming.mail \
    --to=kazrak+kernel@cesmail.net \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=mailing-lists@xs4all.nl \
    /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).