ASCII representation of 1234

Hello,
Please help me find a way of converting a number in to an ASCII representation.
I have int 1234 which can be written in ASCII as 30 34 44 32. How can I do this conversion in Arduino?

Thank you.

You only need ASCII representation when sending stuff, so if your int is called lets say number.
Just use

Serial.println( number);

Hi @florin_bendrea,

I am not sure how you got the numbers for your example ...

  • char '1' has a value of 0x31 = 49 (dec)
  • char '2' has a value of 0x32 = 50 (dec)

etc.

See https://en.wikipedia.org/wiki/ASCII

So a value of 1234 would be printed(!) as decimal 49,50,51,52 or hex 0x31, 0x32, 0x33, 0x34 ...

1 Like

Or displaying it on an LCD - in which case the nearly identical statement might be something like
LCDprint(number);
Though that might, I guess, be considered "sending it". The OP gives no context.

@florin_bendrea What are you doing - is this a classroom exercise?

Without knowing exactly what you want, you may have a look at this:

Sketch;

/*
  Forum: https://forum.arduino.cc/t/ascii-representation-of-1234/1198200
  Wokwi: https://wokwi.com/projects/383546889615798273


  Be aware that aNumber in printASCII() is declared here as int!
  See the range of integer variables:
  https://www.arduino.cc/reference/en/language/variables/data-types/int/

  For other types/ranges have a look at:
  https://forum.arduino.cc/t/itoa-function/975977


*/

void setup() {
  Serial.begin(115200);
  Serial.println("Print ASCII");
  Serial.println();
  printASCII(1234);
  printASCII(32567);
}

void loop() {
}


void printASCII(int aNumber) {
  char buf[40];
  itoa(aNumber,buf,DEC);
  Serial.println(buf);
  for (int i = 0;i<strlen(buf);i++){
    Serial.print(buf[i],HEX);
    Serial.print(" ");
  }
  Serial.println();
}

Good luck!

1. The picture of Fg-1 contains the ASCII codes for the alphabets of English Language. It also contains ASCII codes for the control characters of communication.


Figure-1:

2. To get the ASCII codes for the digits of the hex number 04D2 of decimal nuber 1234, you need to consult ASCII Table of Fig-1.

3. Let us express the ASCII codes for the digits 0 to 9 and A to F by the following array:

byte asciiTable[] =
{
  0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,  //0 - 9
  0x41, 0x42, 0x43, 0x44, 0x45, 0x46 //A - F
};

4. Sketch to collect/display ASCII codes for the digits of 1234 and save them in respective variables:

byte asciiTable[] =
{
  0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,  //0 - 9
  0x41, 0x42, 0x43, 0x44, 0x45, 0x46 //A - F
};

void setup()
{
  Serial.begin(9600);
  //------------------
  int y = 1234; //0x04D2 ; D0 = 0, D1 = 4, D2 = D, D3 = 2
  byte asciiD0 = asciiTable[y >> 12];    //0x30
  Serial.println(asciiD0, HEX);
  //--------------
  byte asciiD1 = asciiTable[(y >> 8) & 0x000F];   //0x
  Serial.println(asciiD1, HEX);
  //------- for D2
  byte asciiD2 = asciiTable[(y >> 4) & 0x000F];
  Serial.println(asciiD2, HEX);
  //------- for D3
  byte asciiD3 = asciiTable[y & 0x000F];
  Serial.println(asciiD3, HEX);
}

void loop()
{

}

5. Output:

30
34
44
32

I need this ASCII conversion to send packets over RS485 network. When I fill in the fields in Modbus Poll, it creates the command I need to send in packets.
ModbusPoll

I have used a online converter and I got this: see pictures
1234 Decimal to ASCII

then I did another conversion like this: see picture

It looks like the numbers from Modbus Poll

With this packets I write the value 1234 to a single register of my PLC over RS485 network

your 1234 is not exactly what's converted

1234 is 0x04D2

you need to get the HEX RTU and then that's what's shown in ASCII

image

so you need to get the HEX representation first and then go to ASCII representation

void printHexRepresentation(uint16_t number) {
  char buffer[10];
  snprintf(buffer, sizeof buffer,  "%04X", number);
  Serial.print("Hex for ");
  Serial.print(number);
  Serial.print(" is 0x");
  Serial.print(buffer);
  Serial.print(" which is in ASCII (HEX) [");

  for (size_t i = 0; i < sizeof buffer && buffer[i] != '\0'; i++) {
    Serial.print((int) buffer[i], HEX);
    if (buffer[i+1] != '\0') Serial.write(' ');
  }
  Serial.println("]");
}

void setup() {
  Serial.begin(115200);
  printHexRepresentation(1234);
}

void loop() {}

Hex for 1234 is 0x04D2 which is in ASCII (HEX) [30 34 44 32]

Thank you guys for your answers.

[quote="GolamMostafa, post:6, topic:1198200"]
byte asciiD1 = asciiTable[(y >> 8) & 0x000F];

I would like to understand this line. What does it mean?
How can I read it in words?

for Modbus RTU you would be after the RTU array it displays not the ASCII..
could be defined as such..

const byte packet[] = {0x02, 0x06, 0x06, 0x01, 0x04, 0xD4, 0x5A, 0x2C};

good luck.. ~q

 ____ ________  ________     _____   
/_   |\_____  \ \_____  \   /  |  |  
 |   | /  ____/   _(__  <  /   |  |_ 
 |   |/       \  /       \/    ^   / 
 |___|\_______ \/______  /\____   |  
              \/       \/      |__|  
                                     

or

                                                   
                         .--,-``-.            ,--, 
     ,---,      ,----,  /   /     '.        ,--.'| 
  ,`--.' |    .'   .' \/ ../        ;    ,--,  | : 
 /    /  :  ,----,'    \ ``\  .`-    ',---.'|  : ' 
:    |.' '  |    :  .  ;\___\/   \   :;   : |  | ; 
`----':  |  ;    |.'  /      \   :   ||   | : _' | 
   '   ' ;  `----'/  ;       /  /   / :   : |.'  | 
   |   | |    /  ;  /        \  \   \ |   ' '  ; : 
   '   : ;   ;  /  /-,   ___ /   :   |\   \  .'. | 
   |   | '  /  /  /.`|  /   /\   /   : `---`:  | ' 
   '   : |./__;      : / ,,/  ',-    .      '  ; | 
   ;   |.'|   :    .'  \ ''\        ;       |  : ; 
   '---'  ;   | .'      \   \     .'        '  ,/  
          `---'          `--`-,,-'          '--'   
                                                   
2 Likes

Meaning of (in text):

byte asciiD1 = asciiTable[(y >> 8) & 0x000F];
  1. Given: y = 1234 //declaration
    ==> y = 0x04D2 //actual storage pattern

  2. y is shifted to the right by 8-bit positions; as a result, we get: 0x0004

  3. 0x0004 is ANDed (bit-by-bit) with 0x000F; as s result, we get: 0x0004.

  4. 0x04 is used as an index to consult the declared array (asciiTsble[]) to get ASCII code for the digit D1 (= D couting from left as D0, D1, D2. D3) of 0x04D2.

  5. The ASCII value so found is: 0x34.

  6. The value 0x34 is assigned to variable asciiD1 (ASCII code of D2).

In short;
The content of y is shifted to the right by 8-bit and then the upper 8-bit is discarded. The result is used as an index to obtain the ASCII code of the target digit from the array.l

if you shift right 0x04D2 by 8 bits you get 0x04, don't you ?

1 Like

Right! The write-up of post #12 is flawed realizing that the data item in y is inherently in binary (0x04D2) and NOT 1234. I am correcting my post.

There is a theory behind this that teaches us about this?
If I shift right 8 bit a number then I apply "and" rule bit by bit between [var >> 8] and 0x000F I get the index for a number in a table?

Simple mathematics, as carried out by machine instructions in the MCU.