Home » Community » Coffee corner » operation with ascii table character (Pascal x C++/U++)
operation with ascii table character (Pascal x C++/U++) [message #56799] |
Sat, 17 April 2021 00:11  |
BetoValle
Messages: 204 Registered: September 2020 Location: Brasil Valinhos SP
|
Experienced Member |
|
|
Hi,
I'm trying to convert a routine from Pascal to U ++, which involves an operation with the number of the character in the ascii table. The problem is that in Pascal
a = ord (result [i]) = 97 , b = ord (result [i]) = 101
a xor b results 4 and this result in U ++ (a^b) is not
considered for screen printing or string accumulation: char(4)
in Pascal, char (4) is printed "x04"!
Do you have an equivalent routine?
How to solve? is there any way?
Thanks
|
|
|
Re: operation with ascii table character (Pascal x C++/U++) [message #56806 is a reply to message #56799] |
Sat, 17 April 2021 10:23   |
Oblivion
Messages: 1202 Registered: August 2007
|
Senior Contributor |
|
|
Hello BetoValle,
for(int i = 32; i < 128; ++i) {
Cout() << "\t0x" << FormatIntHex(i, 2)
<< "\t0x" << FormatIntHex(i + 1, 2)
<< "\t0x" << FormatIntHex(i ^ (i + 1), 2) << "\n";
}
// OR
Cout() << "\t-------------\n";
for(int i = 32; i < 128; ++i) {
Cout() << Format("\t0x%02x`\t0x%02x`\t0x%02x\n", i, i + 1, i ^ (i + 1));;
}
Something like this?
You can use text formatting and/or formatters, as you can see in the above example.
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Sat, 17 April 2021 10:27] Report message to a moderator
|
|
|
Re: operation with ascii table character (Pascal x C++/U++) [message #56812 is a reply to message #56799] |
Sat, 17 April 2021 16:32   |
BetoValle
Messages: 204 Registered: September 2020 Location: Brasil Valinhos SP
|
Experienced Member |
|
|
Hi Oblivion,
thanks, but that's not it. They are the unprinted characters from 1 to 32, encoded in pascal through its "chr" function. In the routine calculations are performed involving "xor" where the result will be the ascii character code from 1 to 255. If you in Pascal do the loop from 1 to 32 for example you will have results according to the array (if you paste these items the 1st vector below in the editor U ++, they will no longer be displayed)
Vector<String> arrAscii{ "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ," " ,"
" ,"" ,"" ,"
" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ," " };
//if you print in a text editor example cudatext, or more sophisticated editor you will see the contents below
Vector<String> arrAscii{ "x01" ,"x02" ,"x03" ,"x04" ,"x05" ,"x06" ,"x07" ,"x08" ," "
," " ,"x0B" ,"x0C" ," " ,"x0E" ,"x0F" ,"x10" ,"x11" ,"x12" ,"x13" ,
"x14" ,"x15" ,"x16" ,"x17" ,"x18" ,"x19" ,"x1A" ,"x1B" ,"x1C" ,"x1D" ,"x1E" ,"x1F" ," " };
CONSOLE_APP_MAIN
{
String s;
s << char(4);
Cout()<< "show " << s << EOL; // not show!
}
//pascal
procedure TForm2.SpeedButton1Click(Sender: TObject);
var
log:String;
begin
log := chr(4);
showmessage( log ); // show ""
ClipBoard.AsText := log ; // in text editor show "x04"
end;
In pascal I can accumulate the characters 1 to 32 in the string using a loop
String [i] = chr (i)
but in U ++ I can't or if it exists then I haven't learned it yet.
|
|
|
Re: operation with ascii table character (Pascal x C++/U++) [message #56816 is a reply to message #56812] |
Sat, 17 April 2021 18:20   |
Oblivion
Messages: 1202 Registered: August 2007
|
Senior Contributor |
|
|
Hello BetoValle,
I see. AFAIK there is no such formatter in U++ (it is too specific, IMO).
But it is easy to create one, using operator overloading, for example.
String& operator*(String& s, int c)
{
if(c >= 0x20 && c <= 0x7E)
s.Cat(c);
else
s << "0x" << FormatIntHex(c, c < 256 ? 2 : 4);
return s;
}
String& operator*(String& s, const String& q)
{
for(int c : q) s * c;
return s;
}
CONSOLE_APP_MAIN
{
Vector<String> v;
for(int i = 0; i < 256; i++) v.Add() * i;
Cout() << v.ToString();
}
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Sat, 17 April 2021 18:22] Report message to a moderator
|
|
|
Re: operation with ascii table character (Pascal x C++/U++) [message #56820 is a reply to message #56816] |
Sat, 17 April 2021 22:05   |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
Oblivion wrote on Sat, 17 April 2021 18:20Hello BetoValle,
I see. AFAIK there is no such formatter in U++ (it is too specific, IMO).
But it is easy to create one, using operator overloading, for example.
String& operator*(String& s, int c)
{
if(c >= 0x20 && c <= 0x7E)
s.Cat(c);
else
s << "0x" << FormatIntHex(c, c < 256 ? 2 : 4);
return s;
}
String& operator*(String& s, const String& q)
{
for(int c : q) s * c;
return s;
}
CONSOLE_APP_MAIN
{
Vector<String> v;
for(int i = 0; i < 256; i++) v.Add() * i;
Cout() << v.ToString();
}
Best regards,
Oblivion
BTW, Format is extensible, so you can in fact add this somewhat weird formatter to Format..
|
|
|
|
Re: operation with ascii table character (Pascal x C++/U++) [message #56825 is a reply to message #56799] |
Sun, 18 April 2021 00:12  |
BetoValle
Messages: 204 Registered: September 2020 Location: Brasil Valinhos SP
|
Experienced Member |
|
|
Hi,
I thank you all very much. The routine was this low, of simple encryption. To decrypt the function is the same. The issue of content resulting from encryption does not need to be displayed (internally the contents are stored, for example in the database) and when decrypting the content recovery, it is correct and logically visible. Only that the function can be improved, but it is a my start.
"mStr" is content you want to encrypt
"mchave" is encryption key content
String Criptografia(String mStr, String mChave)
{
String s;
int pos,posLetra;
mChave=mChave;
int TamanhoString = mStr.GetLength();
int TamanhoChave = mChave.GetLength();
for(int i = 0; i < TamanhoString; i++){
pos = (i % TamanhoChave);
if( pos == 0)
pos = TamanhoChave;
int a = mStr[i];
int b = mChave[pos];
posLetra = a ^ b;
if (posLetra == 0)
posLetra = mStr[i];
s << char(posLetra);
}
return s;
}
CONSOLE_APP_MAIN
{
//first time you encripts
String a=Criptografia("José da Silva","efg");
//second time you decrips
String b=Criptografia(a,"efg");
Cout()<< b << EOL;
}
|
|
|
Goto Forum:
Current Time: Fri Apr 25 17:18:18 CEST 2025
Total time taken to generate the page: 0.00730 seconds
|