conversion from `const char*' to `byte'

DTDest

I have trouble converting const char to byte. I'm reading file using ifstream and it gives me content as string then I'm converting string to const char using c_str(); and then trying insert it to byte array for packet sending purpose. I'm new to c++ and can't understand how I must convert char to byte and need your help guys. here is my piece of code please give me some advice

byte buf[42];

const char* fname = path.c_str();

ifstream inFile;
inFile.open(fname);//open the input file

stringstream strStream;
strStream << inFile.rdbuf();//read the file
string str = strStream.str();//str holds the content of the file

vector<string> result = explode(str,',');

for (size_t i = 0; i < result.size(); i++) {
    buf[i] = result[i].c_str(); // Here is Error
    cout << "\"" << result[i] << "\"" << endl;
}

system("pause");

This is data which i take from file : (0x68,0x32,0x01,0x7B,0x01,0x1F,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00 )

DTDest

i did it by myself, now i will explain solution. so i wanted String(0x68,0x32,0x03,0x22 etc..) variable split per "," and then convert it into hex value after all input it into byte array as 16 bit hex values.

char buf[42]; // Define Packet

const char* fname = path.c_str(); // File Location


ifstream inFile; //
inFile.open(fname);//open the input file

stringstream strStream;
strStream << inFile.rdbuf();//read the file
string str = strStream.str();//str holds the content of the file



vector<string> result = explode(str,','); // Explode Per comma


for (size_t i = 0; i < result.size(); i++) { // loop for every exploded value

unsigned int x;   
std::stringstream ss;
ss << std::hex <<  result[i];    // Convert String Into Integer value 
ss >> x;
buf[i] = x;

 printf(&buf[i],"%04x",x); //Convert integer value back to 16 bit hex value and store into array


    }



  system("pause");

thanks all for replay.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

No conversion from "const char" to "int"

From Dev

No conversion from DWORD to const char*

From Dev

Invalid conversion from ‘const char*’ to ‘unsigned char*’

From Dev

invalid conversion from 'const char*' to 'char*'

From Dev

Invalid conversion from 'char' to 'const char *'

From Dev

error: invalid conversion from ‘char’ to ‘const char*

From Dev

invalid conversion from ‘const char*’ to ‘char*

From Dev

error: invalid conversion from ‘char’ to ‘const char*

From Dev

Conversion not valid from char to const char*

From Dev

invalid conversion from ‘const char*’ to ‘char’ / uninitialized const member in struct

From Dev

Java - possible lossy conversion from byte to char

From Dev

g++ strstr says invalid conversion from const char * to char *

From Dev

ALL_OF invalid conversion from 'char' to 'const char*' [-fpermissive]

From Dev

strcpy() causes invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]

From Dev

C++ invalid conversion from ‘char*’ to ‘const unsigned char*’

From Dev

EVP_DigestUpdate and "invalid conversion from ‘unsigned char*’ to ‘const char*’"

From Dev

Cant find resolution for the error "invalid conversion from char to const char*"

From Dev

error: invalid conversion from 'unsigned char*' to 'const signed char*'

From Dev

C - [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]

From Dev

error: invalid conversion from 'char' to 'const char*' [-fpermissive]|

From Dev

Short one: invalid conversion from char to const char*

From Dev

c++ invalid conversion from 'const char*' to 'char*'

From Dev

Invalid conversion from 'char' to 'const char*' [-fpermissive](idk why)

From Dev

Conversion from string to const char* + size_t in ""operator

From Dev

No suitable conversion function from "std::string" to "const char *" exists

From Dev

C++: invalid conversion from 'const char*' to 'size_t'?

From Dev

Conversion from int to c-string (const char*) fails

From Dev

Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

From Dev

Compare two char variable using strcmp in c++ shows invalid conversion from 'char' to 'const char*'

Related Related

  1. 1

    No conversion from "const char" to "int"

  2. 2

    No conversion from DWORD to const char*

  3. 3

    Invalid conversion from ‘const char*’ to ‘unsigned char*’

  4. 4

    invalid conversion from 'const char*' to 'char*'

  5. 5

    Invalid conversion from 'char' to 'const char *'

  6. 6

    error: invalid conversion from ‘char’ to ‘const char*

  7. 7

    invalid conversion from ‘const char*’ to ‘char*

  8. 8

    error: invalid conversion from ‘char’ to ‘const char*

  9. 9

    Conversion not valid from char to const char*

  10. 10

    invalid conversion from ‘const char*’ to ‘char’ / uninitialized const member in struct

  11. 11

    Java - possible lossy conversion from byte to char

  12. 12

    g++ strstr says invalid conversion from const char * to char *

  13. 13

    ALL_OF invalid conversion from 'char' to 'const char*' [-fpermissive]

  14. 14

    strcpy() causes invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]

  15. 15

    C++ invalid conversion from ‘char*’ to ‘const unsigned char*’

  16. 16

    EVP_DigestUpdate and "invalid conversion from ‘unsigned char*’ to ‘const char*’"

  17. 17

    Cant find resolution for the error "invalid conversion from char to const char*"

  18. 18

    error: invalid conversion from 'unsigned char*' to 'const signed char*'

  19. 19

    C - [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]

  20. 20

    error: invalid conversion from 'char' to 'const char*' [-fpermissive]|

  21. 21

    Short one: invalid conversion from char to const char*

  22. 22

    c++ invalid conversion from 'const char*' to 'char*'

  23. 23

    Invalid conversion from 'char' to 'const char*' [-fpermissive](idk why)

  24. 24

    Conversion from string to const char* + size_t in ""operator

  25. 25

    No suitable conversion function from "std::string" to "const char *" exists

  26. 26

    C++: invalid conversion from 'const char*' to 'size_t'?

  27. 27

    Conversion from int to c-string (const char*) fails

  28. 28

    Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

  29. 29

    Compare two char variable using strcmp in c++ shows invalid conversion from 'char' to 'const char*'

HotTag

Archive