Witchie NL Posted September 11, 2007 Report Posted September 11, 2007 Does anyone know how to copy a String into a char array? For instance:If i have an integer type variable that holds the number 1234567.And a char temp[10] array.How do i get the integer into the char array so i have each number in a seperate array position like: temp[0]=1temp[1]=2temp[3]=3temp[4]=4temp[5]=5temp[6]=6temp[7]=7 At the moment i have this:String temp = t->Amount; // t->Amount is an integer char blah[10]; strncpy(blah, temp, 10);But this copies the value of the string variable into each array position. So with a string holding 100 that would make:blah[0]=100blah[1]=100blah[2]=100
Russky Posted September 11, 2007 Report Posted September 11, 2007 Well when I did coding last year I remember writing an out put Russky is the same as R+u+s+s+k+y Hope that helps.
Drake7707 Posted September 11, 2007 Report Posted September 11, 2007 i dunno if the string is ascii or unicode, but if it is ascii you could use CopyMemory api and p!@#$%^&* the pointer of the string and the pointer of the array
»Admiral Kirk Posted September 11, 2007 Report Posted September 11, 2007 Well, first of all, youll need to specify what language your writing in.... That would be most helpfull. If you are using C++ and the String you speak of is an STL string (which it might not be because STL string is lowercase s) then you would do something like: char temp[20];string test;test = "stuff";strncpy(temp, stuff.c_str(), stuff.length()); If were talking some other language, then who knows. Or again, if were not speaking of the STL string class, then once again, who knows Specify ^^ P.S.: The Above is begging for a buffer overflow fyi, youll want to check that stuff does not exceed the known length of your char buffer ^^.
Smong Posted September 11, 2007 Report Posted September 11, 2007 You're also asking two different things:- Copy String to char array.- Copy each digit of an integer into a char array as integers. Just to make this a bit clearer, these are different:buf[0] = 0;buf[0] = '0';
Witchie NL Posted September 11, 2007 Author Report Posted September 11, 2007 Well, first of all, youll need to specify what language your writing in.... That would be most helpfull. If you are using C++ and the String you speak of is an STL string (which it might not be because STL string is lowercase s) then you would do something like: char temp[20];string test;test = "stuff";strncpy(temp, stuff.c_str(), stuff.length()); If were talking some other language, then who knows. Or again, if were not speaking of the STL string class, then once again, who knows Specify ^^ P.S.: The Above is begging for a buffer overflow fyi, youll want to check that stuff does not exceed the known length of your char buffer ^^. Im doing this in C++ for mervbot. Mervbot has its own string class included and c_str() isnt included.Adding at the top isnt working aswell.
Bak Posted September 12, 2007 Report Posted September 12, 2007 My turn!char buf[10]; int num = 12456; _snprintf(buf, sizeof(buf) - 1, "%i", num); buf[sizeof(buf) - 1] = 0; also, smong is right, if you don't want the ascii values you could do: char buf[10]; int num = 12456; _snprintf(buf, sizeof(buf) - 1, "%i", num); buf[sizeof(buf) - 1] = 0; for (int c = 0; buf[c]; ++c) buf[c] -= '0';
Samapico Posted September 12, 2007 Report Posted September 12, 2007 char temp[20];string test;test = "stuff";strncpy(temp, (char*)stuff, stuff.len); I think this should work with merv
Smong Posted September 12, 2007 Report Posted September 12, 2007 Bak I don't think you need to do the -1 thing on snprintf, it always makes sure it's null-terminated for you. It's just strncpy that needs the null adding manually.
Witchie NL Posted September 12, 2007 Author Report Posted September 12, 2007 (edited) void botInfo::UpdateLvz(Player *p, int Credsadded) { Account *t = FindEntree(p->name); if (!t) { t = new Account(); t->Owner = p->name; t->Credit = InitCred; Credslist.append(t); tell(makeEcho("New Entree created: "+(String)p->name+".")); } string temp; temp = t->Credit - Credsadded; char cred1[10]; strncpy(cred1, temp.c_str(), temp.length()); string temp2; temp2 = t->Credit; char cred2[10]; strncpy(cred2, temp2.c_str(), temp2.length()); for (int i=0; i<10; i++) { int t = i+20; sendPrivate(p, ".*objoff "+(String)t+(String)cred1[i]); sendPrivate(p, ".*objon " +(String)t+(String)cred2[i]); } } To make it a bit easyer: this is what i need to do, What should i change to what?t=a number between 19 and 30 where the first of those is just a number the second number is a pos on the lvz board and the third number cred# is the number of the lvz.So 213 would make 2-position 1-number 3. I also tried to use temp.at(i); but somehow i couldnt use that in sendPrivate(), gives an error and then closes mervbot. Edited September 12, 2007 by Witchie NL
Witchie NL Posted September 12, 2007 Author Report Posted September 12, 2007 (edited) Ok bak's code worked and the buf[sizeof(buf) - 1] = 0; wasnt needed indeed. Now the following:I have this,int temp = t->Credit - Credsadded; char cred1[10]; _snprintf(cred1, sizeof(cred1) - 1, "%i", temp); for (int c = 0; cred1[c]; ++c) cred1[c] -= '0'; int temp2 = t->Credit; char cred2[10]; _snprintf(cred2, sizeof(cred2) - 1, "%i", temp2); for (int d = 0; cred2[d]; ++d) cred2[d] -= '0'; for (int i=0; i<10; i++) { int t = i+20; sendPrivate(p, ".*objoff "+(String)t+(String)cred1[i]); sendPrivate(p, ".*objon " +(String)t+(String)cred2[i]); } Now it works propperly but if i have a number under the 10 digits it gives a little problem. int a = 12345;char b[10]; youll have...: b0=1;b1=2;b2=3;b3=4;b4=5;b6=0;b7=;b8=;b9=; ...after the _snprintf(); Main question: To make it a bit easyer: this is what i need to do, What should i change to what?Or if posible: Is there a way to fill up the with 0's? Edited September 12, 2007 by Witchie NL
Samapico Posted September 13, 2007 Report Posted September 13, 2007 errr, this is Merv right? There are built-in functions to toggle lvz objects that are much more efficient object_target(p); //sets target of the objects to toggle, set to NULL if you want the object to be public queue_enable(objectID); //do that for every object you want to queue; queue_disable to disable them toggle_objects(); //toggles all objects queued Now... you want to display each digit of your number; you don't need a string for that Simple version:int creds = t->Credit - CredsAdded; //or whatever you want to display for (int i = 0; creds > 0; i++) { int currentdigit = creds % 10; //hold the right-most digit queue_enable(i*10 + currentdigit); //depending on how your lvz is made, this is where your ID is calculated, for a digit 'i' creds /= 10; //get rid of the right-most digit by dividing by 10 }Now, if you want to display leading zeros, and hide old objects, you could have:int creds = t->Credit - CredsAdded; //or whatever you want to display int oldcreds = ...; for (int i = 0; i < 10; i++) { int newdigit = creds % 10; //hold the right-most digit int olddigit = oldcreds % 10; queue_enable(i*10 + newdigit); //depending on how your lvz is made, this is where your ID is calculated, for a digit 'i' queue_disable(i*10 + olddigit); creds /= 10; //get rid of the right-most digit by dividing by 10 oldcreds /= 10; } Hope that helps
Bak Posted September 16, 2007 Report Posted September 16, 2007 Bak I don't think you need to do the -1 thing on snprintf, it always makes sure it's null-terminated for you. The man page says snprintf is always supposed to be null terminiated... but the windows version doesn't live up the the specification. You're right about the -1 though, so it should be: _snprintf(buf, sizeof(buf), "%i", num); buf[sizeof(buf)] = 0; whereas accoding to the specification you should just be able to do: _snprintf(buf, sizeof(buf), "%i", num); to see the error run this program: #include <stdio.h> int main() { const char* !@#$%^&*o = "!@#$%^&*o"; char buf[5]; _snprintf(buf, sizeof(!@#$%^&*o), "%s",!@#$%^&*o); //buf[ sizeof(!@#$%^&*o)] = 0; printf("%i: '%s'\n",sizeof(!@#$%^&*o),buf); return 0; } ----------------------------------------------------------------------------------------------------------------------------- try this witchie (but sampacio is right about the more efficient way): int temp = t->Credit - Credsadded; char cred1[10]; _snprintf(cred1, sizeof(cred1) - 1, "%i", temp); for (int c = 0; cred1[c]; ++c) cred1[c] -= '0'; int temp2 = t->Credit; char cred2[10]; _snprintf(cred2, sizeof(cred2) - 1, "%i", temp2); int len = (int)strlen(cred2); for (int d = 0; cred2[d]; ++d) cred2[d] -= '0'; for (int i=0; i<len; i++) { int t = i+20; sendPrivate(p, ".*objoff "+(String)t+(String)cred1[i]); sendPrivate(p, ".*objon " +(String)t+(String)cred2[i]); }
Smong Posted September 18, 2007 Report Posted September 18, 2007 I get your point bak. I don't want to beat a dead horse but without -1 you will overrun the buffer and it only happens to work in the test program because you are looking at the size of a char pointer (4) not of the buffer (5).
Bak Posted September 18, 2007 Report Posted September 18, 2007 crap, that's supposed to be sizeof(buf)... heh, well I guess bugs can happen in the simplest of programs.
Recommended Posts