Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8326937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:56:08+00:00 2026-06-09T00:56:08+00:00

How do i write the following function with cout? My main purpose is to

  • 0

How do i write the following function with cout? My main purpose is to actually print all the values to a file after i know how to use it with cout. std::hex does not work!

void print_hex(unsigned char *bs, unsigned int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
        printf("%02x", bs[i]);
        //Below does not work
        //std::cout << std::hex << bs[i];
    }

}

edit:

cout print out values such as : r9{èZ[¶ôÃ

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:56:10+00:00Added an answer on June 9, 2026 at 12:56 am

    I think adding a cast to int will do what you want:

    #include <iostream>
    #include <iomanip>
    
    void print_hex(unsigned char *bs, unsigned int n)
    {
        int i;
        for (i = 0; i < n; i++)
        {
            std::cout << std::hex << static_cast<int>(bs[i]);
        }
    
    }
    
    int main() {
      unsigned char bytes[] = {0,1,2,3,4,5};
      print_hex(bytes, sizeof bytes);
    }
    

    This is needed to force it to print as a number, not a character which is what you were seeing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the following script, how can I write a function that returns all of
If I write the following code: #include <iostream> using namespace std; int main() {
I keep getting the following error when trying to write a template function: main.cpp|17|error:
Is there a better way to write the following function? Having the '#' +
Hello i am trying to write the following function to display 7 days of
I want to write the following procedure / function: procedure ShowSysPopup(aFile: string; x, y:
The following script: cls function test_me([string]$testVar, $my_array) { Write-Host $testVar } $test_array = a,b
Can I write a hash code function for the following comparer logic? Two instances
I'm trying to write a function that does the following: takes an array of
The following is for Python 3.2.3. I would like to write a function that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.