KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: The 4D Open APIs - Part X: Converting 4D data (sample code)
PRODUCT: 4D Open | VERSION: 6.7 | PLATFORM: Mac & Win
Published On: September 21, 2001
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

4D Open : Converting 4D data

Handling data with the 4D Open libraries may require a data conversion.

Converting a string into a 4D Integer:
data.typ = s_Int16;
StringToNum(kAlphaData, &tempNum );
data.u.i = LoWord(tempNum);
Check(_4D_AddToBuffer(hConnect, hBuffer, 4, &data),"_4D_AddToBuffer\0");

Converting a string into a 4D Real:
data.typ = s_Number;
MonReal = atof(kAlphaData1);
data.u.r.r = MonReal;
Check(_4D_AddToBuffer(hConnect, hBuffer, 3, &data),"_4D_AddToBuffer\0");

Converting a string into a 4D Longint:
data.typ = s_Int32;
StringToNum(kAlphaData1, &tempNum );
data.u.l = tempNum;
Check(_4D_AddToBuffer(hConnect, hBuffer, 5, &data),"_4D_AddToBuffer\0");

This is a code example that will convert these data:
#if __INTEL__ /* <-- defined by CodeWarrior (x86 project) */
/* #pragma once */
/* #include "ansi_prefix.win32.h" */
/* #include */
/* Use pre-compiled header instead */
#elif WIN32 /* <-- defined by Visual C++ */
#include
#endif
#define Openlib 1
#include "Open4D.h"
#include
#include
#include
/*#ifdef __NWERKS__
#ifdef __INTEL__
#define WINVER 0x0400
#define MACVER 0x0400
#endif
#else
#define WINVER 0x0400
#define __INTEL__
#endif*/
const short KNC = NC_TCPIP;
#if def __INTEL__
const unsigned char KStationName[32]="Intel machine\0";
else
const unsigned char KStationName[32]="Motorola machine\0";
#endif
const unsigned char KUserName[32]="APIUser\0";
const unsigned char KUserPass[32]="APIPass\0";
const unsigned char KUserTask[32]="APITask\0";
const unsigned char kAlphaData[256] = "012345\0";
NetworkComponentHandle hNetworkComponent;
NetworkLocationPtr pNetworkLocation;
ConnectHandle hConnect;
IDRec logRec;
unsigned char szName[256];
int choix;
void initialisation(void);
int2 StartWSConnection(void);
void Check(int2 myErr, char *description);
void CloseWSConnection(void);
void AddRecords(void);
int2 StartWSConnection()
{
int2 myErr;
myErr =_4D_Select4DServer(szName, &pNetworkLocation, KNC, hNetworkComponent);
if(!myErr)
{
_4D_Open4DConnection(&logRec, pNetworkLocation, &hConnect,KNC, hNetworkComponent);
}
return(myErr);
}
void CloseWSConnection()
{
_4D_Close4DConnection(hConnect);
}
void initialisation(void)
{
memcpy(&logRec.StationName[1],KStationName, strlen((char *)KStationName));
logRec.StationName[0]=strlen((char *)KStationName);
memcpy(&logRec.UserName[1],KUserName, strlen((char *)KUserName));
logRec.UserName[0]=strlen((char *)KUserName);
memcpy(&logRec.UserPass[1],KUserPass, strlen((char *)KUserPass));
logRec.UserPass[0]=strlen((char *)KUserPass);
memcpy(&logRec.TaskName[1],KUserTask, strlen((char *)KUserTask));
logRec.TaskName[0]=strlen((char *)KUserTask);
}
void Check(int2 myErr, char *description)
{
char err[256];
if(myErr)
{
sprintf(err, "%s: returns %d\n", description , myErr);
printf(err);
getchar();
}
}
void AddRecords(void)
{
BufferHandle hBuffer;
Handle4D hText, hPict, hBlob;
char *pText, *pPict, *pBlob;
DataRec data;
long tempNum;
float MonReal;
short fNb = 1, nbRec=1;
hBuffer = _4D_CreateBuffer(hConnect, fNb);
data.typ = s_Number;
MonReal = atof(kAlphaData1);
data.u.r.r = MonReal;
Check(_4D_AddToBuffer(hConnect, hBuffer, 3, &data),"_4D_AddToBuffer\0");
data.typ = s_Int16;
StringToNum(kAlphaData1, &tempNum );
data.u.i = LoWord(tempNum);
Check(_4D_AddToBuffer(hConnect, hBuffer, 4, &data),"_4D_AddToBuffer\0");
data.typ = s_Int32;
StringToNum(kAlphaData1, &tempNum );
data.u.l = tempNum;
Check(_4D_AddToBuffer(hConnect, hBuffer, 5, &data),"_4D_AddToBuffer\0");
Check(_4D_NewRecord(hConnect, hBuffer),"_4D_NewRecord\0");
Free4DHandle(hText);
Free4DHandle(hPict);
Free4DHandle(hBlob);
Free4DHandle((Handle4D) hBuffer);
}
void main(void)
{
initialisation();
_4D_Init4DWS();
printf("THE VERSION NUMBER IS: %ld\n", (_4D_GetVersionNumber()>>16));
printf("THE 4D NETWORK COMPONENT ID IS %ld\n",_4D_NB_NetworkComponent());
printf("\n");
hNetworkComponent= _4D_InitNetworkComponent(KNC);
if(hNetworkComponent)
{
if(!StartWSConnection())
{
AddRecords();
}
_4D_DeInitNetworkComponent(KNC,hNetworkComponent);
}
_4D_DeInit4DWS();
}