473,770 Members | 4,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting Integers from hex Data stored in a char array

2 New Member
Hi,

Part of my code receives data in a char array, with pairs of characters representing a hex value - e.g. Array = "1234abcd" really represents 0x12, 0x34, 0xab, 0xcd.

I need to convert the pairs of characters into ints, is there a nice elegant way to do this? I have currently implemented a switch solution (below), but it seems a bit messy.

Expand|Select|Wrap|Line Numbers
  1. int convertChar(char tChar){
  2.   switch(tChar){
  3.     case 'A':
  4.     case 'a':
  5.       return 10;
  6.     break;
  7.     case 'B':
  8.     case 'b':
  9.       return 11;
  10.     break
  11.  ......
  12.     default:
  13.       return (int)(tChar - '0');
  14.     break;
  15.   }
  16. }
  17.  
  18. int main(){
  19.   char msg[64] = "1234abcd..........etc........
  20.   int i=0;
  21.   int value = 0
  22.  
  23.   for(i=0; i<32; i++)
  24.     value = (convertChar(msg[ 2 * i ]) * 16) + convertChar[msg[2 * i + 1];
  25.  
  26.     cout << value;
  27.  
  28.   }
  29.   return 0;
  30. }
Apr 25 '07 #1
2 1848
Banfa
9,065 Recognized Expert Moderator Expert
I need to convert the pairs of characters into ints, is there a nice elegant way to do this? I have currently implemented a switch solution (below), but it seems a bit messy.
It may seem messy but it is in fact the normal way of doing this sort of thing.
Apr 25 '07 #2
Bill Maclennan
2 New Member
It may seem messy but it is in fact the normal way of doing this sort of thing.
Really? Ok that makes me feel better then, thanks.

Bill
Apr 25 '07 #3

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

Similar topics

13
2540
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of these, and am interested to know whether the storage efficiency of long integers is in danger of breaking my code if I use too many. Would I do better to write a class that defines bitwise operations on arrays of integers, each integer being assumed...
87
3366
by: j0mbolar | last post by:
I've read in the standard that addresses basically can't be interpreted as integers. If they can, it is implementation defined behavior. However, if they can't be viewed as integers in any sense as far as portability goes, what then, should one think of addresses being composed of?
35
2547
by: Durgesh Sharma | last post by:
I want to use strrchar(source_string,last_char ) function from string.h header file,to find out the last occurrence of the NON SPACE Alphanumeric Character. Then i will put a NULL CHAR after incrementing that position,received by strrchar() function by 1. This is my idea of Trimming a string from right . According to the definition of the strrchar function,i am supposed to
10
4788
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data structures are read only) Ideally one should be able to put the redundant information there automatically so no mistakes are possible, but in a lot of case I see no way how to do it.
4
3152
by: jt | last post by:
I'm getting a compiler error: warning C4172: returning address of local variable or temporary Here is the function that I have giving this error: I'm returning a temporary char string and its not liking it. How can I fix this? char *dequeue(struct node **first) { char temp;
1
2013
by: matthias.kessler | last post by:
I've got an issue understanding where my compiler places data. I'm developing for an embedded target, so using as little RAM as possible is very important. Anonymous strings are stored in ROM and I was looking for a way of doing the same for anonymous arrays. Using the gcc on a windows machine gave the results I expected, both fpData of Foo and Bar are stored in the same location. When I switched to the target compiler however, the...
13
10452
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another set of integers stored in an array.It would be a great help if you could provide some code for it.I tried the function fscanf but by that I am able to read only the first integer of the text file.Please help me.
4
8529
emaghero
by: emaghero | last post by:
Greetings all, I have a small amount of data that I want to read from a CSV file. The data is stored in an array of size rows*cols, the array is zero-based. I open the file using an fstream object ifstream data;
1
4676
by: susheela s | last post by:
Hi, Im doing a multiplication of two large integers which are store in arrays(such as 2345 is in array as 2 in a, 3 in a and so on)like this im storing integers in two arrays.The logic i have used is explained with code below, class large{ int no; //array is used to store the large number. Each digit of a the number is stored as an element.. // 123 is stored as no={1,2,3} public: large operator *(large& x){ // overload...
0
9439
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.