Sunday, August 16, 2009

String processing functions in C library

[1] http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/

/* String functions in C library debugged in VC6 IDE

strncmp, strtol, strtok,strncpy,sprintf,atoi,

*/

#include "stdafx.h"
#include
#include //strncmp,strtok
#include //strtol

typedef unsigned char GSNTYPES_BYTE; /* 0 to 255 */
typedef char GSNTYPES_CHAR; /* -128 to 127 */


#define MAX_NUM_BYTES_HTML_TAG_VALUE (256) //limited by ConnectOne WiFi module.



int main()
{
//unsigned int len;

//len = strspn("this is a test","siht ");
//len = strspn("this is a test","se");
//printf("%d\n",len);


const char DASHSTRING[]="-";

const char CMD2TARGETREAD[]="Cmd2Target_Rd_\x31";
GSNTYPES_CHAR lcTagsValue[MAX_NUM_BYTES_HTML_TAG_VALUE];
GSNTYPES_BYTE lcTemp,lcWeb2TargetCmdCode;
char stringTemp[11];
char FakedMiMe_01[]="HTTP/1.1\xb7 200\xb7 OK\r\nServer:\xb7 CableTrackerIChip\r\nContent-Type:\xb7 application/octet-stream\r\n\r\n";
char word[]="\x6d=\x64=";



int i;


sprintf(lcTagsValue,"I/(-1)\r\n");
sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("decimal value of %s is %d \n", stringTemp,atoi(stringTemp));

sprintf(lcTagsValue,"I/(001)\r\n");
sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("decimal value of %s is %d \n", stringTemp,atoi(stringTemp));

sprintf(lcTagsValue,"I/(000)\r\n");
sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("decimal value of %s is %d \n", stringTemp,atoi(stringTemp));


printf (word);
printf(FakedMiMe_01);


// char string []="abc def ghi";
// char * word;


/* char num[15];

// Test a valid number
strcpy(num,"09012345");

printf("%s(Oct) is %i(Dec)\n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)\n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)\n", num, strtol(num, NULL, 16));

puts("----------------------------------");

*/

printf("string is %s \n", CMD2TARGETREAD);


for (i=0;i {

printf("[%i]=%2X \n",i, CMD2TARGETREAD[i]);
}


sprintf(lcTagsValue,"I/(u got me)\r\nW");

sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
printf("before bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("in bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("After CR/LR %s \n",stringTemp);
printf("After CR/LR 0x%02X \n",stringTemp[0]);

printf("len in bracket %u \n",strlen(stringTemp));


sprintf(lcTagsValue,"I/(u got me)\r\n");

sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
printf("before bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("in bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("After CR/LR %s \n",stringTemp);
printf("After CR/LR 0x%02X \n",stringTemp[0]);

printf("len in bracket %u \n",strlen(stringTemp));



//sprintf(lcTagsValue,"Cmd2Target_Rd_0A");
sprintf(lcTagsValue,"11-22-33-AA-BB-CF");

printf("strlen(CMD2TARGETREAD) %d\n", strlen(CMD2TARGETREAD));


sprintf(stringTemp,"%s",strtok(lcTagsValue, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 1 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 2 %02x\n",lcTemp);


sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 3 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 4 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 5 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 6 %02X\n",lcTemp);


/* if (strncmp(CMD2TARGETREAD,lcTagsValue,strlen(CMD2TARGETREAD))==0)
{
//Read command -- 'Cmd2Target_Rd_4F'
//Change ASCII ('0' ~ 'F' )to HEX and refill the gcU2_RxBuffer[]

strncpy(stringTemp,&lcTagsValue[strlen(CMD2TARGETREAD)],2);

lcWeb2TargetCmdCode=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);



}
*/

printf("%d\n",lcWeb2TargetCmdCode);



/*
word=strtok(string, " ");

printf("%s\n",word);

//word=strtok(NULL, " ");
word=strtok(string, " ");

printf("%s\n",word);

*/

}

Wednesday, August 12, 2009

String operation in C/C++/Java -- strtok strspn

/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/* a sample program that prints the index of the first
character in the string pointed to by str1 that does not
match any of the characters in str2.*/

#include "stdafx.h"
#include
#include

int main()
{
//unsigned int len;

//len = strspn("this is a test","siht ");
//len = strspn("this is a test","se");
//printf("%d\n",len);


char string []="abc def ghi";
char * word;

word=strtok(string, " ");

printf("%s\n",word);

//word=strtok(NULL, " ");
word=strtok(string, " ");

printf("%s\n",word);



}






/* STRSTR.C */

#include
#include

char str[] = "lazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] = " 1 2 3 4 5";
char fmt2[] = "12345678901234567890123456789012345678901234567890";

void main( void )
{
char *pdest;
int result;
printf( "String to be searched:\n\t%s\n", string );
printf( "\t%s\n\t%s\n\n", fmt1, fmt2 );
pdest = strstr( string, str );
result = pdest - string + 1;
if( pdest != NULL )
printf( "%s found at position %d\n\n", str, result );
else
printf( "%s not found\n", str );
}


Output

String to be searched:
The quick brown dog jumps over the lazy fox
1 2 3 4 5
12345678901234567890123456789012345678901234567890

lazy found at position 36


String Manipulation Routines

See Also strcspn, strcmp, strpbrk, strrchr, strspn

Tuesday, August 11, 2009

DecimalHexBinary data convertion

{HTML>
{HEAD>
{TITLE>Number Conversion Table{/TITLE>
{/HEAD>
{BODY>
{B>Using toString() to convert to other number bases:{/B>
{HR>
{TABLE BORDER=1>
{TR>
{TH>Decimal{/TH>{TH>Hexadecimal{/TH>{TH>Binary{/TH>{/TR>
{SCRIPT LANGUAGE="JavaScript">
var content = ""
for (var i = 0; i {= 20; i++) {
content += "{TR>"
content += "{TD>" + i.toString(10) + "{/TD>"
content += "{TD>" + i.toString(16) + "{/TD>"
//content += "{TD>" + i.toString(16).toUpperCase() + "{/TD>"
content += "{TD>" + i.toString(2) + "{/TD>{/TR>"
}
document.write(content)
{/SCRIPT>
{/TABLE>
{/BODY>
{/HTML>

Thursday, August 6, 2009

Multiple lines of string in JavaScript. Using 'back slash'

function createWarningPage()
{


content="";

content+="\
{CENTER>\
{a> {b>{SPAN CLASS='preppy'> Device Communication Time out after "+parent.left.timeoutduration+" Seconds . {/SPAN>{/b> {/a>\
{br>\
{a> Please try again. {/a>\
{BR>\
{BR>\
{BR>\
{BR>\
{/CENTER>";

document.write(content);

}

Check PC Info with 'dxdiag' in command line

Escape sequence in string

From: http://www.python.org/doc/2.5.2/ref/strings.html