2

I need to generate a huge text.

I'm using bash and I want to convert an integer number like 65 to a char like A. So I use random value between 0 and 255 (I need all the ASCII table), convert to a char and redirect to a file using >>. But I cannot make bash interpret the integer as a char. Like printf("%c", 65) in C++. But if I try this in bash, it returns 6.

Eugene S
  • 6,709
  • 8
  • 57
  • 91
Lefsler
  • 1,738
  • 6
  • 26
  • 46
  • possible duplicate of [Integer ASCII value to character in BASH using printf](http://stackoverflow.com/questions/890262/integer-ascii-value-to-character-in-bash-using-printf) – dogbane May 08 '12 at 10:59

3 Answers3

5

If you need to generate a huge random sequence of bytes, why not use /dev/urandom?

$ RANDOM=$( dd if=/dev/urandom bs=1024 count=1|base64 )
1+0 records in
1+0 records out
1024 bytes (1.0 kB) copied, 0.00056872 s, 1.8 MB/s
$ echo $RANDOM
r553ONKlLkU3RvMp753OxHjGDd6LiL1qSdUWJqImggHlXiZjjUuGQvbSBfjqXxlM6sSwQh29y484
KDgg/6XP31Egqwo7GCBWxbEIfABPQyy48mciljUpQLycZIeFc/bRif0aXpc3tl5Jer/W45H7VAng
[...]

I piped the output to base64 to avoid control characters that might confuse the terminal.

$ RANDOM=$( dd if=/dev/urandom bs=1024 count=1 )

or

$ dd if=/dev/urandom of=outputfile bs=1024 count=1

will create a file with 1kB of random data.

Anders Lindahl
  • 41,582
  • 9
  • 89
  • 93
  • The problem is i need between 1 - 255 (char code). The process need to compress it, even if it confuses the terminal. I just cannot get out the ascii table. Thanks – Lefsler May 08 '12 at 12:06
  • 1
    Then remove the `|base64` in my example above, and you will have a random sequence of bytes. – Anders Lindahl May 08 '12 at 12:10
  • Bash doesn't handle binary data very well. – Dennis Williamson May 08 '12 at 12:25
  • I'll not show it on the bash, i'll create a file and compress the data using my compressor, just to test if it's ok. It's a RLE compressor (in asm) – Lefsler May 08 '12 at 12:39
  • @demonofnight - compression doesn't work well on random data, it can even make the file bigger. – Kevin May 08 '12 at 12:45
4

you need to chain it like

printf \\$(printf '%03o' $((65)))
pizza
  • 7,296
  • 1
  • 25
  • 22
2

try this:

How do I convert an ASCII character to its decimal (or hexadecimal) value and back?

randoms
  • 2,793
  • 1
  • 31
  • 48
  • 1
    Whilst this may answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Stéphane Gimenez May 08 '12 at 11:25