|
CNewbie wrote:
just convert them to an int value and leave them be, not convert them to decimal
I don't understand what you're asking there. What's the expected behavior and how is it not working?
--Mike--
Personal stuff:: Ericahist | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
----
There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.
|
|
|
|
|
The above code takes 2 chars from the array and converts them to an int value.
However (and I proved this by running the code) it also converts the new int value from HEX to Decimal.
That helped me in one situation, but another situation where I dont want to convert that new int value from HEX to Dec, but need it to stay a HEX Value it doesnt help.
Hopefully someone understands what I am saying and can help.
Thanks
|
|
|
|
|
You dont convert from hex to decimal, they are just different ways of representing the same thing. In your case, you are outputting an 'int' variable to cout. Unless you tell cout otherwise, it will always display this as a decimal. If you want to display it as hex, then you need:
cout << hex << myNumber;
|
|
|
|
|
You can mofify the code as follows:
int Convert(char a, char b){
int intTemp=0;
char strBuffer[3];
strBuffer[0] = a;
strBuffer[1] = b;
strBuffer[2] = '\0';
sscanf(strBuffer,"%2x",&intTemp);
return intTemp;
}
This returns the HEX'ab' int value. The HEX or Decimal or Octal or whatever is just a representation of an integer number. What is exactly your problem?
|
|
|
|
|
itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow
Convert an integer to a string.
char *_itoa( int value, char *string, int radix );
char *_i64toa( __int64 value, char *string, int radix );
char * _ui64toa( unsigned _int64 value, char *string, int radix );
wchar_t * _itow( int value, wchar_t *string, int radix );
wchar_t * _i64tow( __int64 value, wchar_t *string, int radix );
wchar_t * _ui64tow( unsigned __int64 value, wchar_t *string, int radix );
Routine Required Header Compatibility
_itoa <stdlib.h> Win 95, Win NT
_i64toa <stdlib.h> Win 95, Win NT
_ui64toa <stdlib.h> Win 95, Win NT
_itow <stdlib.h> Win 95, Win NT
_i64tow <stdlib.h> Win 95, Win NT
_ui64tow <stdlib.h> Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
Each of these functions returns a pointer to string. There is no error return.
Parameters
value
Number to be converted
string
String result
radix
Base of value; must be in the range 2 – 36
Remarks
The _itoa, _i64toa, and _ui64toa function convert the digits of the given value argument to a null-terminated character string and stores the result (up to 33 bytes) in string. If radix equals 10 and value is negative, the first character of the stored string is the minus sign ( – ). _itow, _i64tow, and _ui64tow are wide-character versions of _itoa, _i64toa, and _ui64toa respectively.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_itot _itoa _itoa _itow
Example
/* ITOA.C: This program converts integers of various
* sizes to strings in various radixes.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;
_itoa( i, buffer, 10 );
printf( "String of integer %d (radix 10): %s\n", i, buffer );
_itoa( i, buffer, 16 );
printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
_itoa( i, buffer, 2 );
printf( "String of integer %d (radix 2): %s\n", i, buffer );
_ltoa( l, buffer, 16 );
printf( "String of long int %ld (radix 16): 0x%s\n", l,
buffer );
_ultoa( ul, buffer, 16 );
printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,
buffer );
}
Output
String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2
Data Conversion Routines
See Also _ltoa, _ultoa
d
|
|
|
|
|
visual C++
how to add a formatted CString value to the original value, e.g: how to achieve these logical statements?!
CString str;
str += str.Format(" qer23 %d asdgsgd", var);
|
|
|
|
|
str.Format(...) doesn't return a string, it reassigns to str in-place. You'll need to use a second variable for the Format() call and append that to str .
--Mike--
Personal stuff:: Ericahist | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
----
Pinky, are you pondering what I'm pondering?
I think so Brain, but how will we fit the hamster inside the accordion?
|
|
|
|
|
You must use another variable for this purpose.
CString str, strTemp;
strTemp.Format(" qer23 %d asdgsgd", var);
str += strTemp;
Gurmeet S. Kochar
If you believe in God, it's because of the Devil
|
|
|
|
|
or
str.Format("%s qer23 %d asdfgsf",str,var);
MSN Messenger.
prakashnadar@msn.com
"If history isn't good, just burn it." - Sidhuism.
|
|
|
|
|
This approach won't work.
To quote from the MSDN documentation for CString::Format()
The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:
CString str = "Some Data";
str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list.
will cause unpredictable results.
|
|
|
|
|
yeah sounds logical, coz its working on the same object, i assumed that a copy of the parameter would be made.
MSN Messenger.
prakashnadar@msn.com
"If history isn't good, just burn it." - Sidhuism.
|
|
|
|
|
If you are using VS.NET 2002 or later, the CString class includes the member function AppendFormat , which does what you are asking.
If you are using VC6, you will need to do something like this:
CString str,tmp;
str = ";kj;lak;sajlg";
tmp.Format(" qer23 %d asdgsgd", var);
str += tmp;
Software Zen: delete this;
|
|
|
|
|
Visual C++
a simple question!
what is the easiest way to add a simple vertical scroll bar to a dialog based project?
|
|
|
|
|
Override PreCreateWindow and add WM_VSCROLL as shown below:
cs.style |= WS_VSCROLL;
Hope this helps.
Gurmeet S. Kochar
If you believe in God, it's because of the Devil
|
|
|
|
|
Hello!
I'm newbie in C/ANSI programming.
I have another problem:
i have a char *c="word1 word2 word3";
how can I obtain the word in separate strings?
This is what I want to do:
char *c="word1 word2 word3";
char **words;
after proccessing the string c I want to have
words[0]="word1";
words[1]="word2";
words[2]="word3";
Thank you!
|
|
|
|
|
try looking up strtok(), sample usage in MSDN library.
INTP
|
|
|
|
|
Use strchr or strtok .
Gurmeet S. Kochar
If you believe in God, it's because of the Devil
|
|
|
|
|
Hello!
I have another problem.
I want to make 2 programs a client and a server.
This programs must access the same file and read/write into it.
EXAMPLE:
if the client write to the file:
#CLIENT 1 REQUEST
the server must read this and response:
#S CLIENT 1 REPLAY
Now this is the problem:
I want to do this in client:
while (1)
{
read from file;
if string readed containt #S CLIENT 1
{
do some action
Ex: write to file #RESPONSE ACCEPTED
}
}
and I want that client to do this forever.
And I want the same to do the server.
Now How do I open/read/write to file?
Or how to do this?
Only ANSI/C code.
Thank You!
Bye!
|
|
|
|
|
Hi
The program that write the file have to make a fflush to write the data to the file otherwise the data is only in memory...
after that the other program can read de info...
|
|
|
|
|
I have a text file with reserved c words, one in each line. for example:
auto
asm
break
case
char
const
..(continues)
and i want to load all of them in a dynamic char matrix, to reserve memory for all the words in the first dimension, and then for the size of each word, so i declare:
char **palres;
first of all i go through the file once to know how many reserved words there are, and reserve memory:
<br />
i=0;<br />
while (!feof(archpalres))<br />
{<br />
fgets (temp,MAX_PAL,archpalres);<br />
i++;<br />
}<br />
cantpalres=i-1;<br />
palres = (char**) malloc ((sizeof(char*))*cantpalres);
then, i go to the beginning of the file, read the length of every reserved word, and i copy it in a temp string. with malloc i reserve memory and try to copy it in the char **palres, but i'm sure there's something i'm doing wrong, here's the code":
<br />
fseek (archpalres,0,SEEK_SET);<br />
<br />
j=0;<br />
while (!feof(archpalres))<br />
{<br />
i=0;<br />
<br />
c=fgetc(archpalres);<br />
while ((c!='\n') && (!feof(archpalres)))<br />
{<br />
temp[i]=c;<br />
i++;<br />
c=fgetc(archpalres);<br />
}<br />
temp[i]=0;<br />
<br />
*palres = (char*) malloc ((sizeof(char))*i);
<br />
for (k=0;k<=i;k++)<br />
{<br />
palres[j][k]=temp[k];<br />
}<br />
j++;<br />
}<br />
i think i'm not reserving memory in a good way with malloc. I tried checking with the debugger, but i don't understand what it says. It's the first time i use malloc for double (**) pointers.
Thanks!
|
|
|
|
|
Are you getting (compiler, linker, runtime) errors somewhere? What are you seeing in the debugger?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
i got it
thanks anyways!
the problem was this line:
*palres = (char*) malloc ((sizeof(char))*i);
it works with:
palres[j] = (char*) malloc ((sizeof(char))*i);
|
|
|
|
|
cantpalres=i-1;
palres = (char**) malloc ((sizeof(char*))*cantpalres);
cantpalres=i;
palres = (char**) malloc (sizeof(char*)*cantpalres);
i=0;
while (!feof(archpalres))
{
if( !fgets (temp,MAX_PAL,archpalres) )
break;
++i;
}
cantpalres=i;
palres = (char**) malloc (sizeof(char*)*cantpalres);
if( !palres )
return;
fseek (archpalres,0,SEEK_SET);
i=0;
while (!feof(archpalres))
{
if( !fgets (temp,MAX_PAL,archpalres) )
break;
pNewline = strchr(temp,'\n');
if( pNewline )
*pNewLine = '\0';
len = strlen(temp) + 1;
palres[i] = (char*) malloc (sizeof(char)*len);
if( palres[i] )
strcpy(palres[i],temp);
++i;
}
Normaly I would not have given a complete solution, but there was to much wrong too explane it all.
INTP
|
|
|
|
|
thanks mr Shaw!
this line you wrote were the answer. i didn't add 1, that's why it didn;t work
len = strlen(temp) + 1; //
|
|
|
|
|
Try (*palres) = (char*) malloc ((sizeof(char))*i); parles[j] will also work as you have found out. The problem is in the order of precedence. The brackets will solve this problem.
Dave
|
|
|
|
|