Shop OBEX P1 Docs P2 Docs Learn Events
Saving decimal values in array elements as characters — Parallax Forums

Saving decimal values in array elements as characters

NicopowersNicopowers Posts: 12
edited 2016-08-19 23:52 in Propeller 1
Hi everybody,

I am having difficulties formatting data to be transferred over ZigBee API communication using 64-bit addressing. Let's assume that I want to transfer the decimal value 41,410 through an API method that takes string values as its input. So, let's say:
VAR
byte   my_buffer[100]

PUB Main   |  my_value
my_value := 41410

How would I save each individual digit of my decimal variable to one element of "my_buffer" array to achieve the following?:
my_buffer[0] := "4"
my_buffer[1] := "1"
my_buffer[2] := "4"
my_buffer[3] := "1"
my_buffer[4] := "0"   ' The values need to be saved as a string

We need to make it work for any decimal value , I would really appreciate your help.
Thank you!

Comments

  • Look at the ".dec" method of any object that has one (TV or VGA or LCD display drivers for example or FullDuplexSerial). These generally call a character output routine. You can set a pointer value (word or long) with "p := @my_buffer", then store characters with "byte[p++] := c". You might need a zero byte as a string delimiter with "byte[p] := 0".
  • localroger posted some code which in clues the object "strfmt.spin".

    This object includes a ".dec" method like Mike Green suggested but this method lets you specify where to have the characters stored.

    Using your example, you'd use the following code:
      Format.dec(@my_buffer, my_value)
    

    Of course this assumes you're using "Format" to identify the "strfmt.spin" object.
Sign In or Register to comment.