Encoding confusion

I never can seem to get this right.

I have a string (read from a file) the is “encrypted” by XOR’ing each character with 0x99, which means I can decrypt it by doing the same thing again.

I am reading a file, and writing various results from the various fields into a text file. 99% of it is UINT16 values (file is loaded into a memory block)

If I SKIP these encoded strings, my output file is just what I want… but if I include them… the whole file goes bonkers (ie. Chinese glyphs), so I assume there is a change in encoding that occurs.

// read the string from the memory block
s=Decode(rawDATA.StringValue(ptr,optSize-1))

DECODE function

  dim t as string
  dim i as integer
  dim c as integer
  
  for i=1 to len(s) // has 0x00 on the end
    c=ascb(mid(s,i,1)) xor &H99
    t=t+chrb(c)
  next i
  t=t.ConvertEncoding(Encodings.UTF8) // including or excluding this line makes zero difference
// if I put a breakpoint here, T displays as the CORRECT value, but with an encoding of NIL
  return t

You need DefineEncoding, not ConvertEncoding.

ConvertEncoding: The string is properly encoded with X but you want to change the encoding, and the bytes of the string, so it is encoded in Y.

DefineEncoding: The encoding of the bytes that make up the string is unknown or incorrect so you want to properly identify it to the framework.

Same results

except now breakpoint does show UTF-8

I even added DefineEncoding to the other two places where I pull strings out of the memory block

s=rawDATA.StringValue(ptr,optSize).DefineEncoding(Encodings.UTF8)

I pull 3 strings, two are NOT encrypted, one is, all the rest are UINT16 values or BYTE value

When I view the file in a text editor… IT says the file is UTF16LE
and if I skip the decode routine… it says it is ISO-8859-1

NEVERMIND : I found it…
Turns out all the STRING values had 0x00 on the end of them! removed that, and all is good

Did you try to use a BinaryStream without encoding ?

I think (but I may be wrong) if you load a Byte from the file, XORing it will work