Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Escape a string to bytes

1 view
Skip to first unread message

Philliam Auriemma

unread,
Jan 31, 2010, 1:32:58 PM1/31/10
to
Hey guys,

I have a problem. I am building a program that needs to take a string
like "\x20\x21" or something (just the fact that it's hex is
significant) and turn it into a string that has those sequences turned
into 'bytes' or whatever. So, In the previous example, it would take
the string "\x20\x21" and turn it into " !". The source from which I
am getting this string does not provide any way to do this, so I need
the source to pass a C++ dll a string, C++ works on it, and returns
the proper string. The format in which it must be is not \xblah, it
can just be numbers separated by a space, or whatever would be
easiest, like "20 21" etc.
Thanks for any help.

Message has been deleted

Philliam Auriemma

unread,
Jan 31, 2010, 2:02:48 PM1/31/10
to
On Jan 31, 10:54 am, r...@zedat.fu-berlin.de (Stefan Ram) wrote:

> Philliam Auriemma <phil.aurie...@gmail.com> writes:
> >the string "\x20\x21" and turn it into " !". The source from which I
>
>   The following prints » !«, but I wrote it in C, not C++,
>   maybe you can translate it to C++.
>
> #include <stdio.h> /* putchar  */
> #include <ctype.h> /* isxdigit */
>
> void append( unsigned int const value ){ putchar(( int )value ); }
>
> void terminate( void ){ putchar( '\n' ); }
>
> int main( void )
> { const char * const source = "\\x20 21";
>   for( const char * p = source; *p; )
>   { while( !isxdigit( *p ) && *p )++p;
>     if( *p )
>     { unsigned int value; sscanf( p, "%x", &value );
>       append( value );
>       while( isxdigit( *p ) && *p )++p; }}
>   terminate(); }

Thanks, I'll try it out right away.

Victor Bazarov

unread,
Jan 31, 2010, 2:49:41 PM1/31/10
to

I honestly don't understand what the problem is.

std::string space_bang_ascii("\x20\x21");

Perhaps you need to specify it a bit clearer... Try pseudocode.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Daniel Giaimo

unread,
Jan 31, 2010, 3:13:11 PM1/31/10
to
On 1/31/2010 2:49 PM, Victor Bazarov wrote:
> Philliam Auriemma wrote:
>> I have a problem. I am building a program that needs to take a string
>> like "\x20\x21" or something (just the fact that it's hex is
>> significant) and turn it into a string that has those sequences turned
>> into 'bytes' or whatever. So, In the previous example, it would take
>> the string "\x20\x21" and turn it into " !". The source from which I
>> am getting this string does not provide any way to do this, so I need
>> the source to pass a C++ dll a string, C++ works on it, and returns
>> the proper string. The format in which it must be is not \xblah, it
>> can just be numbers separated by a space, or whatever would be
>> easiest, like "20 21" etc.
>
> I honestly don't understand what the problem is.
>
> std::string space_bang_ascii("\x20\x21");
>
> Perhaps you need to specify it a bit clearer... Try pseudocode.

I think he meant that he is reading the string in from somewhere else
and it is known to consist of sequences like "\\x20\\x21", (doubled '\'
to clarify that the string itself contains a '\'), and he wants to
parse the string.

--
Dan G

Philliam Auriemma

unread,
Jan 31, 2010, 3:43:58 PM1/31/10
to

Yeah, actually I am getting them from Lua which does not support
putting hex characters or whatever you call them in strings, so I
would make a DLL to load from Lua that would pass "40 39 20" for
example (since doing "\x40\x39\x20" would do absolutely nothing, and
would probably be an unknown escape sequence), and then the DLL would
return whatever that would equal if you had done "\x40\x39\x20" in C++.

Message has been deleted

Philliam Auriemma

unread,
Jan 31, 2010, 10:03:39 PM1/31/10
to
On Jan 31, 2:45 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
> Philliam Auriemma <phil.aurie...@gmail.com> writes:
> >the string "\x20\x21" and turn it into " !". The source from which I
>
>   Now, a C++ version:
>
> #include <iostream> /* ::std::cout         */
> #include <ostream>  /* <<                  */
> #include <sstream>  /* ::std::stringstream */
> #include <ios>      /* ::std::hex          */
>
> int main()
> { char const * const source_ = "\\x20 21";
>   ::std::stringstream source( source_ ), target;
>   int val; while( !source.eof() )
>   { source >> ::std::hex >> val;
>     if( source.fail() ){ source.clear(); char d; source.get( d ); }
>     else target <<( char )val; }
>   ::std::cout << target.str() << '\n'; }

Any idea how I could make it into a 'string' that could contain null
characters?

Message has been deleted
0 new messages