how can I convert a char* to a byte array. I'm working on VC++ (with MFC). -
char* buffer_reponse="01EFF01000011DF";//hex
-
BYTE* byte_buffer=new BYTE[260];
-
Thanks for help
9 3443
This is off the cuff, but it should work. -
len = (1 + strlen(buffer_response))/2
-
if (260 < len)
-
throw "byte_buffer overrun"; // pick your Exception class here
-
-
char *p = buffer_response;
-
for (int ix = 0; ix < len; (ix++ p+=2))
-
{
-
unsigned int i;
-
sscanf(*p,"%2.2x",&i);
-
byte_buffer[ix] = i;
-
}
-
Luck!
Oralloy
thanks for help. but this instruction does not work
because I'm using VC++ ,Itried to change it but I don't understand it role.
Sorry, that's a stdio construct, and it's wrong. It should be - sscanf(p, "%2.2x", &i); // removed indirection on p
What it is, is a (string) "scanf" of the string p (p pointing to the next byte value to convert).
The resulting scanned hexadecimal value is placed into the integer i.
I did leave off error checking, so if the input is malformed, the error will be quietly ignored. Fixing this problem is an exercise for the student, so to speak.
The other option you have for conversion is to decode the characters of the string yourself. The code to do that is straightforward, but why reinvent it, unless you have some sort of performance requirement that obviates the standard libraries.
Good Luck,
Oralloy
tahnks again, but I don't need to do any input,in fact I have a function receiving a char* -
char* Serveur::ConstServerResponse1(char* buffer)
-
{ //convert buffer to byteArray
-
-
//treatment
-
.
-
.
-
.char* buf;
-
//convert byte array to buf
-
return buf;
-
-
ikbel borcheni,
It seems like we aren't communicating well. My appologies.
If I understand the requirements, they are simple: - Input buffer of type "char *"
- Input is terminated by a NUL character
- Output buffer of type "BYTE *"
- Output buffer limit 260 elements
- Convert input from hexadecimal string to byte values
- Every two characters from the input string form one output value
- Assign converted values to output buffer elements in order
- BYTE type can be assigned from an integer
- . . .
Ok, I'm done being testy. Please look up the sscanf function, I think it will explain what I tried to offer to you.
Once you understand the sscanf function, you'll see that my code example does no input, it simply operates on two buffers, which are assumed to exist, as above.
Cheers,
Oralloy
using
I get this exception:
invalid input format exception
so I tried
but It doesn't work also.
I'm using this reference for sscanf .
I was wrong in offering the format string [icode]"%2.2x"{/icode], which you corrected to "%2x"
I think that the problem is that the .NET code base you are using is complaining that the "x" conversion corresponds to an int type. I declared the variable i as an unsigned int , which is likely the source of your problem. Try changing the declaration to int , instead.
Luck!
Oralloy
can someone help me to decode the characters of a string in a byte array. I tried this code but I have a problem with the function "strcat" and "sscanf" -
char* str=new char[3];
-
int ix;
-
for (ix = 0; ix < longueur_byteArray; ix++ )
-
{
-
unsigned int i;
-
strcat_s(str,3,p);
-
strcat_s(str,3,p+1);
-
-
sscanf_s(str,"%#x",&i);
-
ByteArray[ix] = i;
-
p+=2;
-
}
-
-
Here is the conversion: - char * in = "F4";
-
BYTE out;
-
-
out = ((nybble(in[0]) << 4) | nybble(in[1]));
This is the converter: - unsigned nybble(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':in=in-'a'+'A';
-
case'A':case'B':case'C':case'D':case'E':case'F':return in-'A'+10;
-
}
-
throw "Unknown nybble value.";
-
}
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: Kueishiong Tu |
last post by:
How do I initialize a char array with null characters?
Is there an equivalent function to bzero() in VC++.net?
|
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: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |