Hi, how can I convert a char* to a byte array. I tried this -
unsigned Serveur::CharToByte(char in )
-
{
-
switch(in){
-
case'0': case'1': case'2': case'3': case'4': case'5': case'6':case'7': case'8': case'9': return (in-'0');
-
case'A':case'B': case'C':case'D': case'E':case'F':return(in-'A'+10);
-
-
}
-
}
-
-
I used the inverse function byteToChar to display the result : -
void Serveur::BytesToChar(char* destination, BYTE* source, int longueur)
-
{
-
char chiffres[]="0123456789ABCDEF";
-
int i;
-
for(i=0; i<longueur; i++)
-
{
-
destination[i*2]=chiffres[source[i] >> 4];
-
destination[i*2+1]=chiffres[source[i]& 0x0F];
-
}
-
destination[i*2]=0;
-
-
}
-
if I try the execution with the string
the displayed result is:
thanhs for help
6 3737
What is type BYTE in BytesToChar?
Why does CharToByte return unsigned instead of BYTE or int?
The index into chiffres must be between 0 and 15. Line 8 honors that constraint, line 7 does not.
BYTE is an unsigned char
the function CharToByte returns unsigned because a byte contains normally 2 char: -
while(i<len_buffer)
-
{
-
x=CharToByte(buffer[i]);
-
y=CharToByte(buffer[i+1]);
-
byte_buffer[j]=((x<<4) | y);
-
j++;
-
i++;
-
}
-
for the function BytesToChar, I'm sure that it works well because I tested it seperately before using it here
You're not quite providing enough information.
In your second post,
What are the types of buffer, x, y, and byte_buffer?
(I assume i and j are both ints.)
In your second post, shouldn't you be incrementing i by 2?
Banfa 9,065
Expert Mod 8TB
This line of code - case'A':case'B': case'C':case'D': case'E':case'F':return(in-'A'+10);
is not truly portable because you are reliant on the execution character set to ensure that 'A', 'B', 'C', 'D', 'E' and 'F' are contiguous and not the C standard and those letters are not contiguous in all execution character sets although there are in one of the most common ones (ASCII).
For example, the snippet Banfa points at would not work if the underlying character representation were EBCDIC. EBCDIC is not as common as ASCII, but it still crops up from time to time.
I found the problem, as donbock said I have to increment i by 2 not by 1. Thanks for all
Sign in to post your reply or Sign up for a free account.
Similar topics
by: mailar |
last post by:
HI,
Can anyone tell me how is a multi_byte to single byte and vice versa
conversion done in DB2.
It would be great even if someone can tell me how Oracle does it?
Oracle already has functions...
|
by: Selvin |
last post by:
Hi all,
1. if I use BinaryFormatter(for the struct below) I get:
ÿÿÿÿ IMsgProtocol, Version=1.0.1291.23445, Culture=neutral,
PublicKeyToken=null msgProtocol.Login ...
|
by: Ian |
last post by:
I am creating an XML file through the XmlTextWriter. This is output to a
MemoryStream which I convert a string through a Byte Array. Everything works
correctly except for one BIG issue. My XML...
|
by: Igor Natachanny |
last post by:
Wich way I can to take the BYTE*-pointer to a string of characters that
contain
into CString-object?
|
by: Richard506 |
last post by:
If you take this Byte Arra
Dim bytes() As Byte = {
207, 224, 135, 161, 253, 233, 111, 110, 99, 111, 100, 105, 110, 103,
32, 69, 120, 97, 109, 112, 108, 101
and write the byte array to disk...
|
by: Tim Wagaman |
last post by:
The project that I am working on currently, is a server application that
communicates across TCP IP. In this application, I recieve a request in
a TCP packet with a 4byte header. This 4 byte header...
|
by: Alberto Cardoso |
last post by:
Is there a direct way to convert a short array to a byte array?
I dont to use a for and cast every short to a byte.
I want something like the BitConverter class that accpets a short array as...
|
by: Lew Barnesson |
last post by:
Hi,
I have a byte array read from a file using Stream IO, and containing a
mixture of 16-bit integers, strings, etc..
The problem: what is the best way to convert two byte sequences into...
|
by: =?Utf-8?B?QmFydE1hbg==?= |
last post by:
Greetings,
I am trying to copy an unmanaged buffer into a mananged buffer, and trying
to use marshalling, and I am not having any success because it doesn't seem
to complie.
Basically I am...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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,...
| |