| | |
character hexadeximal
![]() |
•
•
Join Date: Apr 2008
Posts: 29
Reputation:
Solved Threads: 0
I want to convert a character to hexadecimal value.
and that value should be a unsigned char value.
e.g.
unsigned char a[16] = {0x00,0x44,...};
this is the code segment i wrote.
but its not working properly.
unsigned char a1= 'D';
unsigned char a[4] ;
unsigned char temp[16];
sprintf((char *)a,"0x%02X",a1);
cout<<a;
unsigned char *b=a;
cout<<b;
temp[0] = b;
can anyone help me to overcome from this issue
and that value should be a unsigned char value.
e.g.
unsigned char a[16] = {0x00,0x44,...};
this is the code segment i wrote.
but its not working properly.
unsigned char a1= 'D';
unsigned char a[4] ;
unsigned char temp[16];
sprintf((char *)a,"0x%02X",a1);
cout<<a;
unsigned char *b=a;
cout<<b;
temp[0] = b;
can anyone help me to overcome from this issue
'D' isn't likely to have the value 13. You need to convert the character to a hexadecimal digit value:
By the way, you store the hexadecimal values as unsigned char, but the string representation should be char. There's no type mismatch here as you're using sprintf to convert the values to a character representation.
C++ Syntax (Toggle Plain Text)
#include <cctype> // Return [0,15] on success, -1 on failure int hex_value ( unsigned char c ) { if ( std::isxdigit ( c ) ) { // Assume 'A' - 'F' have consecutive values return std::isdigit ( c ) ? c - '0' : toupper ( c ) - 'A' + 10; } return -1; }
C++ Syntax (Toggle Plain Text)
sprintf ( a, "0x%02X", hex_value ( a1 ) );
In case you were wondering, yes, I do hate you.
![]() |
Other Threads in the C++ Forum
- Previous Thread: c++ determine number of elements help
- Next Thread: Void Shift
Views: 443 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm array arrays assignment beginner binary c++ c/c++ calculator char class classes client code command compile compiler console constructor conversion convert count delete dll dynamic encryption error exception file files fstream function functions game givemetehcodez graph gui helpwithhomework homework http iamthwee ifstream input int lazy linker list loop looping loops math matrix member memory multidimensional newbie number object objects opengl output parameter pointer pointers problem program programming project qt random read recursion recursive reference simple sockets sort spoonfeeding string strings struct student studio system template templates text time tree url variable vc++ vector video visual win32 window windows winsock xml






