Good day! I program to send data to a specific printer and I have a problem. I need to send tag packets to the printer.

Char s[]={ 0x05, 0x06, 0xD1, 0xE0, 0xEC, 0xE0, 0xF0, 0xE0 }; 

If set manually then everything works. But how do I correctly fill Char s [] from a variable?

The line feed option does not work:

 std::string s6_str = "0x05, 0x06, 0xD1, 0xE0, 0xEC, 0xE0, 0xF0, 0xE0"; s6 = new char [s6_str.length()+1]; std::strcpy (s6, s6_str.c_str()); 

Tell me how to be! Thank you in advance!

  • You make the task very difficult: instead of "" 0x05, 0x06, "write" 0506 ". Converting HEX digits to trivial values. - Vladimir Martyanov
  • By the condition of the problem is it necessary to store the "Tag Packages" in text form? - Vladimir Gamalyan
  • > According to the condition of the problem, it is necessary to store "Tag packages" in text form? The main thing is that in the end the char c hex array is inside ... - VersuS
  • Inside, forgive what? - Vladimir Gamalyan
  • Can you give an example? - VersuS

1 answer 1

First option:

 褋har s[] = { 0x05, 0x06, 0xD1, 0xE0, 0xEC, 0xE0, 0xF0, 0xE0 };` 

stores a sequence of codes. The total size is 8 bytes.

The second option:

 std::string s = "0x05, 0x06, 0xD1, 0xE0, 0xEC, 0xE0, 0xF0, 0xE0"; 

stores the string in an explicit form. Its total size is 47 bytes (if my calculator was not mistaken).

Translation from the second option to the first will include:

  1. Division into substrings (comma);
  2. Conversion from line to number, for example, through stoi ;
  3. Filling a new array of the resulting numbers.

In general, the answer to the question:

How can I fill Char s [] from a variable correctly?

depends on the form in which this "variable" is stored.

If you just need to score a string with codes, then write codes to it, not strings:

 std::string s = "\x05\x06\xD1\xE0\xEC\xE0\xF0\xE0"; 
  • I need on the contrary to get a char which will contain such data {0x05, 0x06, 0xD1, 0xE0, 0xEC, 0xE0, 0xF0, 0xE0}. Initially, I take a regular line - I encode it in HEX and I don鈥檛 understand how to translate this HEX data into char ... and the data is not simple - 1 byte is an internal tag 2 bytes text line size everything else is a text string ... - VersuS
  • @VersuS describe in more detail what is meant by 斜械褉褍 芯斜褘褔薪褍褞 褋褌褉芯泻褍 - 泻芯写懈褉褍褞 械械 胁 HEX . Add this to your question by editing it. My option from steps 1,2,3 does not suit you? - 伪位蔚蠂慰位蠀蟿
  • And how to implement these steps? - VersuS
  • @VersuS 2 and 3 points, it seems to me should be obvious. For item 1, you can use string::find or more specific string processing functions. It is simply not possible to describe actions to elementary in every answer. There must be some basic knowledge. - 伪位蔚蠂慰位蠀蟿
  • Splitting into substrings is not a problem. But for some reason when I try to fill the array strcpy (s [i], str.c_str ()); does not say: invalid conversion from 'char' to 'char *'. Tell me what I'm doing wrong ... - VersuS