6 Replies - 2021 Views - Last Post: 07 November 2006 - 07:34 PM

#1 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

javascript to c++ array problems

Posted 06 November 2006 - 10:37 PM

Yo,

I am trying to convert a hash function from javascript to c++. I like how js converts all the variable types for you. :crazy:

I am not very knowledgable about pointers... actually i know very little about them... but i tried this code and it gave me a compile error because i was trying to use | with int* and int types.

Im not sure if there is a way to get around this... maybe global variables? :(

int* ccc(string hash)
{
	int *varAAA[1000];
	int varBBB=(1<<chrsz)-1;
	for (int i=0;i<hash.length()*chrsz;i+=chrsz)
	{
		char v1=charAt(hash,i/chrsz);
		int v2=(int)v1;
		[u]varAAA[i>>5] |= (v1 & varBBB) << (24-i%32);[/u]
	}
	return varAAA;
}


Error at underlined line. This is part of a larger function.

string mhash(string hash)
{
	return aaa(bbb(ccc(hash),(hash.length())*chrsz));
}


Basically wondering what the best way to pass these arrays around would be.

Any help appreciated. :)

Thanks,
okyup

Is This A Good Question/Topic? 0
  • +

Replies To: javascript to c++ array problems

#2 NyeNye  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 248
  • Joined: 24-September 06

Re: javascript to c++ array problems

Posted 06 November 2006 - 10:40 PM

char mhash(string hash)
{
.......



char rather than string...

int is for integer and char for strings..
Was This Post Helpful? 0
  • +
  • -

#3 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: javascript to c++ array problems

Posted 06 November 2006 - 11:21 PM

Thanks for the quick response.

Could you give a quick example of passing a character array instead of a string?

Its something i do not really grasp yet (usually use strings). Here is my try.... It works until i call the function, though its probably not the most efficient code you have ever seen. :P

char* stringManip(char manip[]);

int main(int argc, char *argv[])
{
	cout << "Enter string..." << endl;
	string temp;
	getline(cin,temp);
	char *input[100];
	for (int i=0;i<=temp.length()-1;i++)
		*input[i]=temp[i];
	input = stringManip(input);
	system("PAUSE");
	return EXIT_SUCCESS;
}

char* stringManip(char manip[])
{
	  return manip;
}


If you could clarify how to pass this variable properly i will give the original code another try for tomorrow. :)
Was This Post Helpful? 0
  • +
  • -

#4 NyeNye  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 248
  • Joined: 24-September 06

Re: javascript to c++ array problems

Posted 06 November 2006 - 11:42 PM

ex
array[]= helo;<---array[] contain this set of characters

each character had index number:

array[index 0]=h
array[index 1]=e
array[index 2]=l
array[index 3]=o

passing of character is just what you had done already..

you execute a loop a for loop....that's the right thing...
Was This Post Helpful? 0
  • +
  • -

#5 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: javascript to c++ array problems

Posted 07 November 2006 - 10:06 AM

Yea i need to pass an array of characters (string) and an array of intergers through a function.

I know in java you can put int[] or char[] as the return type of the function, but it gives me compiler error in C++... wondering alternative.

So for example, could you take the array from your last post and send it to a function, receive it, and return it?
Was This Post Helpful? 0
  • +
  • -

#6 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: javascript to c++ array problems

Posted 07 November 2006 - 07:28 PM

I just want to say that thanks to this page i am solving my errors with this very fast. :) (i hope so atleast... that or compiler is missing them ;))

http://www.cplusplus...l/pointers.html
Was This Post Helpful? 0
  • +
  • -

#7 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: javascript to c++ array problems

Posted 07 November 2006 - 07:34 PM

Okay... was all going well for awhile.

I knew it was too good to be true. :(

Error message: cannot convert `int*' to `int**' for argument `1' to `char* aaa(int**)'

Sounds horrible... Any ideas to fix?

void magic(char* hash,int l)
{
	cout << aaa(bbb(ccc(hash),(l*chrsz)));
}

int* ccc(char* hash)
{
	int varAAA[1000];
	int * p;
	int varBBB=(1<<chrsz)-1;
	for (int i=0;i<strlen(hash)*chrsz;i+=chrsz)
	{
		char v1=charAt(hash,i/chrsz);
		int v2=(int)v1;
		int v3=(i>>5);
		varAAA[v3] |= (v1 & varBBB) << (24-i%32);
	}
	return p;
}
	
int* bbb(int* x,int len)
{
//	x[len >> 5] |= 0x80 << (24-len%32);
//	x[((len + 64 >> 9) << 4) + 15] = len;
//	int w[80];
//	int a = 1732584193;
//	int b = -271733879;
//	int c = -1732584194;
//	int d = 271733878;
//	int e = -1009589776;
//	for (int i=0;i<x.length();i+=16)
//	{
//		int olda=a,oldb=b,oldc=c,oldd=d,olde=e;
//		for (int j=0;j<80;j++)
//		{
//			if(j<16)
//				w[j]=x[i+j];
//			else
//				w[j]=eee(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16],1);
//			int t=ddd(ddd(eee(a,5),fff(j,b,c,d)),ddd(ddd(e,w[j]),ggg(j)));
//			e=d;
//			d=c;
//			c=eee(b,30);
//			b=a;
//			a=t;
//		}
//		a=ddd(a,olda);
//		b=ddd(b,oldb);
//		c=ddd(c,oldc);
//		d=ddd(d,oldd);
//		e=ddd(e,olde);
//	}
//	int *ret[5]={a,b,c,d,e};
//	return ret;
	  int ret[5]={1,2,3,4,5};
	  int * p;
	  return p;
}
	
char* aaa(int* varDDD[])
{
//	string varCCC="0123456789abcdef";
//	string str="";
//	for (int i=0;i<varDDD.length()*4;i++)
//	{
//		str+=varCCC.charAt((varDDD[i>>2] >> ((3-i%4)*8+4)) & 0xF)+varCCC.charAt((varDDD[i>>2] >> ((3-i%4)*8)) & 0xF);
//	}
//	return str;
	  char ret[8]="string!";
	  char * p;
	  return p;
}


Thanks. :)

Note that i know the commented out lines are all messed and plan on fixing them after i get this to go.

This post has been edited by okyup: 07 November 2006 - 07:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1