Go Down

Topic: Bin Dec Hex Oct Converter (Read 1 time) previous topic - next topic

kopite

Hi,

Below is a quick and simple program that should convert a decimal value into binary. At the moment it converts any value to Bin.

A few questions...

1) How can I ensure that the user input in the serial monitor is a decimal value?

2) If I wanted to convert from Hex to Bin, how would i do this and again, how would I ensure that the user input was Hex?

Many Thanks

Code: [Select]
int val;

void setup() {
//Initialize serial and wait for port to open:
  Serial.begin(9600);
   // prints text and menu options for user to select from

void loop() {
   if (Serial.available())  //Constantly checks to see if anything has been sent over the USB Connection and if it needs to be processed
   {
     val = Serial.read(); //Reads a single letter
     //Serial.println(val);
     Serial.print(val, BIN); 
  }}

AWOL

#1
Dec 14, 2012, 11:11 am Last Edit: Dec 14, 2012, 11:17 am by AWOL Reason: 1
Quote
1) How can I ensure that the user input in the serial monitor is a decimal value?

By rejecting characters other than '0' to '9' inclusive.

Quote
2) If I wanted to convert from Hex to Bin, how would i do this and again, how would I ensure that the user input was Hex?

By rejecting characters other than '0' to '9' inclusive or 'a' to 'f' or 'A' to 'F'

Ultimately, everything is binary.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon


1) How can I ensure that the user input in the serial monitor is a decimal value?


See: isdigit function.


2) If I wanted to convert from Hex to Bin, how would i do this and again, how would I ensure that the user input was Hex?


See: isxdigit function.

kopite

Many thanks for both I your replies.

As I'm a beginner I have never herd of isdigit function etc. I will do some digging on this.

kopite

Hi,

Below is the code for my program, it takes the user input when making a choice from the menu and then takes the user to convert between the specified types.

A couple of questions. Ive used the parseInt for the dec to other ypes but how can i make sure the result for example in binary is 8-bit. This is because i need the user to be able to put in a number between -255 and 255. The minus returns the binary in 32-bit, I need it to be 8-bit.

Also, is there any calculations (or anyway in fact-without using a similar thing to isxdigit) that can ensure the user input is eith HEX, OCT or binary?

Many thanks once again.

Code: [Select]
void setup() {
//Initialize serial and wait for port to open:
  Serial.begin(9600);
   // prints text and menu options for user to select from
  Serial.println(F("Welcome to my Bin, Dec, Oct, Hex Converter"));
  delay(500);
  Serial.println(F(" "));
  Serial.println(F("Please select a number from the options available..."));
  Serial.println(F("*Please Note* The options below are Case Sensitive!"));
  Serial.println(F(" "));
  Serial.println(F("Main Menu"));
  Serial.println(F("---------"));
  Serial.println(F("a) 2's comp Binary to Hex,"));
  Serial.println(F("b) 2's comp Binary to Octal,"));
  Serial.println(F("c) 2's comp Binary to Decimal,"));
  Serial.println(F("d) Octal to 2's comp Binary,"));
  Serial.println(F("e) Octal to Hex,"));
  Serial.println(F("f) Octal to Decimal,"));
  Serial.println(F("g) Hex to 2's comp Binary,"));
  Serial.println(F("h) Hex to Octal,"));
  Serial.println(F("i) Hex to Decimal,"));
  Serial.println(F("j) Decimal to 2's comp Binary,"));
  Serial.println(F("k) Decimal to Hex,"));
  Serial.println(F("l) Decimal to Octal."));
}

void loop()
{
   int menuselect;
   int conversion1;
   int conversion2;
   int conversion3;
   int conversion4;
   int conversion5;
   int conversion6;
   int conversion7;
   int conversion8;
   int conversion9;
   int conversion10;
   int conversion11;
   int conversion12;
     
   if (Serial.available())  //Constantly checks to see if anything has been sent over the USB Connection and if it needs to be processed
    {
      menuselect = Serial.read();
      switch (menuselect) {
  case 97:   //option a) ASCII "a"
    Serial.println(F(" "));
    Serial.println(F("2's comp Binary to Hex"));
       while (Serial.available ()== 0){}
       conversion1 = Serial.read();
       Serial.println(conversion1, HEX);
             
    break;
  case 98: 
    Serial.println(F(" ")); 
    Serial.println(F("2's comp Binary to Octal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, OCT);
       
    break;
  case 99:   
    Serial.println(F(" "));
    Serial.println(F("2's comp Binary to Decimal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       conversion2 = conversion2 - '0';
       Serial.println(conversion2, DEC);
       
    break;
  case 100:   
    Serial.println(F(" "));
    Serial.println(F("Octal to 2's comp Binary"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, BIN);
       
    break;
  case 101:   
    Serial.println(F(" "));
    Serial.println(F("Octal to Hex"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, HEX);
       
    break;
  case 102:
    Serial.println(F(" "));
    Serial.println(F("Octal to Decimal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, DEC);
       
    break;
  case 103:   
    Serial.println(F(" "));
    Serial.println(F("Hex to 2's comp Binary"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, BIN);
       
    break;
  case 104:
    Serial.println(F(" "));
    Serial.println(F("Hex to Octal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, OCT);
       
    break;
  case 105:   
    Serial.println(F(" "));
    Serial.println(F("Hex to Decimal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.read();
       Serial.println(conversion2, DEC);
       
    break;
  case 106:   
    Serial.println(F(" "));
    Serial.println(F("Decimal to 2's comp Binary"));
    while (Serial.available ()== 0){}
       conversion2 = Serial.parseInt();
       Serial.println(conversion2, BIN);
       
    break;
  case 107:
    Serial.println(F(" ")); 
    Serial.println(F("Decimal to Hex"));
    while (Serial.available ()== 0){}
       conversion2= Serial.parseInt();
       Serial.println(conversion2, HEX);
       
    break;
  case 108: 
    Serial.println(F(" ")); 
    Serial.println(F("Decimal to Octal"));
    while (Serial.available ()== 0){}
       conversion2= Serial.parseInt();
       //if using serial.read i could of used conversion2 = conversion2 - '0'
       Serial.println(conversion2, OCT);
       
    break;
  } }}
     
     

Go Up