Converting String to Integer
Converting String to Integer
Hello,
how can i convert integer to (for example) char dd[128] ? I tried to create function like IntToStr (from Pascal), but it didn't work...
how can i convert integer to (for example) char dd[128] ? I tried to create function like IntToStr (from Pascal), but it didn't work...
Last edited by Cmaranec on Sat Jun 16, 2007 3:11 am, edited 1 time in total.
Sorry for my bad English...
1. Divide the value by the base of the numbering system (for decimal, the base is 10)
2. Convert the remainder value of the division to ASCII (for decimal, this is usually done by adding 0x30)
3. Check if the quotient of the division is 0, if it is _not_ then go back to step 1.
4. When the loop ends, reverse the string buffer (for example '54321' becomes '12345')
2. Convert the remainder value of the division to ASCII (for decimal, this is usually done by adding 0x30)
3. Check if the quotient of the division is 0, if it is _not_ then go back to step 1.
4. When the loop ends, reverse the string buffer (for example '54321' becomes '12345')
works
Yes, it works fine, thanks.
Question 2: How can i convert string to integer? (with error code) i have char firstc[64]; and char secndc[64]; and int first; and int secnd; and i want to "copy" all from 'firstc' to 'first' and from 'secndc' to 'secnd'.
Question 2: How can i convert string to integer? (with error code) i have char firstc[64]; and char secndc[64]; and int first; and int secnd; and i want to "copy" all from 'firstc' to 'first' and from 'secndc' to 'secnd'.
Sorry for my bad English...
Re: works
Something like this perhaps?Cmaranec wrote:Yes, it works fine, thanks.
Question 2: How can i convert string to integer? (with error code) i have char firstc[64]; and char secndc[64]; and int first; and int secnd; and i want to "copy" all from 'firstc' to 'first' and from 'secndc' to 'secnd'.
Code: Select all
char *convertStringToInt(int *number, char *string) {
if(strncasecmp(string, "0x", 2) == 0) {
// Hexadecimal
return convertStringToIntWithBase(number, &string[2], 16);
}
if(*string == '0') {
// Octal???
return convertStringToIntWithBase(number, &string[1], 8);
}
// Decimal
return convertStringToIntWithBase(number, string, 10);
}
char *convertStringToIntWithBase(int *number, char *string, int base) {
int digit;
*number = 0;
for(;;) {
if( (*string >= '0') && (*string <= '9') ) {
digit = *string - '0';
} else if( (*string >= 'A') && (*string <= 'Z') ) {
digit = *string - 'A' + 10;
} else if( (*string >= 'a') && (*string <= 'z') ) {
digit = *string - 'a' + 10;
} else {
break;
}
if(digit >= base) {
break;
}
if(*number >= INT_MAX / base) {
return NULL; // Will overflow...
}
*number = *number * base;
if(*number >= INT_MAX - digit) {
return NULL; // Will overflow...
}
*number = *number + digit;
string++;
}
return string;
}
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Re: works
Great job, congrats!Cmaranec wrote:Yes, it works fine, thanks.
Here is an example:
Code: Select all
int atodw(const char *str) {
int value = 0;
for(int counter = strlen(str)-1, multiplier = 1; !(counter < 0); --counter, multiplier *= 10) {
value += (str[counter] - 0x30) * multiplier;
}
return value;
}
Here is a function that I have coded for my kernel that converts an unsigned string to a 32-bit Unsigned DWORD value. This one is optimized and it wouldn't use the MUL instruction at all for multiplication:
Code: Select all
; --------------------------------------------------
__StrToDWORD:
; DWORD __StrToDWORD (const char* InStr)
PUSH EBX
PUSH EDX
PUSH EBP
MOV EBP , ESP
MOV EBX , DWORD PTR [EBP + 0x10]
XOR EAX , EAX
.Loop:
MOV EDX , DWORD PTR [EBX]
AND EDX , 0x000000FF
JE .EP
AND EDX , 0x0000000F
ADD EAX , EAX
LEA EAX , [EAX + 0x04*EAX]
ADD EAX , EDX
INC EBX
JMP .Loop
.EP:
POP EBP
POP EDX
POP EBX
RET 0x04
; --------------------------------------------------
On the field with sword and shield amidst the din of dying of men's wails. War is waged and the battle will rage until only the righteous prevails.
I have written a similar function ASM some time ago. Cmaranec wanted a C function so why post the ASM one? Since Cmaranec also asked how to convert a string back to an integer, I don't think he understands much of ASM since he's just exploring calculators
Imho, you shouldn't overthrow someone with 'complicated' stuff that they will probably not (yet) understand.
Imho, you shouldn't overthrow someone with 'complicated' stuff that they will probably not (yet) understand.
Re: wow
That's good to hearCmaranec wrote:Wow, thanks ! All the functions works well