0
#include <TrueRandom.h>

void setup() {
  char* ss= (char*)TrueRandom.random(111111111,999999999);
  char* finalss;
  Serial.begin(9600);
  finalss[0] = ss[0];
  finalss[1] = ss[1];
  finalss[2] = ss[2];
  finalss[3] = '-';
  finalss[4] = ss[3]; 
  finalss[5] = ss[4]; 
  finalss[6] = '-';
  finalss[7] = ss[5]; 
  finalss[8] = ss[6];
  finalss[9] = ss[7]; 
  finalss[10] = ss[8];
  Serial.println (finalss);
}

void loop() {
  ; // Do nothing
}

This is supposed to output a randomly generated number in this format nnn-nn-nnnn. Updated Code (Doesn't seem to output anything):

#include <TrueRandom.h>

void setup() {
  String ss;
  ss = (String)TrueRandom.random(111111111,999999999);
  String finalss;
  Serial.begin(9600);
  finalss[0] = ss[0];
  finalss[1] = ss[1];
  finalss[2] = ss[2];
  finalss[3] = '-';
  finalss[4] = ss[3]; 
  finalss[5] = ss[4]; 
  finalss[6] = '-';
  finalss[7] = ss[5]; 
  finalss[8] = ss[6];
  finalss[9] = ss[7]; 
  finalss[10] = ss[8];
  Serial.println (finalss);
}

void loop() {
  ; // Do nothing
}
jfpoilpret
  • 9,132
  • 7
  • 37
  • 54

3 Answers3

4

Casting a number to a char* doesn't convert the number to a string. Instead it converts the number to a pointer and starts reading chars from that location.

Either pass the number to a String constructor, or pass it through ltoa().

EDIT:

String ss(...);

or

char ss[11];
ltoa(..., ss, 10);

Also, don't forget to allocate memory for finalss and NUL-terminate it.

char finalss[12];
 ...
finalss[11] = 0;
Ignacio Vazquez-Abrams
  • 17,663
  • 1
  • 27
  • 32
  • I tried to the best of my knowledge to follow your advice but seemingly I didn't understand what you meant. I updated the code. – Hector Prado Dec 16 '14 at 02:52
1

I am really assuming you are doing this on a arduino, do you have the serial terminal switched on, you are putting that entire code in setup, means it executes only once and that maybe before you can get your serial terminal on.

So if you are putting it on setup, you need to push the reset button to get a number.

check the code at - https://code.google.com/p/tinkerit/wiki/TrueRandom and see if that works

A better way to write your code may be -

#include <TrueRandom.h>

void setup() {
  Serial.begin(9600);

  Serial.print(TrueRandom.random(111,999));
  Serial.print('-');
  Serial.print(TrueRandom.random(11,99));
  Serial.print('-');
  Serial.println(TrueRandom.random(1111,9999));

}

void loop() {
  ; // Do nothing
}
Rishabh
  • 33
  • 5
1
void setup() {
  long longrand = TrueRandom.random(111111111,999999999);
  char ss[9+1], finalss[13+1];

  Serial.begin(9600);
  (void)ltoa(longrand, ss, 10);

  finalss[0] = ss[0];
  finalss[1] = ss[1];
  finalss[2] = ss[2];
  finalss[3] = '-';
  finalss[4] = ss[3]; 
  finalss[5] = ss[4]; 
  finalss[6] = '-';
  finalss[7] = ss[5]; 
  finalss[8] = ss[6];
  finalss[9] = ss[7]; 
  finalss[10] = ss[8];
  finalss[10] = '\0';

  Serial.println (finalss);
}

void loop() {
  ; // Do nothing
}
  • TrueRandom.random() returns a long, not a string, so you need to assign the value to a long variable while you translate it.
  • Your code didn't allocate any memory for the arrays you tried to use, so the third statement does that. I made them larger than the number of characters you used because the range you gave to TrueRandom.random() is a set of 9-digit numbers. I don't know how you need to punctuate your numbers so I allocated finalss[] larger yet. You should confirm that the array sizes will be sufficient for your needs.
  • The fifth statement, ltoa() converts a long to an ascii string in the ss[] array.
  • I left your finalss[x] = ss[x];' statements alone except to add the necessary NUL terminator at the end offinalss[]`.

I haven't tested this but it compiled with no errors other than my not having TrueRandom.h. But it should get you a lot closer to (what it looks to me as if) you intended.

JRobert
  • 15,246
  • 3
  • 23
  • 51