简体   繁体   中英

How to convert a string to byte array?

I have the following char*:

char*Problem = "\x8B\x15\x00\x00\x00\x00\x8B\x7C\x24\x14\x85\xC9\x74\x16\x8B\x03\x89\x01\x8B\x2D\x00\x00\x00\x00\x8B\x15\x00\x00\x00\x00\x8B\x0D\x00\x00\x00\x00\x83\xC1\x04"

I am trying to get this:

unsigned char buf[] = {0x8B, 0x15, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x7C, 0x24, 0x14, 0x85, 0xC9, 0x74, 0x16, 0x8B, 0x03, 0x89, 0x01, 0x8B, 0x2D, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x15, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x83, 0xC1, 0x04};

But by runtime. I am reading the char* from a textbox.

I tried the following:

UnicodeString Maskstr = Edit2->Text; //My char*Problem above
const char *chr = AnsiString(Maskstr).c_str();
char* MaskConverted = const_cast<char*>( chr );
unsigned char NewArray[40];
strcpy( (char*) NewArray, MaskConverted  );

I thought, my unsigned char would be NewArray now, but it´s not. So how to get the correct result?..

I am a bit stuck right now. Any ideas?

Thank you! :)

Your example improperly initializes the Problem variable (it is truncating data) after the 2nd byte.

But let's just assume for the sake of argument that your TEdit actually contains the complete text you are interested in. Does the TEdit contain actual raw bytes that have been extended as-is to 16-bit Unicode chars, or does it contain a hex-encoded representation of the raw bytes? This makes a BIG difference in how you process the text.

If the text contains raw bytes , then your processing code is incorrectly storing a char* pointer to data that belongs to a temporary AnsiString that gets destroyed before you use the pointer, thus exhibiting undefined behavior . Don't destroy the AnsiString until after you have copied from it:

AnsiString Maskstr = Edit2->Text;
unsigned char NewArray[40];
strncpy(reinterpret_cast<char*>(NewArray), const_cast<char*>(s.c_str()), 40);

Alternatively, simply don't convert to AnsiString at all, you can just truncate the Unicode characters instead:

UnicodeString Maskstr = Edit2->Text;
unsigned char NewArray[40] = {};
int maxlen = Maskstr.Length();
if (maxlen > 40) maxlen = 40;
for(int i = 0; i < maxlen; ++i) {
    NewArray = static_cast<unsigned char>(Maskstr[i+1]);
}

However, if the text actually contains a hex-encoded representation of raw bytes, then you need to parse the text to decode the hex sequences into actual bytes, eg:

#include <stdio.h>

UnicodeString Maskstr = Edit2->Text;
const WideChar *ptr = Maskstr.c_str();
unsigned char NewArray[40] = {};
int maxlen = Maskstr.Length();
if (maxlen > 40) maxlen = 40;
for(int i = 0; i < maxlen; ++i)
{
    unsigned int b, numChars;
    if (swscanf(ptr, L"\\x%2x%n", &b, &numChars) != 1) break;
    NewArray[i] = static_cast<unsigned char>(b);
    ptr += numChars;
}

The compiler will compile tabbed hex values but I couldn't find a function to translate it - although Remy is using a scanf. I changed the tab to 0x format and used strtol.

// convert hex string to char/byte values
size_t TForm1::TranslateTabbedHex(const AnsiString str, std::vector<unsigned char> & result)
{
  AnsiString char_str(str);
  char * pEnd;

  size_t pos = char_str.AnsiPos("\\");
  for( ; pos; pos = char_str.AnsiPos("\\") )
  {
    if( pos != 1)
      char_str.Delete(1, pos-1);

    char_str[1] = '0';
    result.push_back( strtol(char_str.c_str(), &pEnd, 0) );
    char_str.Delete(1,1);
  }

  return result.size();
}
//---------------------------------------------------------------------------

void TForm1::TestTranslateTabbedHex(std::vector<unsigned char> & result)
{
  unsigned char test_value[] = "\x8B\x15\x00\x00\x00\x00\x8B\x7C\x24\x14"
                               "\x85\xC9\x74\x16\x8B\x03\x89\x01\x8B\x2D"
                               "\x00\x00\x00\x00\x8B\x15\x00\x00\x00\x00"
                               "\x8B\x0D\x00\x00\x00\x00\x83\xC1\x04";

  //"\x8B\x15\x00\x00\x00\x00\x8B\x7C\x24\x14\x85\xC9\x74\x16\x8B\x03\x89\x01\x8B\x2D\x00\x00\x00\x00\x8B\x15\x00\x00\x00\x00\x8B\x0D\x00\x00\x00\x00\x83\xC1\x04"

  Memo1->Clear();

  size_t test_size =  sizeof test_value - 1;
  AnsiString length( test_size );
  Memo1->Lines->Add( AnsiString("size test_value=") + length );
  Memo1->Lines->Add( AnsiString("size result=") + result.size() );

  AnsiString text(Edit1->Text);
  if(Edit1->Text.Length() > 0)
    Memo1->Lines->Add( AnsiString("first char value in EditBox = ") + text[1]);
  else
    Memo1->Lines->Add("No text in EditBox");

  Memo1->Lines->Add(" ");

  if(result.size() == 0)
  { Memo1->Lines->Add("result = 0 length");
  }
  else
  { size_t test_size = std::min( result.size(), test_size );  // #include <algorithm>
    Memo1->Lines->Add("test  - result - same");
    for(size_t i=0; i<test_size; i++)
    { AnsiString line(test_value[i]);
      line += AnsiString("  -  ") + result[i];
      line += AnsiString("  -  ") + AnsiString((result[i] == test_value[i]) ? "Yes" : "No");
      Memo1->Lines->Add( line );
    }
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  std::vector<unsigned char> result;
  TranslateTabbedHex(Edit1->Text, result);
  TestTranslateTabbedHex( result );
}
//---------------------------------------------------------------------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM