TomCrow
TomCrow

Reputation: 102

Hex *char from int number

I asked here how to convert 4 chars to int number here: stackoverflow link

It is working but now I need to do the same but in reverse. I want to make 4 chars from one int. Because I dont understand completly bit shifting and using OR and AND operators in practise so I tried to solve my problem with stupid solutions like converting first the int to Hex String then to char, etc. Could anybody help me here with good solve please? Sorry for my english.

Upvotes: 1

Views: 2162

Answers (5)

Milan
Milan

Reputation: 141

This should do the job:

const char* chars = (const char*)&number;

And now you can access the chars as they are in the array:

chars[0], chars[1], chars[2], chars[3]

Same you can actually do in reverse:

char chars[4];
// write each char in to array
int *pnumber = (int*)chars;
// now you can read the number: 
int number = (*pnumber);

Order here is important and it depends on endiannes of your system.

Upvotes: 0

eerorika
eerorika

Reputation: 238341

I need to do the same but in reverse. I want to make 4 chars from one int.

Just do the operations in reverse. Shift to right instead of left and mask after shift instead of before:

char bytes[4];
long number = 0x00000190L; // int may be only 2 bytes on some implementations
for(int i = 0; i < 4; i++) {
    bytes[i] = (number >> (8 * i)) & 0xFFL;
}

If your intention is to get a hex string instead (like the title suggests, but which is not the reverse of the linked question), then the simplest thing to do is to use std::hex and a stream.

Upvotes: 1

iedoc
iedoc

Reputation: 2324

use bit shifting:

#include <string>
#include <iostream>


void main(void* args)
{
  // 4 byte integer
  unsigned int charInteger = 0;

  // 4 characters
  unsigned char c1 = 'a';
  unsigned char c2 = 'b';
  unsigned char c3 = 'c';
  unsigned char c4 = 'd';

  // put each character above into each byte of a 4 byte integer
  charInteger = (c1 & 0xFF)
       | ((c2 & 0xFF) << 8) 
       | ((c3 & 0xFF) << 16)
       | ((c4 & 0xFF) << 24);

  std::cout << "charInteger = " << charInteger << "\n";

  // get each byte of the integer
  unsigned int i1 = charInteger & 0xff; // first byte
  unsigned int i2 = (charInteger >> 8) & 0xff; // second byte
  unsigned int i3 = (charInteger >> 16) & 0xff; // third byte
  unsigned int i4 = (charInteger >> 24) & 0xff; // fourth byte

  // display each byte of charInteger as characters
  std::cout << char(i1) << "\n";
  std::cout << char(i2) << "\n";
  std::cout << char(i3) << "\n";
  std::cout << char(i4) << "\n";

  std::string h;
  std::cin >> h;
}

Upvotes: 1

rdavb
rdavb

Reputation: 64

Here could be an acceptable solution, manipulating pointers. An excellent tutorial about pointers can be found here

int main()
    {
      int test32 = 0 ;
      int* ptest32 = &test32;

      char* p1st8bits = (char*) ptest32;
      char* p2nd8bits = p1st8bits + 8;
      char* p3nd8bits = p1st8bits + 16;
      char* p4nd8bits = p1st8bits + 24;

      std::cout  <<"first 8 bits: " << *p1st8bits << "\n" ;
      std::cout  <<"second 8 bits: " << *p2nd8bits << "\n" ; 
      std::cout  <<"third 8 bits: " << *p3nd8bits << "\n" ;
      std::cout  <<"fourth 8 bits: " << *p4nd8bits << "\n" ;
    }

Upvotes: 2

Thomas Matthews
Thomas Matthews

Reputation: 57688

Try this:

const unsigned int value = 0x12345678;
std::ostringstream hex_stream;
hex_stream << hex << value;

The above fragment will convert the value to a hex string. The following snippet will print the characters one per line.

std::string chars_as_hex = hex_stream.str();
for (i = 0; i < chars_as_hex.length(); ++i)
{
  std::cout << chars_as_hex[i] << std::endl;
}

Upvotes: 1

Related Questions