0

I have a string and I want to print hex value of each parts ascii code. for example if the string is "0200" the output will be 30323030 .

and here's my code:

string bit_pattern;
bit_pattern = "5678008180000000";
cout << hex << bit_pattern;

but it prints 5678008180000000 instead of 35363738303038313830303030303030 how do i fix it???

2
  • 2
    For each char in the string, convert to unsigned int and sent to a hex - manipulated cout. Commented Feb 7, 2014 at 8:25
  • I'm sure you can cast it or convert it to hashCode(). There is a connection between hashcode and ascii Commented Feb 7, 2014 at 8:27

2 Answers 2

3

You can use the following

for (int i=0; i<bit_pattern.length(); i++)
    cout << hex << (int)bit_pattern[i];

to print the ascii value (in hex format) char by char.

Sign up to request clarification or add additional context in comments.

Comments

1

You're just sending the same std::string right to std::cout. Just sending the hex manipulator isn't going to magically convert all those chars.

I admit this is complete overkill, but I was bored:

#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>

class ascicodes
{
    std::ostringstream ss;

public:
    friend std::ostream& operator <<(std::ostream& os, const ascicodes& obj)
    {
        os << obj.ss.str();
        return os;
    }

    ascicodes(const std::string& s)
    {
        ss << std::hex << std::setfill('0');
        std::for_each(s.begin(), s.end(),
            [this](char ch)
            {
                ss << std::setw(2) << static_cast<unsigned int>(ch);
            });
    }
};


int main()
{
    std::string bit_pattern = "5678008180000000";
    std::cout << ascicodes(bit_pattern) << std::endl;
    std::cout << ascicodes("A completely different string") << std::endl;
    return 0;
}

Output

35363738303038313830303030303030
4120636f6d706c6574656c7920646966666572656e7420737472696e67

6 Comments

@DieterLücking yeah I forgot it i the class actually. i intended it there. you think she's better in main and just go with decimal in the class ? It has to be prior-to-value-insertion, only reason I mention it.
And how to convince a person that this is better than short sample from herohuyongtao using old C-style conversion but still producing the correct result ?
@SChepurin I didn't know I had to convince anyone it was "better"; only that it was one way to do it, particularly if you need to do it frequently and in tons of code with preexisting stream outputs. Also, his sample doesn't handle high-ascii. the sign extension will not be pleasant for chars above 127.
@WhozCraig - I think, this can be counted as "convincing enough".
@DieterLücking is std::hex sticky? I can never remember which is and is not (besides setw, which is not (if I remember right). They two difference streams so it still has to be in the class, but was just curious.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.