[extended ASCII] StringGetChar and CharToStr are not identical

 

Hello,

I am working on some functions such as base64_encode and base64_decode. https://www.mql5.com/en/code/8098

But I realize that StringGetChar and CharToStr do not give the same result with extended ASCII (ASCII code > 127 and code < 256)

Is this a bug?

void testStringChar(ushort chr)
{
  string str = CharToStr( chr );
  ushort value = StringGetChar(str, 0);
  PrintFormat("testStringChar: %d  % d  %d", chr, value, GetLastError());
}

Test Code:

  for (ushort i=0; i < 256; ++i)
  {
     testStringChar(i);
  }


Result:

testStringChar: 165   165  0
testStringChar: 164   164  0
testStringChar: 163   163  0
testStringChar: 162   162  0
testStringChar: 161   161  0
testStringChar: 160   160  0
testStringChar: 159   376  0
testStringChar: 158   382  0
testStringChar: 157   157  0
testStringChar: 156   339  0
testStringChar: 155   8250  0
testStringChar: 154   353  0
testStringChar: 153   8482  0
testStringChar: 152   732  0
testStringChar: 151   8212  0
testStringChar: 150   8211  0
testStringChar: 149   8226  0
testStringChar: 148   8221  0
testStringChar: 147   8220  0
testStringChar: 146   8217  0
testStringChar: 145   8216  0
testStringChar: 144   144  0
testStringChar: 143   143  0
testStringChar: 142   381  0
testStringChar: 141   141  0
testStringChar: 140   338  0
testStringChar: 139   8249  0
testStringChar: 138   352  0
testStringChar: 137   8240  0
testStringChar: 136   710  0
testStringChar: 135   8225  0
testStringChar: 134   8224  0
testStringChar: 133   8230  0
testStringChar: 132   8222  0
testStringChar: 131   402  0
testStringChar: 130   8218  0
testStringChar: 129   129  0
testStringChar: 128   8364  0
testStringChar: 127   127  0
testStringChar: 126   126  0
testStringChar: 125   125  0
testStringChar: 124   124  0
testStringChar: 123   123  0
testStringChar: 122   122  0


Base64
Base64
  • votes: 19
  • 2008.07.01
  • MetaQuotes
  • www.mql5.com
Base64 encoding was developed to provide the possibility of transferring binary data through transport channels that are not purely 8-bit, like the message body. The encoded data consumes about 33% less volume as compared to the original data. The encoding is often used for a simple encryption of data. The library consists of two functions...
 
MarkJoy:

Hello,

I am working on some functions such as base64_encode and base64_decode. https://www.mql5.com/en/code/8098

But I realize that StringGetChar and CharToStr do not give the same result with extended ASCII (ASCII code > 127 and code < 256)

Is this a bug?


Test Code:


Result:


This is still a bug, I am trying to overcome it. 

If anyone has a good solution, I would love to hear.

 
Tom Sasson:

This is still a bug, I am trying to overcome it. 

If anyone has a good solution, I would love to hear.

There is no bug, mql4 and mql5 are using Unicode string. When you print an Unicode value, don't expect to get an ASCII code.

If your issue is about BASE64, you can use CryptEncode()/CryptDecode() functions.

Reason: