Hi,

Thanks

## All 12 Replies

...in what range?

...in what range?

base of 16

In what range?
What is the minimum value?
What is the maximum value?

Please provide some new information to justify a second thread on the topic.

tell me this... is the following code correct?

``````#include <iostream>
using namespace std;

int main () {
int n;
n=48:45:4c:4c:4f:20:57:4f:52:4c:44;
cout << "ASC " << n << endl
return 0;
}``````

tell me this... is the following code correct?

``````#include <iostream>
using namespace std;

int main () {
int n;
n=48:45:4c:4c:4f:20:57:4f:52:4c:44;
cout << "ASC " << n << endl
return 0;
}``````

Is this a trick question? Of course it's not correct, as any compiler will tell you. How about explaining exactly what output you were expecting from this code, and I'll tell you how to achieve it correctly.

Is this a trick question? Of course it's not correct, as any compiler will tell you. How about explaining exactly what output you were expecting from this code, and I'll tell you how to achieve it correctly.

I am getting a hex values as my output and i want to convert it into ascii so that it is readable.. does this help you?

First off, there's no such thing as a 'hex value'; there are binary values, some of which represent numeric values and some of which represent character encodings. I presume what you want is to display a binary numeric value in a character string as a hexadecimal number.

Let's start with the numeric value itself. given the size of the number you seem to have, and the way you've shown it in you earlier attempt to explicate your intentions, I assume that what you're working with is a 128-bit value (your value used 11 bytes, which means that it won't fit in anything less than 88 bits). Unfortunately, there are no standard C++ variable types that large. You'll need at least two `long long` variables in a structure or an array, assuming your compiler supports that type; otherwise, you need four 32-bit `int` variables to hold it.

Just out of curiosity, could you tell us what this is supposed to represent? My initial impression was that it was an IPv6 address, but if so, you wrote it incorrectly (IPv6 addresses are written with sections of two bytes, with leading zeroes elided, so it would have been 4845:4c4c:4f20:574f:524c:44:: or something similar - because there are less than 16 bytes, and you only gave the actual hex values, it's hard to say where the zeroes would go).

Anyway... to print out a numeric value in hex, you simply use the `hex` manipulator with the output stream before the number to be printed:

``cout << hex << n << n << n << n << endl;``

Note that the order which you need to print it is system-dependent.

Actually, what I think you want will be individual ASCII character values.
If you split up that (string?) data you posted into individual characters (even in HEX), you won't need to do any real conversion -- just output them as chars.

``````#include <iostream>
using namespace std;

int main(void)
{
char arr_chr[] = {0x48, 0x45, 0x4c, 0x4c, 0x4f, 0x20, 0x57, 0x4f, 0x52, 0x4c, 0x44, 0x00};

for (int i=0; 0 != arr_chr[i]; i++)
{
//print individual characters here;
}

return 0;
}``````
commented: I should have noticed that before. The string in question is "Hello, World". +8

I am getting a hex values as my output and i want to convert it into ascii so that it is readable.. does this help you?

No, it doesn't. Your terminology is confused, which makes it very difficult to understand exactly what you want without specific examples. Please post the exact output you want to see, since you're clearly not describing the problem adequately.

If that input data is actually a STRING, then you would need to parse it (based either on colons or spaces if you convert the colons to spaces).

There might be an easier way, but here is a conversion of the STRING with the colons converted to spaces:

``````/*
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
*/
///////////////////////////////////////////////////////////////////////
// Use the istringstream to parse the data (now containing spaces)
string strHex = "0123456789ABCDEF";  //base characters for hex conversion
string strData="";
istringstream iss("48 45 4c 4c 4f 20 57 4f 52 4c 44");
while(iss >> strData)
{
////////////////////////////////////////////////////////////////////
// First byte's offset times 16 added to second byte's offset
cout << (char)
((strHex.find(toupper(strData))*16) +
(strHex.find(toupper(strData))));
}``````

Another way to say what Narue said is: programming is an exercise in precision. It's nearly impossible to program something until you can think about it (and thus speak about it) precisely. And once you can think about the problem precisely, the code virtually writes itself. Learning to program is, first and foremost, teaching yourself to think precisely. Start there, and work forwards: what is your input (integer or string of colon-separated hex characters) and what output do you expect to get as a result of your program? Is there some intermediate format you need, between the input and output? What format is that, what will it be used for, and above all, is it needed at all?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.