473,406 Members | 2,956 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,406 software developers and data experts.

Conversion of datatypes (probably not what you think :p)

Hi all,

I have the following problem in C++:
I have a string with the following contents. I cannot change this because this is incoming data from a library (libpcap).
String t = "\x00\x81"; // this is the value I get from pcap

I want to convert this hexadecimal to it's decimal value (not as ascii value).
So eventually I should have:
int i = 129; // 192 is the value of 81 in hex (00 is just 0)

How can I do this?
I tried alot but always seem to get stuck :s

Help will be very appreciated!
May 7 '07 #1
3 1092
weaknessforcats
9,208 Expert Mod 8TB
1) Locate the x.
2) the next char is the left most byte. Save it in a char array.
3) the next char is the next byte. Save as the next byte in the char array.
4) continue saving until you reach the \
5) the number of saves the is hex base value (a power of 16) of the first array element and your number is in the array.

Go from there. Be sure to subtract 48 from the array char values to get the corresponding int value. Multiply by the the hex base value for that position and accumulate the result in an int.
May 7 '07 #2
How can I locate the x? the \x gives a sign that it is a hex value.
So \x00 is just 1 character :s
May 8 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
I'm sorry I didn't pick up on the \x.

This constant:

"\x00\x81"

uses the escape character \ followed by an x. There is no \x escape code. Results are indeterminate. A C++ string has a size() of 0 when initialized to this. printf(), also displays garbage.

What is the value you are getting exactly??

Usually hex values are preceded by 0x.
May 8 '07 #4

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

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
5
by: Amit | last post by:
I am facing a problem while using SQL Server with VB application. Implicit conversion from datatype text to nvarchar is not allowed. Use the convert function to run this query. When i see the...
22
by: kalio80 | last post by:
Hi everyone I am trying to create a file that converts text files from unix to windows and windows to unix I understand the general concept of it as unix uses line feed LF Windows uses CRLF...
47
by: rawCoder | last post by:
Hi, Just wanted to know if there is any speed difference between VB conversion Keywords like CInt, Clng, CStr, CDbl, CBool etc. ..NETs Convert.To<...> methods. And which is better to be...
2
by: Mark Gibson | last post by:
Hello, I've been experimenting with dblink recently, and have encountered some limitations I'd like to discuss. I've been trying to create views of remote tables, like so: CREATE VIEW stuff...
4
by: Cott Lang | last post by:
ERROR: could not convert UTF-8 character 0x00ef to ISO8859-1 Running 7.4.5, I frequently get this error, and ONLY on this particular character despite seeing quite a bit of 8 bit. I don't really...
31
by: Martin Jørgensen | last post by:
Hi, I've had a introductory C++ course in the spring and haven't programmed in C++ for a couple of months now (but I have been programmed in C since january). So I decided to do my conversion...
0
by: egbert.beuker | last post by:
Hi, I encountered a conversion problem in my .net web app (c#), and I hope someone can help me: I'm working on a generic way to store data in a database with a few generated classes. I want...
9
by: arunmib | last post by:
hi all, How can I convert an Integer (int data type) and an unsigned char data type to a quadword? I want to know the conversion mechanism.... Advance thanks for the help.
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.