# Use of saveStrings

**URL:** https://discourse.processing.org/t/use-of-savestrings/13482
**Category:** Beginners
**Created:** [August 19, 2019, 9:08pm UTC](https://discourse.processing.org/t/use-of-savestrings/13482 "2019-08-19T21:08:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![BeginnerForever](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@BeginnerForever](https://discourse.processing.org/u/BeginnerForever)
#### Post date: [August 19, 2019, 9:08pm UTC](https://discourse.processing.org/t/use-of-savestrings/13482/1 "2019-08-19T21:08:59Z")

</div>

Hi ! I’m very new to processing and java, and for one of my first programs i need to create data file in .txt to store some variables… but when i use the syntax below, in the .txt file i have this :  
-2097152  
red

… i can’t figure out why the #e00000 (red colour) turned into “-2097152”…

is my code right ? Thanks for help and sorry for bad english 😓

here is my code :

```auto
String preferences;
final int backgroundcolorreference= #000000;
int backgroundcolor;
String clr;

void setup(){
  size(800,600);
  background(backgroundcolorreference);
}

void draw(){
  if (keyPressed==true){
    if (keyCode==UP){
      backgroundcolor=#ffffff;
      clr="white";
    }
    if (keyCode==DOWN){
      backgroundcolor=#e00000;
      clr="red;
    }
    if (keyCode==LEFT){
      preferences=backgroundcolor+" "+clr;
      String[] tosave = split(preferences," ");
      saveStrings("data/saveddata.txt",tosave);
      exit();
     
    }
    background(backgroundcolor);
  }
}

```

---

<div class="post-metadata">

### Author: ![aaronshenhao](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@aaronshenhao](https://discourse.processing.org/u/aaronshenhao)
#### Post date: [August 19, 2019, 11:50pm UTC](https://discourse.processing.org/t/use-of-savestrings/13482/2 "2019-08-19T23:50:03Z")

</div>

A quotation mark seems to be missing after `clr = “red”`, but I’m not sure if that’s a copying issue.

Use the `hex()` function before writing as a string, or else it will get converted to a number (see first example in the reference): [https://processing.org/reference/hex\_.html](https://processing.org/reference/hex_.html)

Eg: `preferences = hex(backgroundcolor)+...`

Also, welcome to the forums 👋.

---

<div class="post-metadata">

### Author: ![BeginnerForever](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@BeginnerForever](https://discourse.processing.org/u/BeginnerForever)
#### Post date: [August 20, 2019, 8:06am UTC](https://discourse.processing.org/t/use-of-savestrings/13482/3 "2019-08-20T08:06:11Z")

</div>

Ok ! It actually works ! Thanks 😄

---

<div class="post-metadata">

### Author: ![BeginnerForever](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@BeginnerForever](https://discourse.processing.org/u/BeginnerForever)
#### Post date: [August 20, 2019, 3:29pm UTC](https://discourse.processing.org/t/use-of-savestrings/13482/4 "2019-08-20T15:29:37Z")

</div>

Hey again ! sorry to ask something again but i can’t figure out how to get the color i saved into “saveddata.txt”

```auto
String preferences;
final int backgroundcolorreference= #000000;
int backgroundcolor;
String clr;

void setup(){
  size(800,600);
  background(backgroundcolorreference);
  String[] loaded= loadStrings("data/saveddata.txt");
  // backgroundcolor= and i dont know what to put here
  background(backgroundcolor);
  clr = loaded[1];
  print(clr," ",backgroundcolor);
}

void draw(){
  if (keyPressed==true){
    if (keyCode==UP){
      backgroundcolor=#ffffff;
      clr="white";
    }
    if (keyCode==DOWN){
      backgroundcolor=#e00000;
      clr="red";
    }
    if (keyCode==LEFT){
      preferences=hex(backgroundcolor)+" "+clr;
      String[] tosave = split(preferences," ");
      saveStrings("data/saveddata.txt",tosave);
      exit();
     
    }
    background(backgroundcolor);
  }
} 

```

thanks again

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [August 20, 2019, 3:46pm UTC](https://discourse.processing.org/t/use-of-savestrings/13482/5 "2019-08-20T15:46:49Z")

</div>

> [@aaronshenhao](#):
>
> , or else it will get converted to a number…

Being pedantic here: it’s not converted to a number, a Processing `color` is a number already!

More precisely, a 32-bit (4-byte) `int` datatype representing the 4 octets ARGB:

> **[color / Reference](https://processing.org/reference/color_datatype.html)**
>
> Datatype for storing color values. Colors may be assigned with get() and color() or they may be specified directly using hexadecimal notation such as #FFCC00 or 0xFFFFCC00.…

> [@BeginnerForever](#):
>
> … but I can’t figure out how to get the `color` I saved into “saveddata.txt”.

Given you’re using **hex()** in order to convert a `color` value to its hexadecimal representation as a String before **saveStrings()**:

> **[hex() / Reference](https://processing.org/reference/hex_.html)**
>
> Converts an int, byte, char, or color to a String containing the equivalent hexadecimal notation. For example, the color value produced by color(0, 102, 15…

You’re gonna need to convert it back to a `color` value after **loadStrings()**, given Processing can’t use a String in place of a `color`.

Processing’s API has the very convenient function **unhex()** for it btW:

> **[unhex() / Reference](https://processing.org/reference/unhex_.html)**
>
> Converts a String representation of a hexadecimal number to its equivalent integer value.

`println(#FF0000, unhex(hex(#FF0000)) == #FF0000); // -65536 true`

---

<div class="post-metadata">

### Author: ![BeginnerForever](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@BeginnerForever](https://discourse.processing.org/u/BeginnerForever)
#### Post date: [August 20, 2019, 4:20pm UTC](https://discourse.processing.org/t/use-of-savestrings/13482/6 "2019-08-20T16:20:09Z")

</div>

Ok ! It works ! thank you man 🙂
