Click to See Complete Forum and Search --> : Hexidecimal problems in C


bradh352
01-23-2001, 11:38 PM
Alright, I've got a string that's put together like this:
"This is a string\x2cI'm proud of it"
which works beautifully...
BTW, 2c is a comma
But if do this:
"This is a string\x2cAnd I'm proud of it"
The A is interpreted as being part of the hexidecimal number. How do you tell it to end a definition of a hexidecimal number so that digits and A-F don't affect it.

Obviously this is just an example of what I'm doing, actually, the hexidecimal number I'm using is non-viewable and I need it to be in the string I'm creating. This string is statically defined in a stucture so that sprintf is not possible to be used.

Thanks

-Brad

[This message has been edited by bradh352 (edited 23 January 2001).]

BrianDrozd
01-24-2001, 09:36 AM
Okay, I can think of a couple of ways around the problem. First, and probably the best method is to create a string something like this:
char npchar[2] = "\c2"

and then change your printf statement to this:
printf ("This is a sting %sand...",npchar);

This way, there is no confusion about where the hexedecimal code ends, because its segregated out. Seperating the non-printable character is really all you can do, however.

bradh352
01-24-2001, 11:49 AM
Well, like I said, I can't use sprintf, printf or whatever. It is a statically defined string.

Actually it's going out via a socket, and I'm looking for the most effecient method of transfer (both for memory and cpu cycles). Can anyone think of a way to overcome my original problem?

miller
01-24-2001, 12:44 PM
The only thing I can think of is to put a space after the hex you want to print.

Stuka
01-24-2001, 03:56 PM
Shouldn't you be able to pad the hex number with zeros (on the front side, like \x02c) to fill up the space looked for? I mean, the code following \x has to have a length limit (I just don't know what it is http://www.linuxnewbie.org/ubb/smile.gif), and that should do the trick.

bradh352
01-24-2001, 08:56 PM
Well, finally figured it out thanks to the guys on IRC. I should have though of it...completely stupid of me...

it's simple...the string should take the form:
"12345""\x2c""67890"
That way \x2c is evaluated by itself instead of doing:
"12345\x2c67890"
which would evaluate everything after the \x
thanks for the help anyhow...

-Brad

jemfinch
01-25-2001, 07:10 AM
I think Stuka's idea is the best and simplest.

Jeremy