I need to convert array of char to hex one char at a time ,
I tried to use sprintf,but it did not work.

?

Dani AI

Generated

This thread actually contains two separate, commonly confused problems: converting bytes (chars) to a hex string, and counting token/string lengths across multiple input files. ’s stream-based answer shows that hex output is possible with iostreams; the two gaps that typically make sprintf “not work” are (1) signedness/promotion of char and (2) format-specifier portability. The other frequent bug — “everything works for the first file only” — is almost always caused by reusing a stream or not resetting counters between files.

A robust byte→hex pattern (safe zero-padded two-digit ASCII) is to treat each element as an unsigned byte and format it into a 2-char buffer:

unsigned char b = static_cast<unsigned char>(buf[i]);
char hex[3];
snprintf(hex, sizeof hex, "%02X", static_cast<unsigned int>(b));
// hex now contains "00" .. "FF"

A C++ alternative that keeps everything in std::string form uses an ostringstream with explicit width/fill:

#include <sstream>
#include <iomanip>

std::ostringstream os;
os << std::uppercase << std::hex << std::setw(2) << std::setfill('0')
   << static_cast<int>(b);
std::string hex = os.str();

For the multi-file length-counting issue, common corrective measures:

  • Allocate/reset the counts array/vector inside the per-file loop (don’t reuse without clearing).
  • Open a fresh ifstream for each filename (or call in.clear(); in.seekg(0); before rereading).
  • Prefer tokenized reads while (in >> token) over while (!in.eof()).
  • Strip trailing '\r' (Windows CR) from tokens if files come from different platforms.
  • If input may be UTF‑8, remember std::string::length() counts bytes, not characters.

Minimal per-file pattern:

for (auto &name : files) {
  std::ifstream in(name);
  std::vector<int> counts(51,0); // index 50 = "50+"
  std::string token;
  while (in >> token) {
    if (!token.empty() && token.back() == '\r') token.pop_back();
    size_t len = token.length();
    ++counts[len > 50 ? 50 : len];
  }
  // write counts out
}

Checklist summary: cast chars to unsigned before formatting; use %02X or stream manipulators for two-digit hex; reset counters and stream state between files; strip CR; avoid eof()-based loops. These fixes address the most common failures reported in this thread.

Recommended Answers

All 3 Replies

Are you trying to display the character's value as a hexadecimal?

#include <iostream>

int main()
{
  unsigned char myc = 'o';
  
  std::cout << (int)myc << std::endl;
  std::cout << std::hex << (int)myc << std::endl;
  std::cout << std::oct << (int)myc << std::endl;
  
  return 0;
}

actully no , I put the cout<< to test the code , I need to calculate the number of string in all files that is of length 1 , 2,......,50,then write the result to another file


the problem is that every thing is working 100% for the first file only

Sorry I posted tow thread togother I thought you are talking about the other one, anyway thank you for your answer

Be a part of the DaniWeb community

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