How to write a BYTE string as a char*

A

Angus

Hello

I have a load of bytes like this:

unsigned char mybyte[] = {0xa1, 0x14, 0x02, 0x01, 0x01, 0x02, 0x01, 0x0a,
0x30, 0x0c, 0x80,

0x03, 0x32, 0x30, 0x31, 0x62, 0x05, 0x80, 0x03, 0x32, 0x30, 0x32};



But I want to write them as a string. so I tried this:

char szSend[] =
"\x0a1\x014\x002\x001\x001\x002\x001\x00a\x030\x00c\x080\x003201\x062\x005\x
080\x003202";

But that gave me a compile error

error C2022: '12801' : too big for character
error C2022: '12802' : too big for character


Can I not do this? What is easiest way for me to put these non printing
characters into a string? Or will I need to use something like sprintf to
do this?
 
S

sam_cit

Can I not do this? What is easiest way for me to put these non printing
characters into a string? Or will I need to use something like sprintf to
do this?

Which characters do you think are non-printable?
 
R

Richard Tobin

Angus said:
unsigned char mybyte[] = {0xa1, 0x14, 0x02, 0x01, 0x01, 0x02, 0x01, 0x0a,
0x30, 0x0c, 0x80,
0x03, 0x32, 0x30, 0x31, 0x62, 0x05, 0x80, 0x03, 0x32, 0x30, 0x32};
But I want to write them as a string. so I tried this:

char szSend[] =
"\x0a1\x014\x002\x001\x001\x002\x001\x00a\x030\x00c\x080\x003201\x062\x005\x
080\x003202";

Are those two supposed to match? In the first you have 03 32 30 31 and
in the second 003201.

The first is perfectly good as a string if you add a zero on the end.

-- Richard
 
L

Lew Pitcher

Angus said:
Hello

I have a load of bytes like this:

unsigned char mybyte[] = {0xa1, 0x14, 0x02, 0x01, 0x01, 0x02, 0x01, 0x0a,
0x30, 0x0c, 0x80,

0x03, 0x32, 0x30, 0x31, 0x62, 0x05, 0x80, 0x03, 0x32, 0x30, 0x32};



But I want to write them as a string. so I tried this:

char szSend[] =
"\x0a1\x014\x002\x001\x001\x002\x001\x00a\x030\x00c\x080\x003201\x062\x005\x
080\x003202";

But that gave me a compile error

error C2022: '12801' : too big for character
error C2022: '12802' : too big for character

Your initialization string contains two hex constants that appear to be
too big to be characters.
\x003201 == 12801
\x003202 == 12802

Correct these constants so that they each represent something that can
be stored in a char, and your compiler will stop complaining.

HTH
 
A

Angus

Richard Tobin said:
Angus said:
unsigned char mybyte[] = {0xa1, 0x14, 0x02, 0x01, 0x01, 0x02, 0x01, 0x0a,
0x30, 0x0c, 0x80,
0x03, 0x32, 0x30, 0x31, 0x62, 0x05, 0x80, 0x03, 0x32, 0x30, 0x32};
But I want to write them as a string. so I tried this:

char szSend[] =
"\x0a1\x014\x002\x001\x001\x002\x001\x00a\x030\x00c\x080\x003201\x062\x005\
x
080\x003202";

Are those two supposed to match? In the first you have 03 32 30 31 and
in the second 003201.

** Just a typo mistake.
The first is perfectly good as a string if you add a zero on the end.

** But it doesn't compile - get
error C2022: '12801' : too big for character
error C2022: '12802' : too big for character

on line char szSend[] ...

Even if I put a null on the end
 
A

Angus

Richard Tobin said:
Angus said:
unsigned char mybyte[] = {0xa1, 0x14, 0x02, 0x01, 0x01, 0x02, 0x01, 0x0a,
0x30, 0x0c, 0x80,
0x03, 0x32, 0x30, 0x31, 0x62, 0x05, 0x80, 0x03, 0x32, 0x30, 0x32};
But I want to write them as a string. so I tried this:

char szSend[] =
"\x0a1\x014\x002\x001\x001\x002\x001\x00a\x030\x00c\x080\x003201\x062\x005\
x
080\x003202";

Are those two supposed to match? In the first you have 03 32 30 31 and
in the second 003201.

The first is perfectly good as a string if you add a zero on the end.

It doesn't like mixing of the hex values and writing eg eg abc. If all
defined as hex values it compiles ok. Maybe a compiler issue. Using VC++
v6.
 
R

Richard Tobin

** Just a typo mistake.

Then it's a typo in your program, because \x003201 is 12801, which is
what your error message says:
error C2022: '12801' : too big for character
error C2022: '12802' : too big for character

Fix the typo, and if it still doesn't work, post the code again (don't copy
it by hand, cut and paste it).
on line char szSend[] ...

Even if I put a null on the end

The zero I suggested is supposed to go on the end of the {...} version,
not the "..." version.

-- Richard
 
K

Kenneth Brody

Richard said:
Angus said:
unsigned char mybyte[] = {0xa1, 0x14, 0x02, 0x01, 0x01, 0x02, 0x01, 0x0a,
0x30, 0x0c, 0x80,
0x03, 0x32, 0x30, 0x31, 0x62, 0x05, 0x80, 0x03, 0x32, 0x30, 0x32};
But I want to write them as a string. so I tried this:

char szSend[] =
"\x0a1\x014\x002\x001\x001\x002\x001\x00a\x030\x00c\x080\x003201\x062\x005\x
080\x003202";

Are those two supposed to match? In the first you have 03 32 30 31 and
in the second 003201.

The first is perfectly good as a string if you add a zero on the end.

He is assuming that "\x003201" is the same as '\x003' '2' '0' '1',
but the compiler is obviously treating the entire "003201" as the
hex value (12801 decimal -- the number in the error message) for
the character.

Either use hex for everything:

...\x03\x32\x30\x31\x62...

or break the string:

...\x03" "201" "\x62...

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

Angus said:
I have a load of bytes like this:

unsigned char mybyte[] = {0xa1, 0x14, 0x02, 0x01, 0x01, 0x02, 0x01, 0x0a,
0x30, 0x0c, 0x80,

0x03, 0x32, 0x30, 0x31, 0x62, 0x05, 0x80, 0x03, 0x32, 0x30, 0x32};



But I want to write them as a string. so I tried this:

char szSend[] =
"\x0a1\x014\x002\x001\x001\x002\x001\x00a\x030\x00c\x080\x003201\x062\x005\x
080\x003202";

Be sure you understand the difference betwene a string and a string
literal. A string is a run-time data format, defined as "a contiguous
sequence of characters terminated by and including the first null
character". A string literal is a construct in a C source file,
delimited by quotation marks '"'.

Why do you want to use a string literal rather than an array
initializer? A string literal appends a null character '\0', but you
can do that anyway. If most or all of the characters in your array
are non-printable, what's the point of using a string literal to
initialize them?
 
O

Old Wolf

Angus said:
unsigned char mybyte[] = {0xa1, 0x14, 0x02, 0x01, 0x01, 0x02, 0x01, 0x0a,
0x30, 0x0c, 0x80,
0x03, 0x32, 0x30, 0x31, 0x62, 0x05, 0x80, 0x03, 0x32, 0x30, 0x32};

char szSend[] =
"\x0a1\x014\x002\x001\x001\x002\x001\x00a\x030\x00c\x080\x003201\x062\x005\x
080\x003202";

But that gave me a compile error

error C2022: '12801' : too big for character
error C2022: '12802' : too big for character

Instead of "\x003201", write either "\x03\x32\x30\x31", or "\x03"
"201".
BTW most of your zeroes are superfluous, "\x0a1" is the same as "\xa1",
"\x005" is the same as "\x5", etc.
 
K

Keith Thompson

Old Wolf said:
Angus said:
unsigned char mybyte[] = {0xa1, 0x14, 0x02, 0x01, 0x01, 0x02, 0x01, 0x0a,
0x30, 0x0c, 0x80,
0x03, 0x32, 0x30, 0x31, 0x62, 0x05, 0x80, 0x03, 0x32, 0x30, 0x32};

char szSend[] =
"\x0a1\x014\x002\x001\x001\x002\x001\x00a\x030\x00c\x080\x003201\x062\x005\x
080\x003202";

But that gave me a compile error

error C2022: '12801' : too big for character
error C2022: '12802' : too big for character

Instead of "\x003201", write either "\x03\x32\x30\x31", or "\x03"
"201".
BTW most of your zeroes are superfluous, "\x0a1" is the same as "\xa1",
"\x005" is the same as "\x5", etc.

The thing to remember is that an octal escape sequence can contain at
most 3 digits, but a hexadecimal escape sequence can contain
arbitrarily many digits. Either can be terminated either by a
character that's not a valid (octal or hexadecimal, respectively)
digit, or by the end of the string literal. You can achieve the
latter using string literal concatenation:

"x03" "201"

Assuming 8-bit characters, it probably makes sense to use 2 digits in
each hexadecimal escape sequence, but syntactically you still need to
terminate each one somehow.
 
M

Mark L Pappin

Kenneth Brody said:
He is assuming that "\x003201" is the same as '\x003' '2' '0' '1',
but the compiler is obviously treating the entire "003201" as the
hex value

As it is supposed to do.

Octal constants in string literals and character constants are at most
3 digits, stopping at the first non-octal digit or after the third,
whichever comes first.

Hex constants in string literals and character constants can be
arbitrarily long strings of hex digits - the only thing that
terminates such a constant is a non-hex-digit, or the closing quote.

mlp
 
K

Kenneth Brody

Mark said:
As it is supposed to do.
True.

Octal constants in string literals and character constants are at most
3 digits, stopping at the first non-octal digit or after the third,
whichever comes first.

Hex constants in string literals and character constants can be
arbitrarily long strings of hex digits - the only thing that
terminates such a constant is a non-hex-digit, or the closing quote.

I think a closing quote qualifies as a "non-hex-digit". :)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
C

Chris Johnson

Keith said:
Old Wolf said:
Angus said:
unsigned char mybyte[] = {0xa1, 0x14, 0x02, 0x01, 0x01, 0x02, 0x01, 0x0a,
0x30, 0x0c, 0x80,
0x03, 0x32, 0x30, 0x31, 0x62, 0x05, 0x80, 0x03, 0x32, 0x30, 0x32};

char szSend[] =
"\x0a1\x014\x002\x001\x001\x002\x001\x00a\x030\x00c\x080\x003201\x062\x005\x
080\x003202";

But that gave me a compile error

error C2022: '12801' : too big for character
error C2022: '12802' : too big for character

Instead of "\x003201", write either "\x03\x32\x30\x31", or "\x03"
"201".
BTW most of your zeroes are superfluous, "\x0a1" is the same as "\xa1",
"\x005" is the same as "\x5", etc.

The thing to remember is that an octal escape sequence can contain at
most 3 digits, but a hexadecimal escape sequence can contain
arbitrarily many digits. Either can be terminated either by a
character that's not a valid (octal or hexadecimal, respectively)
digit, or by the end of the string literal. You can achieve the
latter using string literal concatenation:

"x03" "201"

Assuming 8-bit characters, it probably makes sense to use 2 digits in
each hexadecimal escape sequence, but syntactically you still need to
terminate each one somehow.

Out of curiosity, why does the standard allow for arbitrarily long hex
escapes in strings, when strings are arrays of characters? It would
make more sense, to me, to dictate that the compiler only read enough
to fill a character. Granted, workarounds are easy enough, but I don't
see a compelling reason to not behave intuitively.
 
K

Keith Thompson

Chris Johnson said:
Out of curiosity, why does the standard allow for arbitrarily long hex
escapes in strings, when strings are arrays of characters? It would
make more sense, to me, to dictate that the compiler only read enough
to fill a character. Granted, workarounds are easy enough, but I don't
see a compelling reason to not behave intuitively.

Because characters can be arbitrarily wide. If we only ever had to
worry about 8-bit characters, then it would make sense to restrict hex
escapes to two digits. But systems can have CHAR_BIT > 8, and wide
string literals such as L"\x1234\x5678" are also a real concern.
Having different syntax rules depending on the value of CHAR_BIT, or
for character string literals vs. wide string literals, would cause
too much confusion.

I think octal escapes are limited to three digits because they were
defined that way back in the days before it occurred to anyone that
you could have characters bigger than 9 bits. Hex escapes were added
later.
 

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

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,251
Latest member
41Ki

Latest Threads

Top