473,883 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

128-bit hex number ---> string buffer

ern
Does a function exist to convert a 128-bit hex number to a string?

Sep 20 '06 #1
14 11979
ern said:
Does a function exist to convert a 128-bit hex number to a string?
There is no such thing as a "hex number". Hexadecimal is a numeric
representation system, not a kind of number.

If you have a hex representation, you already *have* a string, and
converting a string to a string is simple enough.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 20 '06 #2
"ern" <er*******@gmai l.comwrites:
Does a function exist to convert a 128-bit hex number to a string?
What format is the number in to start out with? I ask because
it's strange to specify that a number is in hex if it's simply in
an integer variable. Normally, one thinks of a numeric variable
as simply having a value, and the base is not important. Also: C
doesn't guarantee the existence of an 128-bit integer type.
--
"Am I missing something?"
--Dan Pop
Sep 20 '06 #3
ern

Richard Heathfield wrote:
ern said:
Does a function exist to convert a 128-bit hex number to a string?

There is no such thing as a "hex number". Hexadecimal is a numeric
representation system, not a kind of number.

If you have a hex representation, you already *have* a string, and
converting a string to a string is simple enough.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Let me rephrase. I have a 128 bit value, which I would like to be
converted to a hexadecimally represented string.

For example,

If my 128 bit value (represented hexadecimally) was:

4D9479E256DD4E8 A923E32065141A9 11

I would want this function to populate a char buffer such that:

charBuffer = "4D9479E256DD4E 8A923E32065141A 911"
If you have a hex representation, you already *have* a string, and
converting a string to a string is simple enough.
Last time I checked... http://www.asciitable.com/

the string representation "4" has the hex value 0x34, contradicting
your above statement. You *always* have a string, though (as in this
case) it might not be the one you need.

Sep 20 '06 #4
In article <11************ *********@i3g20 00cwc.googlegro ups.com>,
ern <er*******@gmai l.comwrote:
>Does a function exist to convert a 128-bit hex number to a string?
There is no library function to do that directly, but you can
write it easily enough if you are willing to assume a specific
character set (such as ASCII), and if you know the encoding
represented by the number.

For example, one that would work for one encoding combination is,
#include <stdio.h>

int main(void) {
const char hexnum[33] = "54686520616e73 776572206973203 432";
unsigned short tmpbuf[16];
int i;

sscanf( hexnum,
"%2hx%2hx%2hx%2 hx%2hx%2hx%2hx% 2hx%2hx%2hx%2hx %2hx%2hx%2hx%2h x%2hx",
&tmpbuf[0], &tmpbuf[1], &tmpbuf[2], &tmpbuf[3],
&tmpbuf[4], &tmpbuf[5], &tmpbuf[6], &tmpbuf[7],
&tmpbuf[8], &tmpbuf[9], &tmpbuf[10], &tmpbuf[11],
&tmpbuf[12], &tmpbuf[13], &tmpbuf[14], &tmpbuf[15] );

for (i=0; i<16; i++) printf("%c", (unsigned char) tmpbuf[i] );
printf("\n");

return 0;
}

--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Sep 20 '06 #5
ern said:
>
Richard Heathfield wrote:
>ern said:
Does a function exist to convert a 128-bit hex number to a string?

There is no such thing as a "hex number". Hexadecimal is a numeric
representati on system, not a kind of number.

If you have a hex representation, you already *have* a string, and
converting a string to a string is simple enough.
Let me rephrase. I have a 128 bit value, which I would like to be
converted to a hexadecimally represented string.
Okay. Since C doesn't guarantee the existence of 128-bit integer types, let
us assume that you have your value in an array of unsigned char, which we
will treat as a bit array.

Here's some preprocessing stuff:

#include <stddef.h>
#include <limits.h>

#define BYTE(x) ((x) / CHAR_BIT)
#define BIT(x) ((x) % CHAR_BIT)
#define SET_BIT(a, b) \
(a)[BYTE(b)] |= (1 << (BIT(b)))
#define CLEAR_BIT(a, b) \
(a)[BYTE(b)] &= ~(1 << (BIT(b)))
#define TEST_BIT(a, b) \
(!!(((a)[BYTE(b)]) & (1 << (BIT(b)))))

Okay, here's our routine, which requires a pointer to the first of len bytes
of input and a pointer to the first of len * 2 + 1 bytes of output. I
should warn you that I have *not* tested it very much! Especially the code
that deals with weird byte sizes. So it's likely to have a hole or two. But
it should give you the general idea, and is not limited to 128-bit inputs
(but if that is what you want and CHAR_BIT is 8, then this routine expects
len to be 16):

void to_hex(char *out, unsigned char *in, size_t len)
{
char hexabet[] = "0123456789ABCD EF";
size_t bits = len * CHAR_BIT;
size_t bit = 0;
int ch = 0;

if(bits & 7)
{
bit = bits & 7; bits &= ~7;

while(bit != 0)
{
ch <<= 1;
ch |= TEST_BIT(in, bit);
--bit;
}
*out++ = hexabet[ch];
}

for(; bit < bits; bit += 8)
{
*out++ = hexabet[(TEST_BIT(in, bit + 7) << 3) |
(TEST_BIT(in, bit + 6) << 2) |
(TEST_BIT(in, bit + 5) << 1) |
TEST_BIT(in, bit + 4)];
*out++ = hexabet[(TEST_BIT(in, bit + 3) << 3) |
(TEST_BIT(in, bit + 2) << 2) |
(TEST_BIT(in, bit + 1) << 1) |
TEST_BIT(in, bit)];
}
*out = '\0';
}

And here's a driver:

#include <stdio.h>

int main(void)
{
unsigned char in[] =
{
0x4D, 0x94, 0x79, 0xE2,
0x56, 0xDD, 0x4E, 0x8A,
0x92, 0x3E, 0x32, 0x06,
0x51, 0x41, 0xA9, 0x11
};
char out[33] = {0};
to_hex(out, in, 16);
printf("%s\n", out);
return 0;
}

>If you have a hex representation, you already *have* a string, and
converting a string to a string is simple enough.

Last time I checked... http://www.asciitable.com/
ASCII has nothing to do with C.
the string representation "4" has the hex value 0x34,
You seem to be confusing character sets, representations , and values. The
string "4" comprises two bytes, { '4', '\0' }. The code point of '4' is
implementation-defined. On some systems it is indeed 0x34, but on others
it's 0xF4 or some other value.
contradicting your above statement.
If it contradicts what I said, then it's wrong, because what I said is
right.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 20 '06 #6
Walter Roberson said:
In article <11************ *********@i3g20 00cwc.googlegro ups.com>,
ern <er*******@gmai l.comwrote:
>>Does a function exist to convert a 128-bit hex number to a string?

There is no library function to do that directly, but you can
write it easily enough if you are willing to assume a specific
character set (such as ASCII),
You don't actually need to assume this. See my parallel reply.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 20 '06 #7
ern

Walter Roberson wrote:
In article <11************ *********@i3g20 00cwc.googlegro ups.com>,
ern <er*******@gmai l.comwrote:
Does a function exist to convert a 128-bit hex number to a string?

There is no library function to do that directly, but you can
write it easily enough if you are willing to assume a specific
character set (such as ASCII), and if you know the encoding
represented by the number.

For example, one that would work for one encoding combination is,
#include <stdio.h>

int main(void) {
const char hexnum[33] = "54686520616e73 776572206973203 432";
unsigned short tmpbuf[16];
int i;

sscanf( hexnum,
"%2hx%2hx%2hx%2 hx%2hx%2hx%2hx% 2hx%2hx%2hx%2hx %2hx%2hx%2hx%2h x%2hx",
&tmpbuf[0], &tmpbuf[1], &tmpbuf[2], &tmpbuf[3],
&tmpbuf[4], &tmpbuf[5], &tmpbuf[6], &tmpbuf[7],
&tmpbuf[8], &tmpbuf[9], &tmpbuf[10], &tmpbuf[11],
&tmpbuf[12], &tmpbuf[13], &tmpbuf[14], &tmpbuf[15] );

for (i=0; i<16; i++) printf("%c", (unsigned char) tmpbuf[i] );
printf("\n");

return 0;
}

--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Thanks for the code, but that's the inverse of what I need.

Let me make the problem simpler. Say I have a 32-bit integer. I what
the hexadecimally represented string version.

So the integer is the INPUT, and the hexadecimally represented string
is the OUTPUT.

Sep 20 '06 #8
ern posted:
If my 128 bit value (represented hexadecimally) was:

4D9479E256DD4E8 A923E32065141A9 11

I would want this function to populate a char buffer such that:

charBuffer = "4D9479E256DD4E 8A923E32065141A 911"
If you have an aversion to the Standard Library, then maybe something along
the lines of the following. (This sample is capped at 32-Bit, but this is
easily adjusted.)

#include <assert.h>

#define MAX_VAL 0xFFFFFFFFu
#define MAX_DIVISOR 0x10000000u
#define MAX_HEX_DIGITS 8

typedef long unsigned IntType;

void IntToHexStr(Int Type val,char *const buf)
{
int const assert_dummy = ( assert(val <= MAX_VAL),
assert(!!buf),
0);

static char const digits[16] = "0123456789ABCD EF";
/* No terminating null character */

IntType divisor = MAX_DIVISOR;

char *p = buf;

do
{
/* Maybe "ldiv" should be used... ? */

unsigned const digit_val = val / divisor;
val %= divisor;
divisor /= 0x10;

if(digit_val) *p++ = digits[digit_val];
} while(divisor);

if(buf==p) *p++ = '0';

*p = 0;
}

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
char buf[MAX_HEX_DIGITS+ 1];

IntToHexStr(0x7 78FEE34,buf);

puts(buf);

return 0;
}

--

Frederick Gotham
Sep 20 '06 #9
ern

Richard Heathfield wrote:
ern said:

Richard Heathfield wrote:
ern said:

Does a function exist to convert a 128-bit hex number to a string?

There is no such thing as a "hex number". Hexadecimal is a numeric
representation system, not a kind of number.

If you have a hex representation, you already *have* a string, and
converting a string to a string is simple enough.
Let me rephrase. I have a 128 bit value, which I would like to be
converted to a hexadecimally represented string.

Okay. Since C doesn't guarantee the existence of 128-bit integer types, let
us assume that you have your value in an array of unsigned char, which we
will treat as a bit array.

Here's some preprocessing stuff:

#include <stddef.h>
#include <limits.h>

#define BYTE(x) ((x) / CHAR_BIT)
#define BIT(x) ((x) % CHAR_BIT)
#define SET_BIT(a, b) \
(a)[BYTE(b)] |= (1 << (BIT(b)))
#define CLEAR_BIT(a, b) \
(a)[BYTE(b)] &= ~(1 << (BIT(b)))
#define TEST_BIT(a, b) \
(!!(((a)[BYTE(b)]) & (1 << (BIT(b)))))

Okay, here's our routine, which requires a pointer to the first of len bytes
of input and a pointer to the first of len * 2 + 1 bytes of output. I
should warn you that I have *not* tested it very much! Especially the code
that deals with weird byte sizes. So it's likely to have a hole or two. But
it should give you the general idea, and is not limited to 128-bit inputs
(but if that is what you want and CHAR_BIT is 8, then this routine expects
len to be 16):

void to_hex(char *out, unsigned char *in, size_t len)
{
char hexabet[] = "0123456789ABCD EF";
size_t bits = len * CHAR_BIT;
size_t bit = 0;
int ch = 0;

if(bits & 7)
{
bit = bits & 7; bits &= ~7;

while(bit != 0)
{
ch <<= 1;
ch |= TEST_BIT(in, bit);
--bit;
}
*out++ = hexabet[ch];
}

for(; bit < bits; bit += 8)
{
*out++ = hexabet[(TEST_BIT(in, bit + 7) << 3) |
(TEST_BIT(in, bit + 6) << 2) |
(TEST_BIT(in, bit + 5) << 1) |
TEST_BIT(in, bit + 4)];
*out++ = hexabet[(TEST_BIT(in, bit + 3) << 3) |
(TEST_BIT(in, bit + 2) << 2) |
(TEST_BIT(in, bit + 1) << 1) |
TEST_BIT(in, bit)];
}
*out = '\0';
}

And here's a driver:

#include <stdio.h>

int main(void)
{
unsigned char in[] =
{
0x4D, 0x94, 0x79, 0xE2,
0x56, 0xDD, 0x4E, 0x8A,
0x92, 0x3E, 0x32, 0x06,
0x51, 0x41, 0xA9, 0x11
};
char out[33] = {0};
to_hex(out, in, 16);
printf("%s\n", out);
return 0;
}

If you have a hex representation, you already *have* a string, and
converting a string to a string is simple enough.
Last time I checked... http://www.asciitable.com/

ASCII has nothing to do with C.
the string representation "4" has the hex value 0x34,

You seem to be confusing character sets, representations , and values. The
string "4" comprises two bytes, { '4', '\0' }. The code point of '4' is
implementation-defined. On some systems it is indeed 0x34, but on others
it's 0xF4 or some other value.
contradicting your above statement.

If it contradicts what I said, then it's wrong, because what I said is
right.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Thanks for the help all. I will try and apply the help to solve the
problem.

Sep 20 '06 #10

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

Similar topics

0
2647
by: Connelly Barnes | last post by:
Yet another useful code snippet! This StringBuffer class is a FIFO for character data. Example: B = StringBuffer('Hello W') B.append('orld!') print B.read(5) # 'Hello' print B.read() # 'World!'
1
36098
by: Ren? M?hle | last post by:
I have a psp script with a procedure just to run an update on one table. The Problem occurs when I try to compile this script with pspload: ORA-20006: "frsys_updatereport.psp": compilation failed with the following errors. ORA-06502: PL/SQL: numeric or value error: character string buffer too small Here the whole script:
6
1463
by: ±è¿ë°Ç | last post by:
my project has critical bug that TTS(text to speech) engine has two thread one thread is socket thread that receives string from socket server and other thread is engine thread that processes the TTS(text to speech) the received string is dynamically allocated (char * pRcvstr) i think the bug occured when socket thread receives string buffer while engine thread processes the other TTS, because of two threads share one memory pointer
1
2342
by: Srini | last post by:
Hi, I am working on a project and a portion of which involves receiving xml files on internet, extract values to build a string and pass that string to legacy system. I am planning on using XMLReader to read passed xml file and extract values that need to be mapped to string buffer. However, in this method I am not able to get the full path (ie., path information from root to this node)
2
1202
by: Bae,Hyun-jik | last post by:
Hi, My managed C++ library frequently takes LPCTSTR from managed exe. Due to the fact that my library doesn't modify string buffer if its parameter type is LPCTSTR, it won't be required to copy text data from System::String to another buffer memory, however, I couldn't find how to let my library access System::String text buffer directly. Please reply any answers. Thanks in advance.
3
5723
by: s88 | last post by:
Hello All: I'm trying to make a string buffer for my APIs. For example: void test(char * str){ sprintf(str,"inner test\n"); } int main(void){ int i = -5; char *str=(char*)malloc(sizeof(char)*100);
2
26716
by: pbmanikandan | last post by:
Horrible error. I use a function called cleanse company name that "removes" some strings if present from a string that is passed to it. In a procedure, I do the following. INSERT into table1 SELECT * from table2 where cleanse_company_name(company_name) IS NULL; NOW, this function SOMETIMES throws the error ORA-06502: PL/SQL: numeric or value error: character string buffer too small
8
4059
by: Zytan | last post by:
When using DllImport to import win32 functions that have strings, I know you can use the native c# string type. But, what about a function that expects a string buffer, and writes to it? I've seen people use StringBuilder for this, but is string good enough? The problem is that I am seeing what appears to be uninitialized data when I use StringBuilder, so I am wonder what is the correct solution thanks Zytan
5
2045
by: rsennat | last post by:
Hi, I'm having the following structures and std::list of that structs. I need to read the list of list as below and build a buffer as a table information. Any idea on how to build a table info (string buffer). list<NVPairsWithId> m_ResponseObj; struct NVPairsWithId {
0
9932
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11109
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7957
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5782
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5979
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.