From input argument to BYTE[]

Hello I have a program that uses a
BYTE pin[] = "123"; that I use in a outside method.

Now instead of "123" I want it to be set by an input argument.

I would like something like:

std::string arg1(argv[1]);
BYTE pin[] = arg1;

If that had worked..
What is the easiest way to turn the argument to a BYTE [].


You can't assign a static array without a fixed length values at run time. One option would be to use a std::vector<BYTE>

1
2
3
4
5
6
7
std::string s("123");

std::vector<BYTE> vBytes(s.size());
for (size_t i = 0; i < s.size(); i++)
{
    vBytes[i] = static_cast<BYTE>(s[i]);
}
Hmm ok but I need a BYTE[] for the external method.
How do I turn the vector<BYTE> to a BYTE[].
I think I can get the length by the way. With arg1.length(); Does that make it easier?
You can pass a vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <Windows.h>
#include <string>
#include <iostream>
#include <vector>

void SomeFunc(BYTE bytes[], size_t size)
{
  for (size_t i = 0; i < size; i++)
    std::cout << bytes[i];
}

int main()
{

  std::string s("123");

  std::vector<BYTE> vBytes(s.size());
  for (size_t i = 0; i < s.size(); i++)
  {
    vBytes[i] = static_cast<BYTE>(s[i]);
  }

  SomeFunc(&vBytes[0], vBytes.size());
}
Thank you!
Topic archived. No new replies allowed.