便携式ascii-hex转换 [英] portable ascii-hex conversion

查看:79
本文介绍了便携式ascii-hex转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,


我有一系列字符需要转换为整数值。

每个字符依次从函数中读取' 'nextch''和十六进制数字是由isxdigit函数识别的
- 所以我在看''0'' - ''9'',''A'' - ' 'Z''

和'''' - ''z''。


这就是我所拥有的:


int num = 0;

int ch = nextch(); / * nextc获得下一个字符值* /


while(isxdigit(ch))

{

if(isdigit( ch))

ch = ch - ''0''; / *这是便携式我相信* /

其他

ch =(ch& ~0x20) - ''A''+ 10; / *不确定这是否正常* /


num = num * 0x10 + ch;

ch = nextch();

}


如果你看看while()循环中的if-else语句,你会看到

我如何尝试转换''ch ''从字符值到数字值在

范围内0-15(含)。但我怀疑((ch& ~0x20) - ''A''+

10)表达式:


它假设'' '' - ''F''是连续的值

它假设'''' - ''''''是连续的,并且总是比它们的价格高出$

''大写''对应物。


这些假设是否正确?我猜这段代码是不便携的,所以

有没有人有一个整洁的(呃)建议?


p.s.我从lcc编译器源代码中派生出这个代码...


James

All,

I have a series of characters which I need to convert to integer values.
Each character is read in turn from a function ''nextch'', and hex-digits are
identified by the isxdigit function - so I''m looking at ''0'' - ''9'', ''A'' - ''Z''
and ''a'' - ''z''.

Here is what I''ve got:

int num = 0;
int ch = nextch(); /* nextc obtains the next character value */

while(isxdigit(ch))
{
if(isdigit(ch))
ch = ch - ''0''; /* this is portable I believe */
else
ch = (ch & ~0x20) - ''A'' + 10; /* not sure if this is ok */

num = num * 0x10 + ch;
ch = nextch();
}

If you look at the if-else statement inside the while() loop, you will see
how I attempt to convert ''ch'' from a character-value to a numeric value in
the range 0-15 inclusive. But I have doubts about the ((ch & ~0x20) - ''A'' +
10) expression:

It assumes that ''A'' - ''F'' are consecutive values
It assumes that ''a'' - ''f'' are consecutive, and are always 0x20 above their
''uppercase'' counterparts.

Are these assumptions correct? I''m guessing the code is non-portable, so
does anyone have a neat(er) suggestion?

p.s. I derived this code from the lcc compiler sourcecode...

James

推荐答案

在文章< lt********************@pipex.net>,

James Brown< no*@home.netwrote:
In article <lt********************@pipex.net>,
James Brown <no*@home.netwrote:

>我有一系列字符需要转换为整数值。
每个字符依次从函数''nextch''读取,并且十六进制-digits由isxdigit函数识别 - 所以我看''0'' - ''9'','''' - '''Z''
和''一个'' - ''z''。
>I have a series of characters which I need to convert to integer values.
Each character is read in turn from a function ''nextch'', and hex-digits are
identified by the isxdigit function - so I''m looking at ''0'' - ''9'', ''A'' - ''Z''
and ''a'' - ''z''.


>这是我得到的:
>Here is what I''ve got:


> int num = 0;
int ch = nextch(); / * nextc获取下一个字符值* /
>int num = 0;
int ch = nextch(); /* nextc obtains the next character value */


> while(isxdigit(ch))
>while(isxdigit(ch))



如果是EOF怎么办?

What if it was EOF ?


> {

if(isdigit(ch))

ch = ch - ''0''; / *这是便携式我相信* /
>{
if(isdigit(ch))
ch = ch - ''0''; /* this is portable I believe */



是的。

Yes.


else

ch =(ch& ~0x20) - ''A''+ 10; / *不确定这是否正常* /
else
ch = (ch & ~0x20) - ''A'' + 10; /* not sure if this is ok */



& ~0x20是一个隐藏的toupper(),不能移植到非ASCII。


正如你所想的那样,A到F并不能保证是

结果甚至增加订单。

The & ~0x20 is a hidden toupper() and not portable to non-ASCII.

And as you had thought, ''A'' through ''F'' are not guaranteed to be
consequative or even increasing order.


num = num * 0x10 + ch;
num = num * 0x10 + ch;



如果你的int溢出怎么办?

What if you overflow your int ?


ch = nextch();
}
ch = nextch();
}


>这些假设是否正确?我猜测代码是不可移植的,所以
有没有人有一个整洁的(呃)建议?
>Are these assumptions correct? I''m guessing the code is non-portable, so
does anyone have a neat(er) suggestion?



只要isxdigit(ch)获取更多ch,你就没有比你可以处理更多的字符
更多的字符和存储将它们放入缓冲区。

然后strtoul()指定基数16.

如果您有特殊原因自己处理角色,

然后创建一个大小为UCHAR_MAX的翻译表,

并初始化它,tr [''0''+ i] = i for i从0到9,并且

tr [''A''] = 10,tr [''B''] = 11等,tr [''a''] = 10,tr [''b''] = 11等。 ,

然后进行转换,只需确定isxdigit(ch),如果是这样,那么转换后的值为tr [ch]。是的,这有可能浪费UCHAR_MAX - 26个插槽,但它也是一个便携式单步

转换,没有数学(除了正常的数组索引)

-

编程是在你忙于制定其他计划时发生的事情。

fetch more ch as long as isxdigit(ch) and you haven''t gotten
more chars than you can handle, and store them into a buffer.
Then strtoul() specifying base 16.

If you have particular reasons for handling the characters yourself,
then create a translation table of size UCHAR_MAX,
and initialize it, tr[''0''+i] = i for i from 0 to 9, and
tr[''A''] = 10, tr[''B''] = 11, etc., tr[''a''] = 10, tr[''b''] = 11, etc.,
then to do the conversion, just determine isxdigit(ch) and if so
then the converted value is tr[ch]. Yes, this has the potential
to waste UCHAR_MAX - 26 slots, but it is also a portable single-step
conversion with no math (other than normal array indexing)
--
Programming is what happens while you''re busy making other plans.


" Walter Roberson" ; < ro ****** @ ibd.nrc-cnrc.gc.cawrote in message

news:ei ********** @ canopus.cc.umanitoba.ca ...
"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.cawrote in message
news:ei**********@canopus.cc.umanitoba.ca...

在文章< lt ******************** @ pipex.net>中,

James Brown< no*@home.netwrote:
In article <lt********************@pipex.net>,
James Brown <no*@home.netwrote:

>>我有一系列我需要转换的字符到整数值。
每个字符依次从函数''nextch''读取,而十六进制数字
由isxdigit函数识别 - 所以我在看' '0'' - ''9'',''A'' -
''Z''
和'''' - ''z''。
>>I have a series of characters which I need to convert to integer values.
Each character is read in turn from a function ''nextch'', and hex-digits
are
identified by the isxdigit function - so I''m looking at ''0'' - ''9'', ''A'' -
''Z''
and ''a'' - ''z''.


>>这是我得到的:
>>Here is what I''ve got:


>> int num = 0;
int ch = nextch(); / * nextc获取下一个字符值* /
>>int num = 0;
int ch = nextch(); /* nextc obtains the next character value */


>> while(isxdigit(ch))
>>while(isxdigit(ch))



如果是EOF怎么办?


What if it was EOF ?



我以为会没事的? ch将是EOF,这将导致isxdigit

返回(0),并且循环将爆发。这不是会发生什么吗?

I thought it would be ok? ch would be EOF, which would cause isxdigit to
return(0), and the loop would break out. Is this not what would happen?


>
>

>> {
if(isdigit(ch))
ch = ch - ''0''; / *这是便携式我相信* /
>>{
if(isdigit(ch))
ch = ch - ''0''; /* this is portable I believe */



是的。


Yes.


>否则
ch =(ch& ~0x20) - ''A''+ 10; / *不确定这是否正常* /
> else
ch = (ch & ~0x20) - ''A'' + 10; /* not sure if this is ok */



& ~0x20是一个隐藏的toupper(),不能移植到非ASCII。


正如你所想的那样,A到F并不能保证是

结果甚至增加订单。


The & ~0x20 is a hidden toupper() and not portable to non-ASCII.

And as you had thought, ''A'' through ''F'' are not guaranteed to be
consequative or even increasing order.


> num = num * 0x10 + ch;
> num = num * 0x10 + ch;



如果溢出int怎么办?


What if you overflow your int ?



是的,我没有检查溢出,这是我的下一个任务。

yes, I hadn''t gotten as far as checking for overflow, that''s my next task.


>
>

> ch = nextch();
}
> ch = nextch();
}


>>这些假设是否正确?我猜测代码是不可移植的,所以
有没有人有一个整洁的(呃)建议?
>>Are these assumptions correct? I''m guessing the code is non-portable, so
does anyone have a neat(er) suggestion?



只要isxdigit(ch)获取更多ch,你就没有比你可以处理更多的字符
更多的字符和存储将它们放入缓冲区。

然后strtoul()指定基数16.


fetch more ch as long as isxdigit(ch) and you haven''t gotten
more chars than you can handle, and store them into a buffer.
Then strtoul() specifying base 16.



绝对是一个不错的解决方案,但我认为这很难检测溢出?

我的strtoul编译器文档说它在

溢出时返回ULONG_MAX,但是当我遇到
$时如何区分它? b $ b实际ULONG_MAX值?这就是为什么我对这个东西进行手工编码,这样我就可以在发生这种情况时发出适当的警告信息。

Definitely a nice solution, but I think it will be hard to detect overflows?
My compiler documentation for strtoul says that it returns ULONG_MAX on
overflow, but how do I distinguish this from the case when I encounter the
actual ULONG_MAX value? This is why I am hand-coding this thing, so that I
can emit appropriate warning messages when such things happen.


>

如果您有特殊原因自己处理角色,

然后创建一个大小为UCHAR_MAX的翻译表,

并初始化它,tr [ ''0''+ i] = i for i从0到9,

tr [''A''] = 10,tr [''B''] = 11等。 ,tr [''a''] = 10,tr [''b''] = 11等等,然后
进行转换,只需确定isxdigit(ch),如果是, br />
然后转换后的值是tr [ch]。是的,这有可能浪费UCHAR_MAX - 26个插槽,但它也是一个便携式单步

转换,没有数学(除了正常的数组索引)

-

编程是在你忙于制定其他计划时发生的事情。
>
If you have particular reasons for handling the characters yourself,
then create a translation table of size UCHAR_MAX,
and initialize it, tr[''0''+i] = i for i from 0 to 9, and
tr[''A''] = 10, tr[''B''] = 11, etc., tr[''a''] = 10, tr[''b''] = 11, etc.,
then to do the conversion, just determine isxdigit(ch) and if so
then the converted value is tr[ch]. Yes, this has the potential
to waste UCHAR_MAX - 26 slots, but it is also a portable single-step
conversion with no math (other than normal array indexing)
--
Programming is what happens while you''re busy making other plans.



我绝对会认为这是一个解决方案 - 我希望有一个1/2衬里

(调用一个c-runtime func是理想的),但它看起来像一个查找表

可能是最合适的前进方式。我不太关心

的性能 - 我更喜欢一个简单的循环。


谢谢,

James


I''ll definitely consider this as a solution - I was hoping for a 1/2 liner
(calling a c-runtime func would be ideal), but it looks like a lookup table
may be the most appropriate way forward. I''m not too concerned with
performance though - I would prefer a simple loop above all else.

thanks,
James



>>
>>

>> num = num * 0x10 + ch;
>> num = num * 0x10 + ch;


如果你的int溢出怎么办?


What if you overflow your int ?



是的,我没有检查溢出,这是我的下一个任务。


yes, I hadn''t gotten as far as checking for overflow, that''s my next task.


>>
>>

>> ch = nextch();
}
>> ch = nextch();
}


>>>这些假设是否正确?我猜测代码是不可移植的,所以
有没有人有一个整洁的(呃)建议?
>>>Are these assumptions correct? I''m guessing the code is non-portable, so
does anyone have a neat(er) suggestion?


只要isxdigit(ch)获取更多ch,你就没有比你能处理的更多的字符,并将它们存储到缓冲区中。
然后strtoul()指定基数16.


fetch more ch as long as isxdigit(ch) and you haven''t gotten
more chars than you can handle, and store them into a buffer.
Then strtoul() specifying base 16.



绝对是一个很好的解决方案,但我认为很难检测到

溢出?我对strtoul的编译器文档表示它在溢出时返回

ULONG_MAX,但是我如何区分这与
遇到实际ULONG_MAX值时的情况?这就是为什么我手动编码这个

的东西,以便在发生这样的事情时我可以发出适当的警告信息。


Definitely a nice solution, but I think it will be hard to detect
overflows? My compiler documentation for strtoul says that it returns
ULONG_MAX on overflow, but how do I distinguish this from the case when I
encounter the actual ULONG_MAX value? This is why I am hand-coding this
thing, so that I can emit appropriate warning messages when such things
happen.



ok,所以我读了其余的strtoul文档,并说''errno''设置为

溢出/下溢。看起来这是我的首选解决方案,感谢

帮助。


James

ok, so I read the rest of the strtoul docs and it says ''errno'' is set for
overflow/underflow. Looks like this is my preferred solution, thanks for the
help.

James


这篇关于便携式ascii-hex转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆