Converting an ASCII string to a byte array and vica-versa

Discussions specific to Team Developer 5.1.

Converting an ASCII string to a byte array and vica-versa

Postby SteveLeighton » Mon Jun 28, 2010 5:29 pm

Good morning !
How do I convert an ASCII string to a byte array ( and vica-versa ) ?

I have a 509 char string of XML that I need to run through the CryptoSys dll - SHA1_BytesHash() which takes 3 Parameters:

abDigest
[out] Byte array to receive message digest.

abData
[in] Byte array containing the message to be hashed.

nDataLen
[in] Long containing number of bytes in the array.

I need to get my ASCII string into the 'abData' param.

Any hints greatly appreciated !
Best wishes
Steve
Bankside Systems Ltd. ( New Zealand )
http://www.banksidesystems.co.uk
Image
Go forward! Come join the TD, SqlBase & TDMobile community on
Team Developer Community Forum
User avatar
SteveLeighton
 
Posts: 114
Joined: Tue Jan 15, 2008 7:50 am
Location: Tauranga, New Zealand

Re: Converting an ASCII string to a byte array and vica-versa

Postby lrcuess » Mon Jun 28, 2010 10:45 pm

Hi,

the definition should look like this:
Code: Select all
Function: SHA1_BytesHash
   Description:
   Export Ordinal: 0
   Returns
      Number: LONG
   Parameters
      Receive Binary: LPBYTE
         ! [out] abDigest
      String: LPSTR
         ! [in] abData
      Number: LONG
         ! [in] nDataLen


The second parameter is defined as string with LPSTR, so it's passed as single byte string.
The third parameter will take the string length.
I defined the first parameter as Binary, because it will receive data that is no simple string.
You have to size the binary buffer right, else you might get severe errors during execution.
I found a documentation that says, the buffer hast to be "at lease" 20 bytes, but I'm not sure, if more could be needed.

I hope, this helps!
Max
Markus Eßmayr
RACON Software GmbH
http://www.racon.at
User avatar
lrcuess
 
Posts: 1163
Joined: Mon May 07, 2007 5:58 am
Location: Linz, Austria

Re: Converting an ASCII string to a byte array and vica-versa

Postby SteveLeighton » Mon Jun 28, 2010 10:52 pm

Beautiful . Thanks Max.
Best wishes
Steve
Bankside Systems Ltd. ( New Zealand )
http://www.banksidesystems.co.uk
Image
Go forward! Come join the TD, SqlBase & TDMobile community on
Team Developer Community Forum
User avatar
SteveLeighton
 
Posts: 114
Joined: Tue Jan 15, 2008 7:50 am
Location: Tauranga, New Zealand

Re: Converting an ASCII string to a byte array and vica-versa

Postby SteveLeighton » Tue Jun 29, 2010 1:24 am

.....but please tell Max, what special magic allowed you to declare an external function parameter as
'Receive Binary'
Code: Select all
Parameters
      Receive Binary: LPBYTE


The only LPBYTE type I could declare is
Code: Select all
Parameters
      Receive Number: LPBYTE
Best wishes
Steve
Bankside Systems Ltd. ( New Zealand )
http://www.banksidesystems.co.uk
Image
Go forward! Come join the TD, SqlBase & TDMobile community on
Team Developer Community Forum
User avatar
SteveLeighton
 
Posts: 114
Joined: Tue Jan 15, 2008 7:50 am
Location: Tauranga, New Zealand

Re: Converting an ASCII string to a byte array and vica-versa

Postby lrcuess » Tue Jun 29, 2010 1:40 am

Oh, I'm sorry.
The Binary datatype is available in TD 5.2 and above, but not in the 5.1 version.
So you'll have to change this to a String type with LPSTR.
But keep in mind, that this is not a real string as it contains binary data.

Max
Markus Eßmayr
RACON Software GmbH
http://www.racon.at
User avatar
lrcuess
 
Posts: 1163
Joined: Mon May 07, 2007 5:58 am
Location: Linz, Austria

Re: Converting an ASCII string to a byte array and vica-versa

Postby SteveLeighton » Tue Jun 29, 2010 1:43 am

Ok . Thanks again.
Best wishes
Steve
Bankside Systems Ltd. ( New Zealand )
http://www.banksidesystems.co.uk
Image
Go forward! Come join the TD, SqlBase & TDMobile community on
Team Developer Community Forum
User avatar
SteveLeighton
 
Posts: 114
Joined: Tue Jan 15, 2008 7:50 am
Location: Tauranga, New Zealand

Re: Converting an ASCII string to a byte array and vica-versa

Postby Jeff Luther » Tue Jun 29, 2010 3:16 pm

Here's how I declared that function in v4.2:
Code: Select all
Function: SHA1_BytesHash
   Description: long SHA1_BytesHash(unsigned char *digest, const unsigned char *bytes, long len)
   Export Ordinal: 0
   Returns
      Number: LONG
   Parameters
      Receive String: LPVOID
      String: LPSTR
      Number: LONG

I didn't convert that to v5.x, but I do have a demo. of a few of David's CryptoSys.DLL functions: http://www.jeffluther.net/unify/#Code_Samples
and see the CRYPTO section. Those demos should work for TD up to and including v5.2.

Note, I originally had to 'play' with declarations and so on to fine-tune and get the CryptoSys functions working for TD. You'll likely need to do the same for v5.1.
Jeff Luther
 

Re: Converting an ASCII string to a byte array and vica-versa

Postby SteveLeighton » Tue Jun 29, 2010 10:32 pm

Jeff Thanks alot for all help.
Best wishes
Steve
Bankside Systems Ltd. ( New Zealand )
http://www.banksidesystems.co.uk
Image
Go forward! Come join the TD, SqlBase & TDMobile community on
Team Developer Community Forum
User avatar
SteveLeighton
 
Posts: 114
Joined: Tue Jan 15, 2008 7:50 am
Location: Tauranga, New Zealand


Return to Team Developer 5.1

Who is online

Users browsing this forum: No registered users and 1 guest

cron