473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to write a BYTE string as a char*

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\x00 c\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?


Jan 10 '07 #1
13 7207
In article <eo*******************@news.demon.co.uk>,
Angus <no****@gmail.comwrote:
>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\x0 0c\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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jan 10 '07 #2
>
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?

Jan 10 '07 #3

Angus wrote:
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\x00 c\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
--
Lew

Jan 10 '07 #4

"Richard Tobin" <ri*****@cogsci.ed.ac.ukwrote in message
news:eo**********@pc-news.cogsci.ed.ac.uk...
In article <eo*******************@news.demon.co.uk>,
Angus <no****@gmail.comwrote:
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\x0 0c\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
>
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Jan 10 '07 #5

"Richard Tobin" <ri*****@cogsci.ed.ac.ukwrote in message
news:eo**********@pc-news.cogsci.ed.ac.uk...
In article <eo*******************@news.demon.co.uk>,
Angus <no****@gmail.comwrote:
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\x0 0c\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.
>
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Jan 10 '07 #6
In article <eo******************@news.demon.co.uk>,
Angus <no****@gmail.comwrote:
>Are those two supposed to match? In the first you have 03 32 30 31 and
in the second 003201.
>** 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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jan 10 '07 #7
Richard Tobin wrote:
>
In article <eo*******************@news.demon.co.uk>,
Angus <no****@gmail.comwrote:
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\x00 c\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:Th*************@gmail.com>

Jan 10 '07 #8
"Angus" <no****@gmail.comwrites:
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\x00 c\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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 10 '07 #9
Angus wrote:
>
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\x00 c\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.

Jan 11 '07 #10
"Old Wolf" <ol*****@inspire.net.nzwrites:
Angus wrote:
>>
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\x0 0c\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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 11 '07 #11
Mark L Pappin wrote:
>
Kenneth Brody <ke******@spamcop.netwrites:
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.
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:Th*************@gmail.com>

Jan 12 '07 #12

Keith Thompson wrote:
"Old Wolf" <ol*****@inspire.net.nzwrites:
Angus wrote:
>
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\x00 c\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.

Jan 13 '07 #13
"Chris Johnson" <ef******@gmail.comwrites:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 13 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
43
by: Vladimir | last post by:
Method UnicodeEncoding.GetMaxByteCount(charCount) returns charCount * 2. Method UTF8Encoding.GetMaxByteCount(charCount) returns charCount * 4. But why that? Look: /* Each Unicode character...
8
by: Sowen | last post by:
Hi, all I am wondering how to write bits by using ofstream? I have finished a huffman tree, but how can I write the bits to the file in order to gain compression? for example, 'A' returns a...
13
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
27
by: lovecreatesbea... | last post by:
This code snippet is an exercise on allocating two dimension array dynamically. Though this one is trivial, is it a correct one? Furthermore, when I tried to make these changes to the original...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.