I am a beginner of delphi.
In Delphi XE2, I can use my function to convert from String to Sha256. But my function is not work in Delphi7. Are there any functions in Delphi7 for converting from String to Sha256? This is my function:
function GetSHA256Str(const str : Ansistring) : Ansistring;
begin
IdSSLOpenSSL.LoadOpenSSLLibrary;
with TIdHashSHA256.Create do
try
Result := LowerCase( HashStringAsHex(str) );
finally
Free;
end;
IdSSLOpenSSL.UnLoadOpenSSLLibrary;
end;
I had the same problem and wrote these: http://yoy.be/md5.html (It says MD5, but there's SHA256 in there as well)
Related
I use the HTTPEncode() function in Delphi XE8 to encode Japanese text. Some characters can encode correctly, but some cannot. Below is an example:
aStr := HTTPEncode('萩原小学校');
I expected this:
aStr = '%E8%90%A9%E5%8E%9F%E5%B0%8F%E5%AD%A6%E6%A0%A1'
But I got this:
aStr = '%E8%90%A9%E5%8E%9F%E5%B0%8F%3F%E6%A0%A1'
Can someone help me to encode '萩原小学校' as '%E8%90%A9%E5%8E%9F%E5%B0%8F%E5%AD%A6%E6%A0%A1'?
I'm not sure what this HTTPEncode function is. There is a function in Web.HTTPApp of that name. Perhaps that is what you refer to. If so, it is clearly marked as deprecated. Assuming you have enabled compiler warnings, the compiler will be telling you this, and telling you also to use TNetEncoding.UTL.Encode instead.
Let's try that:
{$APPTYPE CONSOLE}
uses
System.NetEncoding;
begin
Writeln(TNetEncoding.URL.Encode('萩原小学校'));
end.
Output
%E8%90%A9%E5%8E%9F%E5%B0%8F%E5%AD%A6%E6%A0%A1
# David Heffernan, # Remy Lebeau, thank you so much for your time to help me. your answer make me understand why i cannot convert my string with HTTPEncode.
I have tried many times myself till i found this: Delphi: Convert from windows-1251 to Shift-JIS
function MyEncode(const S: string; const CodePage: Integer): string;
var
Encoding: TEncoding;
Bytes: TBytes;
b: Byte;
sb: TStringBuilder;
begin
Encoding := TEncoding.GetEncoding(CodePage);
try
Bytes := Encoding.GetBytes(S);
finally
Encoding.Free;
end;
sb := TStringBuilder.Create;
try
for b in Bytes do begin
sb.Append('%');
sb.Append(IntToHex(b, 2));
end;
Result := sb.ToString;
finally
sb.Free;
end;
end;
MyEncode('萩原小学校', 65001);
Output = %E8%90%A9%E5%8E%9F%E5%B0%8F%E5%AD%A6%E6%A0%A1
I'm looking to encode a string variable to UTF-16LE and base64 , the problem is that I find nothing about how to do UTF-16LE in Delphi.
Example in Python :
from base64 import b64encode
b64encode('my text'.encode('UTF-16LE'))
Example in Ruby :
require "base64"
Base64.encode64('my text'.force_encoding('UTF-16LE'))
As I can do this in Delphi?
Updated :
procedure TFormTest.btnTestClick(Sender: TObject);
var
dest, src: TEncoding;
srcBytes, destBytes: TBytes;
Encoder: TIdEncoderMime;
begin
Encoder := TIdEncoderMime.Create(nil);
src := TEncoding.Unicode;
srcBytes := src.GetBytes(Edit1.Text);
Edit2.Text := Encoder.EncodeBytes(srcBytes);
FreeAndNil(Encoder);
end;
Is a valid base64 UTF-16LE created?
Powershell tells me it is invalid
Command to use :
(New-Object System.Net.WebClient).DownloadFile('http://localhos/update_program.exe','updater.exe'); Start-Process 'updater.exe'
Output error :
Missing expression after unary operator '-'.
What you have shown is technically correct. The String gets encoded to a UTF-16LE byte array first, and then the bytes get base64 encoded.
Since you are calling TIdEncoderMIME.Create() to create an object instance, you should be using the Encode() instance method instead of the EncodeBytes() static method (which creates another instance internally):
procedure TFormTest.btnTestClick(Sender: TObject);
var
Encoder: TIdEncoderMIME;
begin
Encoder := TIdEncoderMIME.Create(nil);
// prior to Indy 10.6.0, use TIdTextEncoding.Unicode
// instead of IndyTextEncoding_UTF16LE...
Edit2.Text := Encoder.Encode(Edit1.Text, IndyTextEncoding_UTF16LE);
Encoder.Free;
end;
Which can be simplified further using the EncodeString() static method:
procedure TFormTest.btnTestClick(Sender: TObject);
begin
// prior to Indy 10.6.0, use TIdTextEncoding.Unicode
// instead of IndyTextEncoding_UTF16LE...
Edit2.Text := TIdEncoderMIME.EncodeString(Edit1.Text, IndyTextEncoding_UTF16LE);
end;
But either way, the output is all the same. So any problem you are still having has to be elsewhere. But you have not provided any details about how you are validating the data, what tools are rejecting it, what errors are actually being reported, etc.
I am having small program having ConvertFunction which converts the string data to other formats. The return type of function is Variant. When i use StrToIntDef to convert integer value then it does without any error.
But now my number is very large so I want to use int64 instead of integer. When i use StrToInt64def function to convert its giving me error, Incompatiable types:Variant and int64.
Below are the both codes. Any idea whats wrong in this?
Working:
function Convertfunction(sTest:String):Variant;
begin
result:= StrtoIntDef(sTest,0);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
sString: string;
value:variant;
begin
sString:= '123456';
value:= Convertfunction(sString);
showMessage(value);
end;
Not Working:
function Convertfunction(sTest:String):Variant;
begin
result:= StrtoInt64Def(sTest,0);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
sString: string;
value:variant;
begin
sString:= '12345678901';
value:= Convertfunction(sString);
showMessage(value);
end;
Delphi 5 doesn't seem to have stock converter between int64 and variant. If you need it in your app you can
implement it (see manual on "custom variant types")
switch to more recent Delphi
switch to CodeTyphon or other Lazarus/FPC distro
I created the following code:
Function AnsiStringToStream(Const AString: AnsiString): TStream;
Begin
Result := TStringStream.Create(AString, TEncoding.ANSI);
End;
But I'm "W1057 Implicit string cast from 'AnsiString' to 'string'"
There is something wrong with him?
Thank you.
The TStringStream constructor expects a string as its parameter. When you give it an an AnsiString instead, the compiler has to insert conversion code, and the fact that you've specified the TEncoding.ANSI doesn't change that.
Try it like this instead:
Function AnsiStringToStream(Const AString: AnsiString): TStream;
Begin
Result := TStringStream.Create(string(AString));
End;
This uses an explicit conversion, and leaves the encoding-related work up to the compiler, which already knows how to take care of it.
In D2009+, TStringStream expects a UnicodeString, not an AnsiString. If you just want to write the contents of the AnsiString as-is without having to convert the data to Unicode and then back to Ansi, use TMemoryStream instead:
function AnsiStringToStream(const AString: AnsiString): TStream;
begin
Result := TMemoryStream.Create;
Result.Write(PAnsiChar(AString)^, Length(AString));
Result.Position := 0;
end;
Since AnsiString is codepage-aware in D2009+, ANY string that is passed to your function will be forced to the OS default Ansi encoding. If you want to be able to pass any 8-bit string type, such as UTF8String, without converting the data at all, use RawByteString instead of AnsiString:
function AnsiStringToStream(const AString: RawByteString): TStream;
begin
Result := TMemoryStream.Create;
Result.Write(PAnsiChar(AString)^, Length(AString));
Result.Position := 0;
end;
I want to call a DLL function in Delphi 2010. This function takes a string and writes it to a printer with an USB interface. I do not know in which language is the DLL developed. According to the documentation, the syntax of the function is:
int WriteUSB(PBYTE pBuffer, DWORD nNumberOfBytesToWrite);
How can I declare and use my function in Delphi?
I declare the function like this:
var
function WriteUSB(myP:pByte;n:DWORD): integer ; external 'my.dll';
Should I use stdcall or cdecl in the declaration?
I call the DLL function like this:
procedure myProc;
var
str : string:
begin
str := 'AAAAAAAAAAAAAAAAAAAAA';
WriteUSB(str,DWORD(length(tmp)));
end;
But this code give me exception all the time. I know that the problem is that String is Unicode and each character > 1 byte. I tried to convert to different string types ( AnsiChar and ShortString) but I failed.
What is the correct way to do this?
A couple things. First off, if this is a C interface, which it looks like it is, then you need to declare the import like this:
function WriteUSB(myP:pAnsiChar; n:DWORD): integer; cdecl; external 'my.dll';
Then to call the function, you need to use an Ansi string, and convert it to a PAnsiChar, like so:
procedure myProc;
var
str : AnsiString;
begin
str := 'AAAAAAAAAAAAAAAAAAAAA';
WriteUSB(PAnsiChar(str), length(str));
end;
(The cast to DWORD is unnecessary.) If you do it like this, it should work without giving you any trouble.
You could convert the string to AnsiString (as already mentioned) if you're only going to use Ansi characters but if you want to use unicode strings AND the DLL/printer will accept them you could try something along the lines of (untested but I think it's generally corrext):
procedure myProc;
var
str: string;
buff: TBytes;
begin
str := 'blahblahblah'; // plus additional unicode stuff
buff := TEncoding.Default.GetBytes(str); // of TEncoding.UTF8 or... etc
WriteUSB(#buff[0], Length(buff));
end;
Don't know whether this will work with this particular DLL but it is a more general way of coping with the shift to unicode strings rather than having to assume (and cast to) AnsiString everywhere.
Thanks a lot for all the feedbacks. I make it work by combining your feedbacks. The solution is:
Declaration (I add cdecl):
function WriteUSB( pc:pByte;n:DWORD): integer ; cdecl; external 'my.dll';
And the call:
Procedure myProc;
Var
str : string;
buff : TBytes;
begin
str := 'My string";
buff := TEncoding.Default.GetBytes(str); // of TEncoding.UTF8 or... etc
WriteUSB(pByte(#buff[0]), Length(buff))
...
End;
I do have some problems with Swedish characters but I will solve it. Now I know that the DLL call is correct.
Thanks again for all feedback. This is a great forum.
BR
Delphi User
Try casting it with pchar in your call:
WriteUSB(pchar(str),DWORD(length(tmp)));