473,387 Members | 1,530 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,387 software developers and data experts.

How to convert char* byte[ ] to array?

Hi, how can I convert a char* to a byte array. I tried this
Expand|Select|Wrap|Line Numbers
  1. unsigned Serveur::CharToByte(char in )
  2.  {
  3.    switch(in){
  4.   case'0': case'1': case'2': case'3': case'4': case'5':    case'6':case'7': case'8': case'9': return (in-'0');
  5.   case'A':case'B': case'C':case'D': case'E':case'F':return(in-'A'+10);
  6.  
  7.   }
  8.  }
  9.  
  10.  
I used the inverse function byteToChar to display the result :
Expand|Select|Wrap|Line Numbers
  1. void Serveur::BytesToChar(char* destination, BYTE* source, int longueur)
  2. {
  3.     char chiffres[]="0123456789ABCDEF";
  4.     int i;
  5.     for(i=0; i<longueur; i++)
  6.     {
  7.         destination[i*2]=chiffres[source[i] >> 4];
  8.         destination[i*2+1]=chiffres[source[i]& 0x0F];
  9.     }
  10.     destination[i*2]=0;
  11.  
  12. }
  13.  
if I try the execution with the string
Expand|Select|Wrap|Line Numbers
  1. 000100000006FF01007B000C
  2.  
the displayed result is:
Expand|Select|Wrap|Line Numbers
  1. 00000110000000000000066F
  2.  
thanhs for help
Mar 10 '11 #1
6 3737
donbock
2,426 Expert 2GB
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.
Mar 10 '11 #2
BYTE is an unsigned char
the function CharToByte returns unsigned because a byte contains normally 2 char:
Expand|Select|Wrap|Line Numbers
  1. while(i<len_buffer)
  2.     {
  3.       x=CharToByte(buffer[i]);
  4.       y=CharToByte(buffer[i+1]);
  5.       byte_buffer[j]=((x<<4) | y);
  6.       j++;
  7.       i++;
  8.     }
  9.  
for the function BytesToChar, I'm sure that it works well because I tested it seperately before using it here
Mar 10 '11 #3
donbock
2,426 Expert 2GB
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?
Mar 11 '11 #4
Banfa
9,065 Expert Mod 8TB
This line of code

Expand|Select|Wrap|Line Numbers
  1.   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).
Mar 11 '11 #5
donbock
2,426 Expert 2GB
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.
Mar 11 '11 #6
I found the problem, as donbock said I have to increment i by 2 not by 1. Thanks for all
Mar 11 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
0
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 ...
5
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...
5
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?
4
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...
1
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...
9
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...
4
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
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...
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,...

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.