Enter ascii code inside quotes c ++

2

Could you tell me how to insert ascii codes in "" ?

For example insert 3 (decimal) which in ascii means End of text within char asd = "" .

Or within string asd = ""

And I also saw that \n is used to jump a line but the ascii code for that is 10 (decimal) then why is \n used? Are there other \ something that mean ascii codes? Thanks, I could not find this anywhere

    
asked by rusty 13.05.2017 в 22:53
source

2 answers

2

To insert 3 in decimal (which equals 03 in hexadecimal) you would use "\x03"
The escape codes in C ++ are:

Secuencia 
de Escape Descripción      Representación
   \'     comilla simple    byte 0x27 codificado ASCII
   \"     doble comilla     byte 0x22 codificado ASCII
   \?     interrogante      byte 0x3f codificado ASCII
   \     barra invert.     byte 0x5c codificado ASCII
   \a     campana sonora    byte 0x07 codificado ASCII
   \b     borrar atrás      byte 0x08 codificado ASCII
   \f     alimentación      byte 0x0c codificado ASCII
          formulario - nueva página     
   \n     alimentación      byte 0x0a codificado ASCII 
         de linea - nueva página      
   \r     retorno de carro  byte 0x0d codificado ASCII  
   \t     tabulador         byte 0x09 codificado ASCII 
         horizontal        
   \v     tabulador         byte 0x0b codificado ASCII
         vertical          
   \nnn   cualquier valor   byte nnn 
         octal             
   \xnn   cualquier valor   byte nn
         hexadecimal          
   \unnnn nombre universal  code point U+nnnn
         de carácter(*)     
   \Unnnnnnnn nombre        code point U+nnnnnnnn 
         universal de caracter(*)

(*) These are unicode codes. And they can generate several characters.

Translated from cppreference.com under license Creative Commons Attribution-Sharealike 3.0 Unported License

    
answered by 13.05.2017 в 23:18
0

could you tell me how to insert ascii codes in ""?

if within quotes it refers something like this:

  • std::string dentro_de_comillas_1 = "\n";

or so:

  • dentro_de_comillas += (char)test;
// Example program
#include <iostream>
#include <string>

int main()
{
  int test = 10; // -> \n en ascii

  std::string dentro_de_comillas = "dentro_de_comillas";
  dentro_de_comillas += (char)test;

  std::cout << dentro_de_comillas;
  std::string name;
  std::cout << "What is your name? " << (char)test;
  std::cout << "Hello, " << name << "!\n";

  std::string dentro_de_comillas_1 = "\n";
  std::cout << dentro_de_comillas;
  std::cout << "What is your name? " << (char)test;
}

stdout:

dentro_de_comillas
What is your name? 
Hello, !
dentro_de_comillas
What is your name? 

Test Ideone

is 10 (decimal) then why is \n used?

If I understand well that part we use \n so that you can use it within a string for example, you can see below how it behaves with \n and with 10 in some cases, I hope it helps you.

// Example program
#include <iostream>
#include <string>

int main()
{



  std::cout << "What is your name? \n";
  std::cout << "What is your name?" << std::endl;

  std::cout << "---" << std::endl;

  std::cout << "What is your name? 10";
  std::cout << "What is your name?" << (char)10;
  std::cout << "What is your name? (char)10";
  std::cout << "What is your name?";
}

stdout:

What is your name? 
What is your name?
---
What is your name? 10What is your name?
What is your name? (char)10What is your name?
    
answered by 13.05.2017 в 23:28