473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

\x used with no following hex digits

Hi ,

I have a program where I want to print a string
"457e31b2db200dc125f3e00886ff57de"
like "\x45\x7e\x31\xb2\xdb\x20\x0d\xc1\x25\xf3\xe0\x08\ x86\xff\x57\xde"
.. But in each and every case it is giving an error :

test.c:16:9 : \x used with no following hex digits

*********Test.c****************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>

int main(int argc, char* argv[])
{
int i;
unsigned char mbBuf[16] = "457e31b2db200dc125f3e00886ff57de";
for(i=0;i<16;i++)
printf("\x%02",mbBuf[i]);
}

Need help !!

Jul 31 '06 #1
14 14056
In article <11**********************@m79g2000cwm.googlegroups .com>,
<ab*****@gmail.comwrote:
I have a program where I want to print a string
"457e31b2db200dc125f3e00886ff57de"
like "\x45\x7e\x31\xb2\xdb\x20\x0d\xc1\x25\xf3\xe0\x08\ x86\xff\x57\xde"
> printf("\x%02",mbBuf[i]);
printf("\\x%02",mbBuf[i]);

When you want a \ to appear in the output string, you have to
escape the \ by doubling it.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Jul 31 '06 #2
ab*****@gmail.com wrote:
Hi ,

I have a program where I want to print a string
"457e31b2db200dc125f3e00886ff57de"
like "\x45\x7e\x31\xb2\xdb\x20\x0d\xc1\x25\xf3\xe0\x08\ x86\xff\x57\xde"
. But in each and every case it is giving an error :
[OP's code at EOM]

#include <stdio.h>

int main(void)
{
unsigned char mbBuf[] = "457e31b2db200dc125f3e00886ff57de";
unsigned i, n = sizeof mbBuf - 1;
for (i = 0; i < n; i += 2)
printf("\\x%c%c", mbBuf[i], mbBuf[i + 1]);
return 0;
}

[OP's code]
test.c:16:9 : \x used with no following hex digits

*********Test.c****************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>

int main(int argc, char* argv[])
{
int i;
unsigned char mbBuf[16] = "457e31b2db200dc125f3e00886ff57de";
for(i=0;i<16;i++)
printf("\x%02",mbBuf[i]);
}
Jul 31 '06 #3

"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.caha scritto nel messaggio
news:ea**********@canopus.cc.umanitoba.ca...
In article <11**********************@m79g2000cwm.googlegroups .com>,
<ab*****@gmail.comwrote:
I have a program where I want to print a string
"457e31b2db200dc125f3e00886ff57de"
like "\x45\x7e\x31\xb2\xdb\x20\x0d\xc1\x25\xf3\xe0\x08\ x86\xff\x57\xde"
printf("\x%02",mbBuf[i]);

printf("\\x%02",mbBuf[i]);
Wrong code. You don't print mbBuf[i].
>
When you want a \ to appear in the output string, you have to
escape the \ by doubling it.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int i;
/* even number of chars !!! */
unsigned char mbBuf[] = "457e31b2db200dc125f3e00886ff57de";

for(i=0;i<(sizeof(mbBuf) / sizeof(mbBuf[0]))/2;i++) {
printf("\\x%c%c",mbBuf[2*i],mbBuf[2*i+1]);
}
printf("\n");
return EXIT_SUCCESS;
}
--
Giorgio Silvestri
DSP/Embedded/Real Time OS (RTOS) Software Engineer


Jul 31 '06 #4
Martin Ambuhl wrote:
ab*****@gmail.com wrote:
>Hi ,

I have a program where I want to print a string
"457e31b2db200dc125f3e00886ff57de"
like "\x45\x7e\x31\xb2\xdb\x20\x0d\xc1\x25\xf3\xe0\x08\ x86\xff\x57\xde"
. But in each and every case it is giving an error :

[OP's code at EOM]

#include <stdio.h>

int main(void)
{
unsigned char mbBuf[] = "457e31b2db200dc125f3e00886ff57de";
unsigned i, n = sizeof mbBuf - 1;
^^^
should be 2. Sorry.
[e.g. if mbBuf[] = "1234", the last pair begins at mbBuf[2].
sizeof mbBuf is 5, n = 3, and 2 is the largest integer
less than 3]. Or I could set n = strlen(mbBuf) - 1 instead,
but only at the cost of an extra function call and #including
<string.h>.]
for (i = 0; i < n; i += 2)
printf("\\x%c%c", mbBuf[i], mbBuf[i + 1]);
return 0;
}

[OP's code]
>test.c:16:9 : \x used with no following hex digits

*********Test.c****************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <wchar.h>

int main(int argc, char* argv[])
{
int i;
unsigned char mbBuf[16] = "457e31b2db200dc125f3e00886ff57de";
for(i=0;i<16;i++)
printf("\x%02",mbBuf[i]); }
Jul 31 '06 #5

"Martin Ambuhl" <ma*****@earthlink.netha scritto nel messaggio
news:4j************@individual.net...
ab*****@gmail.com wrote:
Hi ,

I have a program where I want to print a string
"457e31b2db200dc125f3e00886ff57de"
like "\x45\x7e\x31\xb2\xdb\x20\x0d\xc1\x25\xf3\xe0\x08\ x86\xff\x57\xde"
. But in each and every case it is giving an error :

[OP's code at EOM]

#include <stdio.h>

int main(void)
{
unsigned char mbBuf[] = "457e31b2db200dc125f3e00886ff57de";
unsigned i, n = sizeof mbBuf - 1;
for (i = 0; i < n; i += 2)
printf("\\x%c%c", mbBuf[i], mbBuf[i + 1]);
return 0;
}
It could not work.

Missing:

printf("\n"); /* :-) */

before

return 0;
--
Giorgio Silvestri
DSP/Embedded/Real Time OS (RTOS) Software Engineer


Jul 31 '06 #6
Giorgio Silvestri wrote:
"Martin Ambuhl" <ma*****@earthlink.netha scritto nel messaggio
news:4j************@individual.net...
>ab*****@gmail.com wrote:
>>Hi ,

I have a program where I want to print a string
"457e31b2db200dc125f3e00886ff57de"
like "\x45\x7e\x31\xb2\xdb\x20\x0d\xc1\x25\xf3\xe0\x08\ x86\xff\x57\xde"
. But in each and every case it is giving an error :
[OP's code at EOM]

#include <stdio.h>

int main(void)
{
unsigned char mbBuf[] = "457e31b2db200dc125f3e00886ff57de";
unsigned i, n = sizeof mbBuf - 1;
for (i = 0; i < n; i += 2)
printf("\\x%c%c", mbBuf[i], mbBuf[i + 1]);
return 0;
}

It could not work.

Missing:

printf("\n"); /* :-) */

before

return 0;
Thanks for pointing out one of (at least) two errors in an extremely
simple program. I shouldn't offer advice after being up three days.
Jul 31 '06 #7
"Giorgio Silvestri" <gi**************@libero.itwrites:
[...]
It could not work.

Missing:

printf("\n"); /* :-) */

before

return 0;
It could work. Whether a trailing '\n' is required for a text stream
is implementation-defined.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 31 '06 #8
Martin Ambuhl wrote:
Giorgio Silvestri wrote:
"Martin Ambuhl" <ma*****@earthlink.netha scritto nel messaggio
news:4j************@individual.net...
ab*****@gmail.com wrote:
Hi ,

I have a program where I want to print a string
"457e31b2db200dc125f3e00886ff57de"
like "\x45\x7e\x31\xb2\xdb\x20\x0d\xc1\x25\xf3\xe0\x08\ x86\xff\x57\xde"
. But in each and every case it is giving an error :
[OP's code at EOM]

#include <stdio.h>

int main(void)
{
unsigned char mbBuf[] = "457e31b2db200dc125f3e00886ff57de";
unsigned i, n = sizeof mbBuf - 1;
for (i = 0; i < n; i += 2)
printf("\\x%c%c", mbBuf[i], mbBuf[i + 1]);
return 0;
}
It could not work.

Missing:

printf("\n"); /* :-) */

before

return 0;

Thanks for pointing out one of (at least) two errors in an extremely
simple program. I shouldn't offer advice after being up three days.
Thanks a lot to all of you . Although it solved the question i had
asked .. But now i m stuck with another problem related to the query .

Actually when i put a "\x" manually in front of every two digits in the
string the string gets printed as "¦ñ²±Z˜-=§ßÉ¢Sp{¦"

The code which prints these special characters is :

int main(int argc, char* argv[])
{
char mbBuf[BUF_SIZE] =
"\xb2\xa4\xfd\xf1\x5a\xf7\xc4\xf2\x15\xe1\x90\x9b\ xe4\x70\x7b\xdb";

printf("\nThe mbBuf string is %s\n",mbBuf);

}

but when i print the string "b2a4fdf15af7c4f215e1909be4707bdb" with
the \x using the for loop it doesn't print those special chars :-(

Jul 31 '06 #9
Keith Thompson said:
"Giorgio Silvestri" <gi**************@libero.itwrites:
[...]
>It could not work.

Missing:

printf("\n"); /* :-) */

before

return 0;

It could work. Whether a trailing '\n' is required for a text stream
is implementation-defined.
Yes, but I think this is a translation issue. It seems to me that maybe
Giorgio probably meant "it might not work", perhaps.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 31 '06 #10

"Richard Heathfield" <in*****@invalid.invalidha scritto nel messaggio
news:KY********************@bt.com...
Keith Thompson said:
"Giorgio Silvestri" <gi**************@libero.itwrites:
[...]
It could not work.

Missing:

printf("\n"); /* :-) */

before

return 0;
It could work. Whether a trailing '\n' is required for a text stream
is implementation-defined.

Yes, but I think this is a translation issue. It seems to me that maybe
Giorgio probably meant "it might not work", perhaps.
Yes.
--
Giorgio Silvestri
DSP/Embedded/Real Time OS (RTOS) Software Engineer

Jul 31 '06 #11

<ab*****@gmail.comha scritto nel messaggio
news:11**********************@m79g2000cwm.googlegr oups.com...
Martin Ambuhl wrote:
Giorgio Silvestri wrote:
"Martin Ambuhl" <ma*****@earthlink.netha scritto nel messaggio
news:4j************@individual.net...
ab*****@gmail.com wrote:
>int main(int argc, char* argv[])
{
char mbBuf[BUF_SIZE] =
"\xb2\xa4\xfd\xf1\x5a\xf7\xc4\xf2\x15\xe1\x90\x9b \xe4\x70\x7b\xdb";

printf("\nThe mbBuf string is %s\n",mbBuf);

}

BUF_SIZE ?

if BUF_SIZE is equal to the value of
strlen("\xb2\xa4\xfd\xf1\x5a\xf7\xc4\xf2\x15\xe1\x 90\x9b\xe4\x70\x7b\xdb")

then mbBuf is not '\0' terminated.
char mbBuf[] =
"\xb2\xa4\xfd\xf1\x5a\xf7\xc4\xf2\x15\xe1\x90\x9b\ xe4\x70\x7b\xdb";
--
Giorgio Silvestri
DSP/Embedded/Real Time OS (RTOS) Software Engineer



Jul 31 '06 #12
In article <11**********************@m79g2000cwm.googlegroups .comab*****@gmail.com writes:
....
unsigned char mbBuf[16] = "457e31b2db200dc125f3e00886ff57de";
The string on the right is a bit long as initialiserf for the array
on the left.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jul 31 '06 #13
"Giorgio Silvestri" <gi**************@libero.itwrites:
"Richard Heathfield" <in*****@invalid.invalidha scritto nel messaggio
news:KY********************@bt.com...
>Keith Thompson said:
"Giorgio Silvestri" <gi**************@libero.itwrites:
[...]
It could not work.
[...]
It could work. Whether a trailing '\n' is required for a text stream
is implementation-defined.

Yes, but I think this is a translation issue. It seems to me that maybe
Giorgio probably meant "it might not work", perhaps.

Yes.
Ok. I read it as "It (could not) work", not as "It could (not work)",
and I expect many others would read it the same way.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 31 '06 #14
On 31 Jul 2006 03:41:51 -0700, ab*****@gmail.com wrote:
Martin Ambuhl wrote:
>> I have a program where I want to print a string
>>"457e31b2db200dc125f3e00886ff57de"
>>like "\x45\x7e\x31\xb2\xdb\x20\x0d\xc1\x25\xf3\xe0\x08\ x86\xff\x57\xde"
<snip>
Thanks a lot to all of you . Although it solved the question i had
asked .. But now i m stuck with another problem related to the query .

Actually when i put a "\x" manually in front of every two digits in the
string the string gets printed as "¦ñ²±Z˜-=§ßÉ¢Sp{¦"

The code which prints these special characters is :

int main(int argc, char* argv[])
{
char mbBuf[BUF_SIZE] =
"\xb2\xa4\xfd\xf1\x5a\xf7\xc4\xf2\x15\xe1\x90\x9b\ xe4\x70\x7b\xdb";

printf("\nThe mbBuf string is %s\n",mbBuf);

}

but when i print the string "b2a4fdf15af7c4f215e1909be4707bdb" with
the \x using the for loop it doesn't print those special chars :-(
Aha. It sounds like what you meant is not "print so that the output is
in the form \x45 ...", but "print the output you would get if you had
\x45 in the source". These are not the same. Backslash escapes
including \xXX in a string (or char) literal are converted _at compile
time_ to the specified byte value(s). For example
char mbBuf[N] = "\xb2\xa4";
(given N is >= 3) should produce the same object/executable (and
must produce the same runtime effect) as
char mbBuf[N] = {0xb2, 0xa4, 0};
or
char mbBuf[N] = {178, 164, 0};

If what you want is to extract values represented as two hex digits
each in a string, and output those values, you must do the conversion
explicitly, not expect \x to cause it to happen for you:

char xx [] = "457e"; /* etc. */
int i, x; /* in general i might better be size_t */
for( i = 0; xx[i] /* && xx[i+1] */; i += 2 ){
/* or i < strlen(xx) or with array visible i < sizeof xx - 1 */
#if ONEWAY
sscanf (xx+i, "%2x", &x);
/* in general should check return value from sscanf for error(s)
but here I assume input data is always good and don't bother */
#elif ANOTHERWAY
char yy [3] = {0};
memcpy (yy, xx+i, 2); /* extract 2 digits plus null terminator */
x = /* (int) */ strtol (yy, NULL, 16);
#elif EXPLICITWAY
x = getdig (yy[i]) * 16U + getdig (yy[i+1);
#endif
putchar (x); /* for stdout, or fputc (x, fp); */
/* or have a second buffer and copy the converted bytes into it
then fwrite or if applicable fputs or puts the whole buffer */
}

where int getdig (char d)
can be for ASCII something like
return isdigit(d)? d-'0': toupper(d)-'A'+10;
or for complete portability something like
const char digitlist [] = "0123456789ABCDEF";
return strchr (digitlist, d) - digitlist;
in each case with addition of error checks and handling
if your input data is not guaranteed good or already checked.

- David.Thompson1 at worldnet.att.net
Aug 14 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: srikanth | last post by:
static char buf = {1,2,3,4,5,6,7,8}; printf("Address: %u : Val = %d \n",(int*)buf+1,*((int*)buf+1)); Thanks Vija
1
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating...
13
by: Kosio | last post by:
Hello, I know of a way to extract digits from a number using the %10 and divide by 10. But I am wondering if there is an algorithm out there that does not use a divide by 10 feature. The...
27
by: Luke Wu | last post by:
Is there a C function that returns the number of digits in an input int/long? example: numdigits(123) returns 3 numdigits(1232132) returns 7
4
by: Anoop | last post by:
Hi All I am getting two different outputs when i do an operation using string.digits and test.isdigit(). Is there any difference between the two. I have given the sample program and the output ...
109
by: jmcgill | last post by:
Hello. Is there a method for computing the number of digits, in a given numeric base, of N factorial, without actually computing the factorial? For example, 8! has 5 digits in base 10; 10! has...
4
by: mistral | last post by:
Can anyone help to identify what this encryption used in this script? <html> <body> <script type="text/javascript" language="JavaScript"> function decrypt_p(x){ var l=x.length, b=1024,...
2
by: Ulrich Dorda | last post by:
I need a pytho nscript to read numbers(with loads of digits) from a file, do some basic math on it and write the result out to another file. My problem: I don't get python to use more digits: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.