# Problems parsing an UTF-8 string backwards

**URL:** <https://discourse.processing.org/t/problems-parsing-an-utf-8-string-backwards/13294>\
**Category:** Coding Questions\
**Created:** [August 10, 2019, 3:37pm UTC](https://discourse.processing.org/t/problems-parsing-an-utf-8-string-backwards/13294 "2019-08-10T15:37:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [August 10, 2019, 3:37pm UTC](https://discourse.processing.org/t/problems-parsing-an-utf-8-string-backwards/13294/1 "2019-08-10T15:37:35Z")

</div>

If I go forward like

```auto
String str = "This is a string containing different encodings. В этой строке есть кириллица, например (2 bytes per char)";
for (int i=0; i<str.length(); i++) {
  println(str.charAt(i));
}

```

it works fine.  
But if I then need to check a previous char `str.charAt(i-1)` and that char is encoded with more than 1 byte I get garbage.

any ideas how to solve this?

---

<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 10, 2019, 4:28pm UTC](https://discourse.processing.org/t/problems-parsing-an-utf-8-string-backwards/13294/2 "2019-08-10T16:28:10Z")

</div>

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/StringBuilder.html#reverse()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/StringBuilder.html#reverse())

```auto
/**
 * Reverse Unicode String w/ Surrogate Characters (v1.1)
 * GoToLoop (2019/Aug/10)
 * Discourse.Processing.org/t/problems-parsing-an-utf-8-string-backwards/13294/2
 */

static final String ORIGINAL =
  "This is a string containing different encodings.\n" + 
  "В этой строке есть кириллица, например (2 bytes per char).";

static final String REVERSED = reverseString(ORIGINAL);

static final color FG = #FFFF00, BG = #0000FF;
static final int FONT_SIZE = 030;

void setup() {
  size(900, 200);
  noLoop();

  fill(FG);
  textSize(FONT_SIZE);
  textAlign(CENTER, CENTER);

  println(ORIGINAL + ENTER + ENTER + REVERSED);
}

void draw() {
  final int cx = width >> 1, qy = height >> 2;
  background(BG);

  text(ORIGINAL, cx, qy);
  text(REVERSED, cx, 3*qy);
}

static final String reverseString(final String original) {
  return new StringBuilder(original).reverse().toString();
}

```

---

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [August 10, 2019, 7:07pm UTC](https://discourse.processing.org/t/problems-parsing-an-utf-8-string-backwards/13294/3 "2019-08-10T19:07:53Z")

</div>

![2019-08-10](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4c4dbee5b70449374f2ca87c2c1a1561d22061bf.png)  
I guess my problem is local then. Some settings must have been went off with the console fonts, I guess…

…I sense the flavor of «da Old School» in your code here: `width >> 1` `height >> 2` as we were writing back in those days 🙂
