FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Bitmap to String
Posts: 811
Joined: Tue May 06, 2008 04:28 AM
Bitmap to String
Posted: Wed Mar 24, 2010 07:50 AM
Hello All,

This shows the string as bitmap in xbrowse:
Code (fw): Select all Collapse
..
cStr := BmpToStr( hBmp )
xBrowse( cStr )
..

True and correct.

but If I use MsgInfo( cStr ).. shows only 'BM6$'.. Why?
the length is 4000 bytes..

I tried MsgInfo( AnsiToOEM(cStr) ) .. or .. MsgInfo( OEMToAnsi(cStr) ).. but almost the same with only 4 chars..

Can anybody explain please..

Regards,
FAP
Kind Regards,

Frances



Fivewin for xHarbour v18.07

xHarbour v1.2.3.x

BCC 7.3 + PellesC8 ( Resource Compiler only)

ADS 10.1 / MariaDB

Crystal Reports 8.5/9.23 DE

xMate v1.15
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Bitmap to String
Posted: Wed Mar 24, 2010 09:43 AM

MsgInfo() can not show Binary Data and also some variables.

XBrowse() function is different. It can show any kind of variable/data you can imagine. For quick debugging purposes during development, instead of MsgInfo, MsgList, etc, we can use xbrowse( uVar ). There can be no variable that XBrowse does not show in the appropriate way. It can even show objects as object inspector. You can try xbrowse( oWnd )

Regards



G. N. Rao.

Hyderabad, India
Posts: 811
Joined: Tue May 06, 2008 04:28 AM
Re: Bitmap to String
Posted: Thu Mar 25, 2010 01:04 AM
nageswaragunupudi wrote:MsgInfo() can not show Binary Data and also some variables.

XBrowse() function is different. It can show any kind of variable/data you can imagine. For quick debugging purposes during development, instead of MsgInfo, MsgList, etc, we can use xbrowse( uVar ). There can be no variable that XBrowse does not show in the appropriate way. It can even show objects as object inspector. You can try xbrowse( oWnd )



Dear RAO,

I use xBrowse( uVar ) too for debugging unusual variable and even objects.

My purpose of using ' BmpToStr() -> bmpstring ', is to attach the bmpstring to sql command something like this..
Code (fw): Select all Collapse
..
"Update table Set blobfield = Convert('" +bmpstring + "', SQL_BINARY )"   // => failed.
..


If I test it using something like this..
Code (fw): Select all Collapse
..
"Update table Set blobfield = Convert( '1234567890' , SQL_BINARY )"     // => success
..



I know there's adsFile2blob() but using SQL scalar Convert() or Cast() makes it more cleaner and simplified.


Best regards,
FAP
Kind Regards,

Frances



Fivewin for xHarbour v18.07

xHarbour v1.2.3.x

BCC 7.3 + PellesC8 ( Resource Compiler only)

ADS 10.1 / MariaDB

Crystal Reports 8.5/9.23 DE

xMate v1.15
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Bitmap to String
Posted: Thu Mar 25, 2010 03:47 AM

If you are particular of using SQL update statement to set binary data, you may construct the sql statement as:
"UPDATE TABLE SET FLDNAME = 0x" + StrToHex( <cBinaryData> )
This works for MSSql but I am not sure it works in ADS. You may try.

If we want to set directly from RDD it is simpler: ALIAS->FLDNAME := <cBinaryData>. No need to use blob functions.

Regards



G. N. Rao.

Hyderabad, India
Posts: 811
Joined: Tue May 06, 2008 04:28 AM
Re: Bitmap to String
Posted: Thu Mar 25, 2010 05:29 AM
nageswaragunupudi wrote:If you are particular of using SQL update statement to set binary data, you may construct the sql statement as:
"UPDATE TABLE SET FLDNAME = 0x" + StrToHex( <cBinaryData> )
This works for MSSql but I am not sure it works in ADS. You may try.

If we want to set directly from RDD it is simpler: ALIAS->FLDNAME := <cBinaryData>. No need to use blob functions.


Dear RAO,

I tried..
Code (fw): Select all Collapse
..
"UPDATE TABLE SET FLDNAME = Convert( '0x'" + StrToHex(  BmpToStr( oImg:hBitmap)  )+"', SQL_BINARY) "  //success!
..


but when..
Code (fw): Select all Collapse
..
xBrowse( 'Table' )  //no image displayed..
..


If I check the variable like this..
Code (fw): Select all Collapse
..
cStr := BmpToStr( oImg:hBitmap )
xBrowse( cStr )   //image was displayed.. OK
..


if I check the variable like this..
Code (fw): Select all Collapse
..
cStr := StrToHex(BmpToStr( oImg:hBitmap ))  //Or  '0x'+StrToHex(BmpToStr( oImg:hBitmap )) 
xBrowse( cStr )   //apps crashed.. NO ERROR.
..



maybe '0x' for binary? I dont know.


Best regards,
FAP
Kind Regards,

Frances



Fivewin for xHarbour v18.07

xHarbour v1.2.3.x

BCC 7.3 + PellesC8 ( Resource Compiler only)

ADS 10.1 / MariaDB

Crystal Reports 8.5/9.23 DE

xMate v1.15
Posts: 811
Joined: Tue May 06, 2008 04:28 AM
Re: Bitmap to String
Posted: Sat Mar 27, 2010 07:42 AM

Dear RAO,

I tried to attached '0x' like '0x'+BmpToStr( oImg:hBitmap )

but with still not saving..

Regards,

FAP

Kind Regards,

Frances



Fivewin for xHarbour v18.07

xHarbour v1.2.3.x

BCC 7.3 + PellesC8 ( Resource Compiler only)

ADS 10.1 / MariaDB

Crystal Reports 8.5/9.23 DE

xMate v1.15
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Bitmap to String
Posted: Sat Mar 27, 2010 09:48 AM

Oh.
We shall find a way to do through SQL.

Till then you are aware that adsFile2Blob(..) works and even much simpler way of
ALIAS->IMAGEFIELD := MemoRead( <ImageFile> ) works well.
I prefer the last simpler method.

There is a sample at

viewtopic.php?f=6t=18396

Meanwhile let me also test the SQL way.

&

Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Bitmap to String
Posted: Sat Mar 27, 2010 10:59 PM
It appears it is not possible to do this in Streamline SQL ( Advantage SQL ) directly in the manner you wanted. Here is a posting from Advantage Forums on the same issue:
Hi Reinaldo,

You need to use parameters for binary data.

Best Regards,

Lance Schmidt
Advantage R&D

"Reinaldo Crespo-Bazán" <noname@microsoft.com> wrote in message
news:4a8b25d7@solutions.advantagedatabase.com...
> Hi, again;
>
> I have the contents of a JPG file on a memory variable and would like to
> store it on a binary field. Can someone help with the SQL syntax?
>
> Suppose the memory variable is cBuffer, then I'm looking for something
> like:
>
> INSERT INTO MyTable(document) VALUES ( CAST(" + cBuffer + " AS
> SQL_BINARY )"
>
> Or better yet;
>
> INSERT INTO myTable(Document)
> SELECT * FROM
> OPENROWSET(BULK N'C:\Image1.jpg', SINGLE_BLOB)
>
> Or
>
> INSERT INTO Mytable(document) VALUES(x'C9CBBBCCCEB9C8CABCCCCEB9C9CBBB')
>
> Thank you,
>
>
> Reinaldo.
>

Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Bitmap to String
Posted: Sat Mar 27, 2010 11:00 PM
nageswaragunupudi wrote:It appears it is not possible to do this in Streamline SQL ( name of ADS SQL ) directly in the manner you wanted. Here is a posting from Advantage Forums on the same issue:
Hi Reinaldo,

You need to use parameters for binary data.

Best Regards,

Lance Schmidt
Advantage R&D

"Reinaldo Crespo-Bazán" <noname@microsoft.com> wrote in message
news:4a8b25d7@solutions.advantagedatabase.com...
> Hi, again;
>
> I have the contents of a JPG file on a memory variable and would like to
> store it on a binary field. Can someone help with the SQL syntax?
>
> Suppose the memory variable is cBuffer, then I'm looking for something
> like:
>
> INSERT INTO MyTable(document) VALUES ( CAST(" + cBuffer + " AS
> SQL_BINARY )"
>
> Or better yet;
>
> INSERT INTO myTable(Document)
> SELECT * FROM
> OPENROWSET(BULK N'C:\Image1.jpg', SINGLE_BLOB)
>
> Or
>
> INSERT INTO Mytable(document) VALUES(x'C9CBBBCCCEB9C8CABCCCCEB9C9CBBB')
>
> Thank you,
>
>
> Reinaldo.
>

Regards



G. N. Rao.

Hyderabad, India
Posts: 811
Joined: Tue May 06, 2008 04:28 AM
Re: Bitmap to String
Posted: Mon Mar 29, 2010 01:59 AM
Dear RAO,

Thank you so much for all your inputs and highlights.

it seems not possible at this time..

I used:
Code (fw): Select all Collapse
..
ALIAS->IMAGEFIELD := uBinary  //works ok..  but need to create index of table.. totally RDD style.  it works ok..
..



Im still looking forward for the SQL way of handling binary/image objects..


Thank to so much..
Best regards,
FAP
Kind Regards,

Frances



Fivewin for xHarbour v18.07

xHarbour v1.2.3.x

BCC 7.3 + PellesC8 ( Resource Compiler only)

ADS 10.1 / MariaDB

Crystal Reports 8.5/9.23 DE

xMate v1.15
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Bitmap to String
Posted: Mon Mar 29, 2010 02:08 AM

Yes, I use this syntax "ALIAS->IMAGEFIELD := uBinary "
This is always very simple.

Regards



G. N. Rao.

Hyderabad, India
Posts: 1054
Joined: Sun Oct 09, 2005 10:41 PM
Re: Bitmap to String
Posted: Mon Mar 29, 2010 03:04 PM
Hi.
If you want save file BMP into SQL table, you can use these functions:
Regards

Code (fw): Select all Collapse
cFileBmp := MEMOREAD("image.bmp")
txtBmp := Text2Hex(cFileBmp)

oData:Image := txtBmp       // save to blob field
...
...

txtBmp := oData:Image    // read bmp file

cFileBmp := Hex2Txt(txtBmp)

MEMOWRITE("image.bmp", cFileBmp)   

oBmp := LoadBmp("image.bmp")
....
..
.





//-------------------------------------------------------------------------------------------
Function Text2Hex(cFile)  // Str2Hex(cFile)
local nLen, nCont, cChr, cBin
cBin := ""
nLen := LEN(cFile)
FOR nCont := 1 TO nLen
   cChr := SUBSTR(cFile, nCont, 1)
   cBin := cBin + STRTOHEX(cChr)
NEXT nCont

Return(cBin)
//-------------------------------------------------------------------------------------------
Function Hex2Text(cFile) // Hex2Str(cFile)
local nLen, nCont, cChr, cBin
cBin := ""
nLen := LEN(cFile)
FOR nCont := 1 TO nLen STEP 2
   cChr := SUBSTR(cFile, nCont, 2)
   cBin := cBin + HEXTOSTR(cChr)
NEXT nCont

Return(cBin)

//====================================================================================================
#pragma BEGINDUMP
#include "hbapi.h"
#include "hbapiitm.h"    //#include "hbfast.h"
#include "hbstack.h"
#include "hbdefs.h"
#include "hbvm.h"
#include "hbapierr.h"

HB_ULONG HB_EXPORT hb_hextonum(char *cHex)
{
   HB_ULONG   ulNum = 0;
   char       c;
   int        iDigit;

   while ( *cHex && (*cHex == ' ') ) cHex++;

   while ( *cHex )
   {
      ulNum <<= 4;

      c = *cHex;
      if ( c >= '0' && c <= '9' )
      {
         iDigit = c - '0';
      }
      else if ( c >= 'A' && c <= 'F' )
      {
         iDigit = c - 'A' + 10;
      }
      else if ( c >= 'a' && c <= 'f' )
      {
         iDigit = c - 'a' + 10;
      }
      else
      {
         ulNum = 0;
         break;
      }
      ulNum += iDigit;
      cHex++;
   }

   return ulNum;
}

HB_FUNC( NUMTOHEX )
{
   int       iDigit;
   char      ret[ 33 ];
   int       iLen, iDefaultLen;
   HB_ULONG  ulNum;

   if( ISNUM( 2 ) )
   {
      iLen = hb_parni( 2 );
      iLen = ( iLen < 1 ) ? 1 : ( ( iLen > 32 ) ? 32 : iLen );
      iDefaultLen = 0;
   }
   else
   {
      iLen = 32;
      iDefaultLen = 1;
   }

   if( ISNUM( 1 ) )
   {
      ulNum = hb_parnint( 1 );
   }
   else if ( ISPOINTER( 1 ) )
   {
      ulNum = (HB_PTRDIFF) hb_parptr( 1 );
   }
   else
   {
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "NUMTOHEX", 2, hb_paramError( 1 ), hb_paramError( 2 ) );
      return;
   }

   ret[ iLen ] = '\0';
   do {
      iDigit = (int) ( ulNum & 0x0F );
      ret[ --iLen ] = iDigit + ( iDigit < 10 ? '0' : 'A' - 10 );
      ulNum >>= 4;
   } while ( iDefaultLen ? ulNum > 0 : iLen > 0 );

   hb_retc( &ret[ iLen ] );
}


HB_FUNC( HEXTONUM )
{
   if( ! ISCHAR(1) )
   {
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "HEXTONUM", 1, hb_paramError( 1 ) );
      return;
   }

   hb_retnint( hb_hextonum( hb_parc( 1 ) ) );
}


HB_FUNC( STRTOHEX )
{
   char   *cOutBuf;
   char   *cStr;
   char   *c;
   char   *cSep = "";
   unsigned char  ucChar;
   ULONG  ul, ulLen, ulLenSep = 0;
   int    iDigit;

   if( ! ISCHAR(1) )
   {
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "STRTOHEX", 2, hb_paramError( 1 ), hb_paramError( 2 ) );
      return;
   }

   if ( ISCHAR( 2 ) )
   {
     cSep = hb_parc( 2 );
     ulLenSep = hb_parclen( 2 );
   }

   cStr = hb_parc( 1 );
   ulLen = hb_parclen( 1 );
   c = cOutBuf = (char*) hb_xgrab( ulLen * 2 + ( ulLen - 1 ) * ulLenSep + 1 );

   for( ul = 0; ul < ulLen; ul++ )
   {
      if ( ulLenSep && ul )
      {
         memcpy( c, cSep, ulLenSep );
         c += ulLenSep;
      }

      ucChar = (unsigned char) cStr[ ul ];

      iDigit = (int) ( ucChar & 0x0F );
      c[ 1 ] = iDigit + ( iDigit < 10 ? '0' : 'A' - 10 );

      ucChar >>= 4;

      iDigit = (int) ucChar;
      c[ 0 ] = iDigit + ( iDigit < 10 ? '0' : 'A' - 10 );

      c += 2;
   }
   hb_retclen( cOutBuf, c - cOutBuf );
   hb_xfree( cOutBuf );
}


HB_FUNC( HEXTOSTR )
{
   char *cOutBuf, *cStr;
   char c;
   int  iByte, iFirst;
   ULONG  ul, ulLen, ulPos, ulAlloc;

   if( ! ISCHAR(1) )
   {
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "HEXTOSTR", 1, hb_paramError( 1 ) );
      return;
   }

   cStr = (char *) hb_parc( 1 );
   ulLen = hb_parclen( 1 );
   ulAlloc = (int) ( ulLen / 2 );
   cOutBuf = (char *) hb_xgrab( ulAlloc + 1 );

   ulPos = 0;
   iByte = 0;
   iFirst = 1;

   for ( ul = 0; ul < ulLen; ul++ )
   {
      iByte <<= 4;

      c = *cStr++;
      if ( c >= '0' && c <= '9' )
      {
         iByte += c - '0';
      }
      else if ( c >= 'A' && c <= 'F' )
      {
         iByte += c - 'A' + 10;
      }
      else if ( c >= 'a' && c <= 'f' )
      {
         iByte += c - 'a' + 10;
      }
      else
      {
        continue;
      }

      iFirst ^= 1;
      if ( iFirst )
      {
        cOutBuf[ ulPos++ ] = (char) iByte;
        iByte = 0;
      }
   }
   hb_retclen( cOutBuf, ulPos );
   hb_xfree( cOutBuf );
}

#pragma ENDDUMP

Continue the discussion