Escape a string to bytes

P

Philliam Auriemma

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

Stefan Ram

Philliam Auriemma said:
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(); }
 
P

Philliam Auriemma

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

Victor Bazarov

Philliam said:
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.

V
 
D

Daniel Giaimo

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

Philliam Auriemma

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.

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++.
 
S

Stefan Ram

Philliam Auriemma said:
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'; }
 
P

Philliam Auriemma

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

Stefan Ram

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

This is already possible with the version you just quoted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top