Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > \x used with no following hex digits

Reply
Thread Tools

\x used with no following hex digits

 
 
abhi147@gmail.com
Guest
Posts: n/a
 
      07-31-2006
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 !!

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      07-31-2006
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> 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"


> 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
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-31-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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]);
> }

 
Reply With Quote
 
Giorgio Silvestri
Guest
Posts: n/a
 
      07-31-2006

"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> ha scritto nel messaggio
news:eakgre$a2o$(E-Mail Removed)...
> In article <(E-Mail Removed) .com>,
> <(E-Mail Removed)> 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"

>
> > 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




 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-31-2006
Martin Ambuhl wrote:
> (E-Mail Removed) 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]); }

 
Reply With Quote
 
Giorgio Silvestri
Guest
Posts: n/a
 
      07-31-2006

"Martin Ambuhl" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed)...
> (E-Mail Removed) 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




 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-31-2006
Giorgio Silvestri wrote:
> "Martin Ambuhl" <(E-Mail Removed)> ha scritto nel messaggio
> news:(E-Mail Removed)...
>> (E-Mail Removed) 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-31-2006
"Giorgio Silvestri" <(E-Mail Removed)> writes:
[...]
> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
abhi147@gmail.com
Guest
Posts: n/a
 
      07-31-2006
Martin Ambuhl wrote:
> Giorgio Silvestri wrote:
> > "Martin Ambuhl" <(E-Mail Removed)> ha scritto nel messaggio
> > news:(E-Mail Removed)...
> >> (E-Mail Removed) 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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-31-2006
Keith Thompson said:

> "Giorgio Silvestri" <(E-Mail Removed)> writes:
> [...]
>> 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hex digits to special chars abhi147@gmail.com C Programming 8 08-04-2006 05:37 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
How to escape hex digits in a regular expression!!! aefxx Java 1 12-17-2004 10:12 PM
4 digits hex (WORD) Magix C Programming 2 06-15-2004 01:19 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments