hi all,

I want to convert a string contain hex format '0x15' and what to convert it integer because i want to write data contain by a integer variable in hex format (binary data) into the file.

here is my code

char * str = "0x15";
int value;

FILE FILE *fptrSample = fopen("e:\\sample.txt","rb+");
if (fptrSample)
{
    value = atoi(str);
    putc(value,fptrSample);
    fclose(fptrSample);
}

after executing this
output is
value = 0

where as i want to the correct output
value = 0x15


can any one help me?.................

Of course, atoi() converts the leading zero for you.
Skip "0x" then use strtol() function.
It's so easy...

>Of course, atoi() converts the leading zero for you.
>Skip "0x" then use strtol() function.
>It's so easy...

well sir many thanks for respoonce

my problem is that I dont want to skip "0x" because this is way we can write binary data into the file e.g

putc(0x15,fptrSample);

now i want to generate "0x15" at runtime which in a char string and first parameter type of putc() is int. So if i skip "0x" the writing into file will be only 15 but I want to write "0x15" so that binary will be written. okay

Many thanks

waiting for your responce

Regrettably you don't understand me.

char* str = "0x15";
int value;
char* junk;
...
value = strtol(str+2,&junk,16); /* str+2 <=> skip 0x */
...

hi all
i want to know how to reverse a string
eg i like apple

ans: Apple like i

Regrettably you don't understand me.

char* str = "0x15";
int value;
char* junk;
...
value = strtol(str+2,&junk,16); /* str+2 <=> skip 0x */
...

Well sir I understand you and did exactly what you said but still it is in problem see my whole code below

int sample_size = 720*496*4;
char *buffer = buffer = new char[8];
char *hexValue;
itoa(sample_size,buffer,16);
hexValue = new char[5];

		while(buffer[i]!=NULL)
		{
			hexValue[0] = '0';
			hexValue[1] = 'x';
			hexValue[2] = buffer[i];
			hexValue[3] = buffer[i+1];
			hexValue[4] = '\0';
			
			value = strtol(hexValue+2,&stopstring,16); /* value is again = 0 but i need 0xnnn*/
			putc(value,fptrSampleVideo);  // writing to the file
			i++;
		}
		delete(hexValue);
		delete(buffer);

C string is an array of char. Get this array size (length of string) then make a loop swapping leading and trailing chars ( s[0]<=>s[n-1], s[1]<=>s[n-2] and so on)...

Stop stop: now I don't understand you. Please, explain what do you want to do really?
If you want fill char value 0x15, use hex char literal '\x15' directly: what for this bustle with conversions of "0x15" string? If you want an array of 5 (or 8) char, declare it as char buffer[8]; - why operator new?
Please, use CODE tag for your snippets:
[code=c] your code

[/code]
We have the intruder in this thread; now one haveaclick has his/her own thread on the Global Problem of String Reversions topic...

hi sir,

my problem is that

I have an integer value e.g. 1428480.
first I have to convert it into hexa decimal format ---------> 0x15cc00.
convert the resultant hexa format data into little endian format ---------> 0x00cc15
write the little endian hex format data into the binary file.

to write binary or hex data into the binary file I am using putc()method having two parameters.

That what I want to do sir...

Do you actually want to convert to little endian or is this some kind of homework project? Because converting to hexadecimal doesn't enter into the picture if you're working with the bytes. The representation only matters for display and such.

Do you actually want to convert to little endian or is this some kind of homework project? Because converting to hexadecimal doesn't enter into the picture if you're working with the bytes. The representation only matters for display and such.

Sir I want to do ti actually because I am professional Software Engineer working in a software house and currently this is my requirement, but i am stuck here. Many thanks

Asif Javaid

Well, since you want to convert to little endian, I'll assume you have something in big endian format already. If that's the case it's a simple matter of reversing the bytes. For example:

void bswap ( unsigned int x )
{
  x = ( x >> 24 ) | ( ( x << 8 ) & 0x00FF0000 ) |
    ( ( x >> 8 ) & 0x0000FF00 ) | ( x << 24 );
}

Though ntohl and htonl are generally a better option if they're available.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.