dec to hex conversions.....more 
Author Message
 dec to hex conversions.....more

hello all,

I have a function which i needed...to convert a value from dec to
hexadecimal....and sent via a packet array into a particular equipment
for remote serial comms control....

here's the function:

unsigned char CASM::dec2hex(int i_Value)
{

int i=0;
unsigned char buffer[8];

while(i_Value > 0){
buffer[i++] = i_Value%16;
i_Value = i_Value/16;

Quote:
}

for (int j=j-1; j>=0; j--)
{
if(buffer[j]==10)
buffer[j] = 'A';
else if(buffer[j]==11)
buffer[j] = 'B';
else if(buffer[j]==12)
buffer[j] = 'C';
else if(buffer[j]==13)
buffer[j] = 'D';
else if(buffer[j]==14)
buffer[j] = 'E';
else if(buffer[j]==15)
buffer[j] = 'F';
else
buffer[j] = buffer[j];
Quote:
}

return NULL; //*buffer; << ARGH!?

Quote:
}

there're a few issues here....

1. I cannot put this into a separate function and call it when i
needed to cos it's not returning the proper array.

2. the output is not what i wanted:
if the value to be converted is 75000000, i am getting this values in
return:
00 04 07 08 06 08 0c 00

we know that:
75000000 = 0478 68C0H

in my equipment i need to send it as
Packet[1] = 0x04;
Packet[2] = 0x78;
Packet[3] = 0x68;
Packet[4] = 0xC0;

but the conversion is sending it as
Packet[1] = 0x00;
Packet[2] = 0x04;
Packet[3] = 0x07;
Packet[4] = 0x08;

Is there a easier way to do this conversions? I will also have to
convert the return hex values to binary to read them off for data
later in my development....



Mon, 13 Dec 2004 11:37:11 GMT  
 dec to hex conversions.....more
These code bellow will work.

int i = htonl(75000000);
unsigned char* buffer = (unsigned char*) &i;

you have;

buffer[0] = 0x04;
buffer[1] = 0x78;
buffer[2] = 0x68;
buffer[3] = 0xC0;

Quote:

> hello all,

> I have a function which i needed...to convert a value from dec to
> hexadecimal....and sent via a packet array into a particular equipment
> for remote serial comms control....

> here's the function:

> unsigned char CASM::dec2hex(int i_Value)
> {

> int i=0;
> unsigned char buffer[8];

> while(i_Value > 0){
> buffer[i++] = i_Value%16;
> i_Value = i_Value/16;
> }

> for (int j=j-1; j>=0; j--)
> {
> if(buffer[j]==10)
> buffer[j] = 'A';
> else if(buffer[j]==11)
> buffer[j] = 'B';
> else if(buffer[j]==12)
> buffer[j] = 'C';
> else if(buffer[j]==13)
> buffer[j] = 'D';
> else if(buffer[j]==14)
> buffer[j] = 'E';
> else if(buffer[j]==15)
> buffer[j] = 'F';
> else
> buffer[j] = buffer[j];
> }
> return NULL; //*buffer; << ARGH!?
> }

> there're a few issues here....

> 1. I cannot put this into a separate function and call it when i
> needed to cos it's not returning the proper array.

> 2. the output is not what i wanted:
> if the value to be converted is 75000000, i am getting this values in
> return:
> 00 04 07 08 06 08 0c 00

> we know that:
> 75000000 = 0478 68C0H

> in my equipment i need to send it as
> Packet[1] = 0x04;
> Packet[2] = 0x78;
> Packet[3] = 0x68;
> Packet[4] = 0xC0;

> but the conversion is sending it as
> Packet[1] = 0x00;
> Packet[2] = 0x04;
> Packet[3] = 0x07;
> Packet[4] = 0x08;

> Is there a easier way to do this conversions? I will also have to
> convert the return hex values to binary to read them off for data
> later in my development....



Mon, 13 Dec 2004 15:38:54 GMT  
 dec to hex conversions.....more
Hello,

  Sorry, but I think this is not the proper group for this question.

  You should think that radix is only for representing a number! an
int is not decimal or hexadecimal (although it is handled in binary
format internally). I deduce you only need to break i_Value in bytes
(be careful with byte-order endianism!).

  Try this:

  union _int2bytes
  {
    unsigned long integer;
    unsigned char bytes[];
  } int2bytes;

  int2bytes.integer = 75000000L;

  if you have a little-endian machine, int2bytes.bytes contains:
  0xC0, 0x68, 0x78, 0x04. If you want to serialize a integer in
network-order (big-endian) the you need iterate the array in reverse
order (or swap the bytes of the integer before). This is even more
compact with the macro:

  #define GET_BYTE(val, i) ((unsigned char*)(val))[i]

  so, GET_BYTE(75000000L, 0) returns 0xC0 and so on... This macro
works with short and long integers (and long long or __int64!). You
can add a check (with assert((i) >= 0 && (i) < sizeof(val)) for the
byte index.

  Another issue is the bit-order, (if the first bit is the most
significative or the least...)

  I hope this can help,

  Jordi Vilar

Quote:

> hello all,

> I have a function which i needed...to convert a value from dec to
> hexadecimal....and sent via a packet array into a particular equipment
> for remote serial comms control....

> here's the function:

> unsigned char CASM::dec2hex(int i_Value)
> {

> int i=0;
> unsigned char buffer[8];

> while(i_Value > 0){
> buffer[i++] = i_Value%16;
> i_Value = i_Value/16;
> }

> for (int j=j-1; j>=0; j--)
> {
> if(buffer[j]==10)
> buffer[j] = 'A';
> else if(buffer[j]==11)
> buffer[j] = 'B';
> else if(buffer[j]==12)
> buffer[j] = 'C';
> else if(buffer[j]==13)
> buffer[j] = 'D';
> else if(buffer[j]==14)
> buffer[j] = 'E';
> else if(buffer[j]==15)
> buffer[j] = 'F';
> else
> buffer[j] = buffer[j];
> }
>  return NULL; //*buffer; << ARGH!?
> }

> there're a few issues here....

> 1. I cannot put this into a separate function and call it when i
> needed to cos it's not returning the proper array.

> 2. the output is not what i wanted:
> if the value to be converted is 75000000, i am getting this values in
> return:
> 00 04 07 08 06 08 0c 00

> we know that:
> 75000000 = 0478 68C0H

> in my equipment i need to send it as
> Packet[1] = 0x04;
> Packet[2] = 0x78;
> Packet[3] = 0x68;
> Packet[4] = 0xC0;

> but the conversion is sending it as
> Packet[1] = 0x00;
> Packet[2] = 0x04;
> Packet[3] = 0x07;
> Packet[4] = 0x08;

> Is there a easier way to do this conversions? I will also have to
> convert the return hex values to binary to read them off for data
> later in my development....



Mon, 13 Dec 2004 18:27:15 GMT  
 dec to hex conversions.....more
Hello,

  Sorry, but I think this is not the proper group for this question.

  You should think that radix is only for representing a number! an
int is not decimal or hexadecimal (although it is handled in binary
format internally). I deduce you only need to break i_Value in bytes
(be careful with byte-order endianism!).

  Try this:

  union _int2bytes
  {
    unsigned long integer;
    unsigned char bytes[];
  } int2bytes;

  int2bytes.integer = 75000000L;

  if you have a little-endian machine, int2bytes.bytes contains:
  0xC0, 0x68, 0x78, 0x04. If you want to serialize a integer in
network-order (big-endian) the you need iterate the array in reverse
order (or swap the bytes of the integer before). This is even more
compact with the macro:

  #define GET_BYTE(val, i) ((unsigned char*)(val))[i]

  so, GET_BYTE(75000000L, 0) returns 0xC0 and so on... This macro
works with short and long integers (and long long or __int64!). You
can add a check (with assert((i) >= 0 && (i) < sizeof(val)) for the
byte index.

  Another issue is the bit-order, (if the first bit is the most
significative or the least...)

  I hope this can help,

  Jordi Vilar

Quote:

> hello all,

> I have a function which i needed...to convert a value from dec to
> hexadecimal....and sent via a packet array into a particular equipment
> for remote serial comms control....

> here's the function:

> unsigned char CASM::dec2hex(int i_Value)
> {

> int i=0;
> unsigned char buffer[8];

> while(i_Value > 0){
> buffer[i++] = i_Value%16;
> i_Value = i_Value/16;
> }

> for (int j=j-1; j>=0; j--)
> {
> if(buffer[j]==10)
> buffer[j] = 'A';
> else if(buffer[j]==11)
> buffer[j] = 'B';
> else if(buffer[j]==12)
> buffer[j] = 'C';
> else if(buffer[j]==13)
> buffer[j] = 'D';
> else if(buffer[j]==14)
> buffer[j] = 'E';
> else if(buffer[j]==15)
> buffer[j] = 'F';
> else
> buffer[j] = buffer[j];
> }
>  return NULL; //*buffer; << ARGH!?
> }

> there're a few issues here....

> 1. I cannot put this into a separate function and call it when i
> needed to cos it's not returning the proper array.

> 2. the output is not what i wanted:
> if the value to be converted is 75000000, i am getting this values in
> return:
> 00 04 07 08 06 08 0c 00

> we know that:
> 75000000 = 0478 68C0H

> in my equipment i need to send it as
> Packet[1] = 0x04;
> Packet[2] = 0x78;
> Packet[3] = 0x68;
> Packet[4] = 0xC0;

> but the conversion is sending it as
> Packet[1] = 0x00;
> Packet[2] = 0x04;
> Packet[3] = 0x07;
> Packet[4] = 0x08;

> Is there a easier way to do this conversions? I will also have to
> convert the return hex values to binary to read them off for data
> later in my development....



Mon, 13 Dec 2004 18:29:06 GMT  
 dec to hex conversions.....more
Assuming that you are on Win32 (i.e. an int is 4 bytes....)
{
    unsigned char buf[4];

    Dec2Hex( buf, 75000000 );

Quote:
}

  void DEc2Hex( void *da, long Dec)
  {
   unsigned char *DestinationAddr = (unsigned char *)da;
   *(DestinationAddr)   = (unsigned char)((long)(Dec) >> 24);
   *(DestinationAddr  + 1) = (unsigned char)((long)(Dec) >> 16);
   *(DestinationAddr  + 2) = (unsigned char)((long)(Dec) >> 8);
   *(DestinationAddr  + 3) = (unsigned char)((Dec) & 0xff);
  }


Quote:
> hello all,

> I have a function which i needed...to convert a value from dec to
> hexadecimal....and sent via a packet array into a particular equipment
> for remote serial comms control....

> here's the function:

> unsigned char CASM::dec2hex(int i_Value)
> {

> int i=0;
> unsigned char buffer[8];

> while(i_Value > 0){
> buffer[i++] = i_Value%16;
> i_Value = i_Value/16;
> }

> for (int j=j-1; j>=0; j--)
> {
> if(buffer[j]==10)
> buffer[j] = 'A';
> else if(buffer[j]==11)
> buffer[j] = 'B';
> else if(buffer[j]==12)
> buffer[j] = 'C';
> else if(buffer[j]==13)
> buffer[j] = 'D';
> else if(buffer[j]==14)
> buffer[j] = 'E';
> else if(buffer[j]==15)
> buffer[j] = 'F';
> else
> buffer[j] = buffer[j];
> }
> return NULL; //*buffer; << ARGH!?
> }

> there're a few issues here....

> 1. I cannot put this into a separate function and call it when i
> needed to cos it's not returning the proper array.

> 2. the output is not what i wanted:
> if the value to be converted is 75000000, i am getting this values in
> return:
> 00 04 07 08 06 08 0c 00

> we know that:
> 75000000 = 0478 68C0H

> in my equipment i need to send it as
> Packet[1] = 0x04;
> Packet[2] = 0x78;
> Packet[3] = 0x68;
> Packet[4] = 0xC0;

> but the conversion is sending it as
> Packet[1] = 0x00;
> Packet[2] = 0x04;
> Packet[3] = 0x07;
> Packet[4] = 0x08;

> Is there a easier way to do this conversions? I will also have to
> convert the return hex values to binary to read them off for data
> later in my development....



Mon, 13 Dec 2004 19:25:06 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Help... conversion of hex to dec

2. Text to Hex to Dec conversion

3. BIN HEX DEC conversions...HELP!!!!

4. String Hex to Dec conversion.

5. converting hex to dec (from a file)

6. hex to dec - strtol works best!!

7. HEX to DEC IMAL problemo

8. Converting from hex to dec

9. hex to dec function?

10. hex to dec

11. Convert Dec to Hex??

12. #define DEC or HEX?

 

 
Powered by phpBB® Forum Software