473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Convert hex string to byte

Hi

I've got the following problem:
In one of my programs I receive a string that contains a hex number (e.g. "B4"). Now, I want to convert this "number" into a var of type BYTE. How can this be done

Thanks a lo
Gordon
Nov 17 '05 #1
3 14369
Here you have two solutions:

=======================
#include "stdafx.h"
#include <map>
#include <string>

using namespace std;

map<char, unsigned short int> myMap;

unsigned short int ToHex( char ch )
{
unsigned short int ret = ch - '0';
if( ret <=9 && ret >=0 )
return ret;

return (ch - 'A' ) + 0x0A;
}
int main(int argc, char* argv[])
{
myMap['0'] = 0;
myMap['1'] = 1;
myMap['2'] = 2;
myMap['3'] = 3;
myMap['4'] = 4;
myMap['5'] = 5;
myMap['6'] = 6;
myMap['7'] = 7;
myMap['8'] = 8;
myMap['9'] = 9;
myMap['A'] = 0x0A;
myMap['B'] = 0x0B;
myMap['C'] = 0x0C;
myMap['D'] = 0x0D;
myMap['E'] = 0x0E;
myMap['F'] = 0x0F;

string str = "B4";

unsigned short int n = myMap[ str[0] ] * 16 + myMap[ str[1] ];
unsigned short int m = ToHex( str[0] ) * 16 + ToHex( str[1] );

return 0;
}

"Gordon Knote" <an*******@discussions.microsoft.com> wrote in message
news:74**********************************@microsof t.com...
Hi,

I've got the following problem:
In one of my programs I receive a string that contains a hex number (e.g. "B4"). Now, I want to convert this "number" into a var of type BYTE. How can
this be done?
Thanks a lot
Gordon

Nov 17 '05 #2
Gordon Knote wrote:
I've got the following problem:
In one of my programs I receive a string that contains a hex number
(e.g. "B4"). Now, I want to convert this "number" into a var of type
BYTE. How can this be done?


One way is to use the standard C function strtol, specifying a base of
16, and then casting the result from long to byte. For example:

BYTE result = (BYTE)strtol("B4", NULL, 16);

See the documentation of strtol for more details.

--
David Olsen
qg********@yahoo.com

Nov 17 '05 #3
Gordon Knote <an*******@discussions.microsoft.com> wrote:
Hi,

I've got the following problem:
In one of my programs I receive a string that contains a hex
number (e.g. "B4"). Now, I want to convert this "number" into a var
of type BYTE. How can this be done?
#include <sstream>

typedef unsigned char byte_t;

inline byte_t convertFromHex(const char* str)
{
std::istringstream iss(str);
byte_t byte;
iss >> std::hex >> byte;
if( !iss ) throw my_exception(str, "is not a hex number");
return byte;
}
Thanks a lot
HTH,
Gordon


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored...
1
by: Swarup | last post by:
I am reading a file (txt, xml, gif, ico, bmp etc) byte by byte and filling it into a byte arry. Now i have to convert it into a string to store it in the database. I use...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
6
by: Bob Altman | last post by:
Hi all, I'm looking for the fastest way to convert an array of bytes to String. I also need to convert a String back to its original Byte() representation. Convert.ToBase64String and...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
19
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.